Newbie in need - can't even follow a drunken tutorial! |
[eluser]LukeZach[/eluser]
Hi Guys, I'm sure I am being an idiot, but I guess that is what being a newbie is all about. Was trying to follow Michael Wales's tutorial on putting in a 'Contact Us' form to my first CI website ... and my first intro to php. in the controller i have : --- contact.php start --- <?php function Contact() { parent::Controller(); } class Contact extends Controller{ function index(){ $this->load->helper('form'); $this->load->view('headerview.php'); $this->load->view('contactview.php'); $this->load->view('footerview.php'); $this->load->library('validation'); $rules['name'] = "required|trim"; $rules['email'] = "required|valid_email|trim"; $rules['iamoption'] = "required"; $rules['body'] = "required|trim"; $this->validation->set_rules($rules); $fields['name'] = 'name'; $fields['email'] = 'email address'; $fields['iamoption'] = 'i am option'; $fields['msgbody'] = 'message body'; $this->validation->set_fields($fields); if ($this->validation->run()) { $this->load->view('success'); } else { $this->load->view('contactview'); } } } ?> --- contact.php controller end ---- and in the viewer file I have : ---- contactview.php start ---- <div id="content"> <div id="contact_main"> <?= if ($this->validation->error_string) { echo $this->validation->error_string; } ?> <?= form_open('contact'); ?> <p> <label for="Name">Name:</label><br /> <input type="text" name="Name" class="contactformfield" id="namefield" /> </p> <p> <label for="emailaddress">Email Address:</label> <br /> <input name="emailaddress" type="text" class="contactformfield" id="emailaddress" /> </p> <p> <label for="iamoption">Regarding:</label> <br /> <select name="iamoption" id="iamoption" class="contactformfield"> <option>New Business</option> <option>Existing Business</option> <option>University</option> <option>School</option> <option>Investor</option> <option>Press</option> <option>Other</option> </select> </p> <p> <label for="msgbody">Email Address:</label> <br /> <textarea name="msgbody" cols="10" rows="5" class="contactformfield" id="msgbody"></textarea> </p> <input type="submit" value="Send Mail" /> <?= form_close(); ?> </div> --- conactview.php end ---- I am getting this thrown up : Parse error: syntax error, unexpected T_IF in /Applications/xampp/xamppfiles/htdocs/lza/system/application/views/contactview.php on line 7 line7 : <?= if ($this->validation->error_string) followed by: { echo $this->validation->error_string; } ?> Anyone that can end my frustration (it's been well over two hours!) ... THANKS! Cheers, Luke line 7
[eluser]Kromack[/eluser]
Hi, I think you have made a mix between the standard PHP synthax and the alternative. You should have this : Code: <?php if($this->validation->error_string): ?> Or this : Code: <?php if ($this->validation->error_string) You can check this page to see how alternative PHP synthax works : http://ellislab.com/codeigniter/user-gui...e_php.html ![]()
[eluser]LukeZach[/eluser]
Thanks Summer, One problem down ... on to the next! Cheers mate, Luke
[eluser]Alex.[/eluser]
[quote author="LukeZach" date="1210005286"] --- contact.php start --- <?php function Contact() { parent::Controller(); } class Contact extends Controller{ function index(){ $this->load->helper('form'); $this->load->view('headerview.php'); $this->load->view('contactview.php'); $this->load->view('footerview.php'); $this->load->library('validation'); $rules['name'] = "required|trim"; $rules['email'] = "required|valid_email|trim"; $rules['iamoption'] = "required"; $rules['body'] = "required|trim"; $this->validation->set_rules($rules); $fields['name'] = 'name'; $fields['email'] = 'email address'; $fields['iamoption'] = 'i am option'; $fields['msgbody'] = 'message body'; $this->validation->set_fields($fields); if ($this->validation->run()) { $this->load->view('success'); } else { $this->load->view('contactview'); } } } ?> [/quote] The function Contact() is the Contact class' constructor, and should be within the class, like so: Code: <?php If it isn't, then your controller won't initiate properly and you'll have a lot of problems trying to access the core libraries / loader ![]() Alex
[eluser]LukeZach[/eluser]
Thanks a lot Alex, I am now stuck at another juncture! ----contactview----- <div id="content"> <div id="contact_main"> <div style="color: red; background-color: #ccc; border: 1px black solid;"> <?php if ($this->validation->error_string()) { echo $this->validation->error_string; } ?> </div> <?php form_open('contact'); ?> <p> <label for="name">Name:</label><br /> <input type="text" name="name" class="contactformfield" id="namefield" /> </p> <p> <label for="emailaddress">Email Address:</label> <br /> <input name="emailaddress" type="text" class="contactformfield" id="emailaddress" /> </p> <p> <label for="iamoption">Regarding:</label> <br /> <select name="iamoption" id="iamoption" class="contactformfield"> <option>New Business</option> <option>Existing Business</option> <option>University</option> <option>School</option> <option>Investor</option> <option>Press</option> <option>Other</option> </select> </p> <p> <label for="body">Email Address:</label> <br /> <textarea name="body" cols="10" rows="5" class="contactformfield" id="msgbody"></textarea> </p> <input type="submit" value="Send Mail" /> <?= form_close(); ?> </div> <div id="contact_right"> <a href="http://www.abcdefg.co.uk" target="_blank"> <img src="/../lza/images/scratch/contactab.png" /> </a> <br /> <a href="http://www.abcdefg.co.uk" target="_blank"> <img src="/../lza/images/scratch/contactab.png" /> </a> <br /> <a href="http://www.abcdefg.co.uk" target="_blank"> <img src="/../lza/images/scratch/contactab.png" /> </a> </div> </div> --end --- -- contact -- <?php class Contact extends Controller{ function index(){ $this->load->library('validation'); $rules['name'] = "required|trim"; $rules['emailaddress'] = "required|valid_email|trim"; $rules['iamoption'] = "required"; $rules['body'] = "required|trim"; $this->validation->set_rules($rules); $fields['name'] = 'name'; $fields['emailaddress'] = 'email address'; $fields['iamoption'] = 'i am option'; $fields['body'] = 'message body'; $this->validation->set_fields($fields); //if ($this->validation->run()) { // $this->load->view('mailsent'); // } // else{ // $this->load->view('contactview'); // } if ($this->validation->run() == FALSE) { $this->load->view('contactview'); } else { $this->load->view('mailsent'); } $this->load->view('headerview.php'); $this->load->view('contactview.php'); $this->load->view('footerview.php'); } } ?> ---end--- im getting the following thrown at me: Fatal error: Call to undefined method CI_Validation::error_string() in /Applications/xampp/xamppfiles/htdocs/lza/system/application/views/contactview.php on line 9 The only thing I have been able to deduce us that the error being thrown up is subject to the styling i put in on the div tag in codeview.php Thanks in advance, Luke
[eluser]James Gifford[/eluser]
There is no error_string function in the validation class, its just a variable. Also, you don't need to test the validation error string before echoing it out (you can if you want to though). Your code: Code: <?php if ($this->validation->error_string()) { Should just be: Code: <?php echo $this->validation->error_string; ?>
[eluser]LukeZach[/eluser]
Thanks James, still cant get the bloody thing working!! Another problem has cropped up now!! Thanks for your help mate L
[eluser]James Gifford[/eluser]
Also, I just noticed your opening form tag doesn't seem to be echoing anything Code: <?php form_open('contact'); ?> Try adding an echo: Code: <?php echo form_open('contact'); ?> As Kromack said, you are mixing the short and long php tags you might want to pick one type and stick with it. |
Welcome Guest, Not a member yet? Register Sign In |