Trouble fixing parse error, unexpected T_IF error – PHP

  php

Q(Question):

Howdy –
In setting up a registration form, I’d like to keep any correct entries on the form (make them sticky).

The Registration Form is "included" and has the the following structure:


<body>
<?php
echo " <p>
<form action=$_SERVER[PHP_SELF] method='POST'>
<table width='95%' border='0' cellspacing='0' cellpadding='2'>
<tr><td></td><td>Please fill in all information.</td></tr>
<tr><td align='right'><b>First Name:</b></td>
<td><input type='text' name='FirstName' size='35'
maxlength='35' value="if (isset($_POST['FirstName']))
{echo $_POST['FirstName']}; "></td></tr>
</table>
<input type='hidden' name='submitted' value='yes'>
</form>
</p>
?>
</body>

This generates the following error message:
parse error, unexpected T_IF, expecting ‘,’ or ‘;’ in /html/RegistrationForm1.php on line 12

How does one punctuate the "if (isset…) to provide the indicated ", or ;" the compiler is looking for?
Would appreciate any help or suggestions.

Thanks

A(Answer):

you missed the ; in the execution block (line 34) and put it outside the brackets (just swap } and 😉
[PHP]if (…)
{
echo $var; // semicolon here
} // not here[/PHP]
note: a proper indentation style may increase readability a lot. you may consider to separate markup and processing by first evaluating all expressions and insert the results in your markup.

regards

A(Answer):

Ciaroscuro,

your code will be better formated and easier for our experts to read if you put [code]…[/code] tags round it like I have done for you now.

Please read our posting guidelines

Banfa
Administrator

A(Answer):

Dormilich & Banfa –

Thank you for your responses. I tried changing the position of the semi-colon but that didn’t change the error message. I ended up converting from a PHP echo form to a staight HTML form with <?php …?> phrases for the

<form action="<?php $_SERVER[PHP_SELF];?>" method="POST">

and

<tr><td align='right'><b>First Name:</b></td>
<td><input type='text' name='FirstName' size='35'
maxlength='35' value="<?php if(isset($_POST['FirstName']))
echo $_POST['FirstName'];?>"/></td></tr>

to get the sticky Value attribute to work.

Still haven’t figured out why the <?php echo format didn’t work, but this other approach does, so I’m making forward progress again. Thanks.

A(Answer):

Dormilich & Banfa –

Thank you for your responses. I tried changing the position of the semi-colon but that didn’t change the error message. I ended up converting from a PHP echo form to a staight HTML form with <?php …?> phrases for the

<form action="<?php $_SERVER[PHP_SELF];?>" method="POST">

and

<tr><td align='right'><b>First Name:</b></td>
<td><input type='text' name='FirstName' size='35'
maxlength='35' value="<?php if(isset($_POST['FirstName']))
echo $_POST['FirstName'];?>"/></td></tr>

to get the sticky Value attribute to work.

Still haven’t figured out why the <?php echo format didn’t work, but this other approach does, so I’m making forward progress again. Thanks.

Ciaroscuro, please pay attention to post No.3 – it makes your post a lot easier to read for our experts if you use code tags.

Moderator.

A(Answer):

I tried changing the position of the semi-colon but that didn’t change the error message.

checked your code snippet (with subethaedit using php) and I didn’t get a parser error. judging from that, the code should be running fine.

regards

note: could verify the error message for the original code (just if you wonder…)

A(Answer):

You can’t have an IF statement inside and echo statement. Won’t work. Create a function that does your IF checks and then RETURNs a value.

value=" . some_function() . "

LEAVE A COMMENT