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
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.

Link to comment
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.

 

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.

Link to comment
Share on other sites

Nightslyer,

 

I don't know how to circumvent this problem you pointed out.

Could you please give me some advice to how to hide that html on successful form submit?

 

Your help so far has been appreciated.

Link to comment
Share on other sites

The best solution would be to have all your form processing within the following conditional:

 

if (isset($_POST['submit'))
{
   $form_message = false;

   // more form processing
}

if (isset($form_message) && $form_message == true)
{
   // display HTML
}

Link to comment
Share on other sites

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>
...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.