Welcome Guest, Not a member yet? Register   Sign In
Newbie in need - can't even follow a drunken tutorial!
#1

[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">

&lt;?= if ($this->validation->error_string)
{
echo $this->validation->error_string;
} ?&gt;


&lt;?= form_open('contact'); ?&gt;


<p>
<label for="Name">Name:</label><br />
&lt;input type="text" name="Name" class="contactformfield" id="namefield" /&gt;
</p>
<p>
<label for="emailaddress">Email Address:</label> <br />
&lt;input name="emailaddress" type="text" class="contactformfield" id="emailaddress" /&gt;
</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 />
&lt;textarea name="msgbody" cols="10" rows="5" class="contactformfield" id="msgbody"&gt;&lt;/textarea&gt;
</p>

&lt;input type="submit" value="Send Mail" /&gt;

&lt;?= form_close(); ?&gt;



</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 : &lt;?= if ($this->validation->error_string)

followed by:

{
echo $this->validation->error_string;
} ?&gt;


Anyone that can end my frustration (it's been well over two hours!) ... THANKS!

Cheers,

Luke


line 7
#2

[eluser]Kromack[/eluser]
Hi,

I think you have made a mix between the standard PHP synthax and the alternative.

You should have this :

Code:
&lt;?php if($this->validation->error_string): ?&gt;

&lt;?=$this->validation->error_string;?&gt;

&lt;?php endif;?&gt;

Or this :

Code:
&lt;?php if ($this->validation->error_string)
{
  echo $this->validation->error_string;
} ?&gt;

You can check this page to see how alternative PHP synthax works : http://ellislab.com/codeigniter/user-gui...e_php.html

Wink
#3

[eluser]LukeZach[/eluser]
Thanks Summer,

One problem down ... on to the next!

Cheers mate,

Luke
#4

[eluser]Kromack[/eluser]
Quote:Thanks Summer,

:bug:
#5

[eluser]LukeZach[/eluser]
Kromack .... been awake too long!

Cheers
#6

[eluser]Alex.[/eluser]
[quote author="LukeZach" date="1210005286"]
--- contact.php start ---

&lt;?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');
}




}
}

?&gt;
[/quote]
The function Contact() is the Contact class' constructor, and should be within the class, like so:

Code:
&lt;?php

class Contact extends Controller{


    function Contact()
    {
        parent::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');
        }
    
    
        
    
    }
    }    

?&gt;

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 Wink

Alex
#7

[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;">

&lt;?php if ($this->validation->error_string()) {
echo $this->validation->error_string;
} ?&gt;

</div>
&lt;?php form_open('contact'); ?&gt;


<p>
<label for="name">Name:</label><br />
&lt;input type="text" name="name" class="contactformfield" id="namefield" /&gt;
</p>

<p>
<label for="emailaddress">Email Address:</label> <br />
&lt;input name="emailaddress" type="text" class="contactformfield" id="emailaddress" /&gt;
</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 />
&lt;textarea name="body" cols="10" rows="5" class="contactformfield" id="msgbody"&gt;&lt;/textarea&gt;
</p>

&lt;input type="submit" value="Send Mail" /&gt;

&lt;?= form_close(); ?&gt;



</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 --
&lt;?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');


}
}

?&gt;
---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
#8

[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:
&lt;?php if ($this->validation->error_string()) {
echo $this->validation->error_string;
} ?&gt;

Should just be:
Code:
&lt;?php echo $this->validation->error_string; ?&gt;
#9

[eluser]LukeZach[/eluser]
Thanks James,

still cant get the bloody thing working!! Another problem has cropped up now!!

Thanks for your help mate

L
#10

[eluser]James Gifford[/eluser]
Also, I just noticed your opening form tag doesn't seem to be echoing anything

Code:
&lt;?php form_open('contact'); ?&gt;

Try adding an echo:

Code:
&lt;?php echo form_open('contact'); ?&gt;

As Kromack said, you are mixing the short and long php tags you might want to pick one type and stick with it.




Theme © iAndrew 2016 - Forum software by © MyBB