Jump to content

can't get html to hide through PHP


anevins

Recommended Posts

I'm working on a contact form and I want the instructions to hide from view once the user submits successfully.

I've currently implemented this through the form; as the form disappears once submitted but am having trouble using the instruction html elements.

 

The variable $form_message holds the html elements I want to hide on successful submit:

<?php
$form_message = true;

if ($form_message){
?>
		<h1> Need to contact me? </h1>
		<p> Please fill out your details and press the submit button to email me. <span class="bold">All fields are required</span>, thank you.</p><br />

<?php
}
?>

 

This is the entire document:

		<div id="form">
<?php
$form_message = true;

if ($form_message){
?>

		<h1> Need to contact me? </h1>
		<p> Please fill out your details and press the submit button to email me. <span class="bold">All fields are required</span>, thank you.</p><br />


<?php
}
include_once('php/functions.php');

$firstname = $_POST['first_name'];
$lastname = $_POST['last_name'];
$email = $_POST['email'];
$reason = $_POST['reason'];
$form = true;



if(isset($_POST['submit'])){

if (empty($firstname)){
	echo '	

	<script>		
		$("span.fn_error").slideDown("slow", function());
	</script>

	';
	echo '<span class="error">Please enter your First name.<br /></span>';
}

if (empty($lastname)){
	echo '<span class="error">Please enter your Last name.<br /></span>';
}

if (check_email_address($email)==false){
	echo '<span class="error">Please enter a valid email address.<br /></span>';
}

if (empty($reason)){
	echo '<span class="error">Please enter your reason to contact me.<br /></span>';
}

if (!empty($firstname) && !empty($lastname) && (check_email_address($email)) && !empty($reason)){
	$form = false;
	$form_message = false;
	echo '<p><span class="green">Thank you </span>'. $firstname .' '. $lastname . ' for taking the time to contact me.<br />I will try to get
	back to you through your email <span class="bold">'.$email.' </span>as soon as possible.</p><br />';
	echo '<p>In the mean time, feel free to look at my Flickr and Blogger feeds.</p><br />';
}

}

if ($form){
?>



	<form id="contact" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
		<table>
			<tr><td>First name:</td><td><input type="text" name="first_name" value="<?php echo $firstname; ?>"/></td></tr>
			<tr><td></td><td><?php echo $firstname_error; ?></td></tr>
			<tr><td>Last name: </td><td><input type="text" name="last_name" value="<?php echo $lastname; ?>"/></td></tr>
			<tr><td></td><td><?php echo $lastname_error; ?></td></tr>
			<tr><td>Email Address: </td><td><input type="text" name="email" value="<?php echo $email; ?>"/></td></tr>
			<tr><td></td><td><?php echo $email_error; ?></td></tr>
			<tr><td>Reason: </td><td><textarea rows="5" cols="16" name="reason" ><?php echo $reason; ?></textarea></td></tr>
			<tr><td></td><td><?php echo $reason_error; ?></td></tr>
			<tr><td> </td><td class="submit"><input type="submit" name="submit" id="submit" value="submit" /></td></tr>
		</table>
	</form>
</div>
<?php
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/238963-cant-get-html-to-hide-through-php/
Share on other sites

I thought that was what I was doing:

	if (!empty($firstname) && !empty($lastname) && (check_email_address($email)) && !empty($reason)){
	$form = false;
	$form_message = false;
}

 

$form = false; works and this is the same technique I'm trying to do with $form_message.

I thought that was what I was doing:

	if (!empty($firstname) && !empty($lastname) && (check_email_address($email)) && !empty($reason)){
	$form = false;
	$form_message = false;
}

 

$form = false; works and this is the same technique I'm trying to do with $form_message.

 

Remember, PHP scripts are executed from the top down.  Having:

 

$form_message = true;

if ($form_message)
{
   ?>
      <!-- html -->
   <?php
}

 

Will simply spit out the HTML where it is.

 

Why does it work for $form?  It works because you set $form as false before you check for it and try to output the form.  Again, top-down execution.

I would recommend that you don't use of $_SERVER['PHP_SELF'] in the form's action attribute. For more information on the security risks behind $_SERVER['PHP_SELF'], check out the following article:

http://www.mc2design.com/blog/php_self-safe-alternatives

 

 

Instead, you could use the actual page name or leave the action attribute blank.

Thank you so much, I have solved this problem from your help Nightslyer.

 

All I did was put the part I wanted to hide in with the other part of the form, that works.

 

...
if ($form){
?>
<!-- This is the bit I just moved inside the if($form), which originally just held the form but now the instructions.-->
		<h1> Need to contact me? </h1>
		<p> Please fill out your details and press the submit button to email me. <span class="bold">All fields are required</span>, thank you.</p><br />

	<form id="contact" method="post" action="contact.php">
		<table>
...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.