views:

94

answers:

5

I'm making a form and when a user hits submit, i want it to hide the form and show a section that says "thank you, blah blah"

I'm using PHP/PEAR Mail Factory:

$mailObj =& Mail::factory('mail');

$bool = $mailObj->send($recipient, $headers, $body);

if($bool)
    echo "Thank you for your submission.";
else
    echo "There was a problem processing your request.";

$body = ""; // clear the body
A: 

PHP is server side, you would have to have it output the text 'style="display: none"' when the page reloads. If you are trying to submit using AJAX, then no, there is no way to do that without javascript.

+2  A: 

Not without reloading the page. If you reload the entire page then its trivial to make a php if statement that shows/or not the form depending on factors(was it submited?).

<?php if($submited): ?>
some html
<?php else: ?>
show the form
<?php endif; ?>

Otherwise you have to go with jquery(or some other javascript code).

Iznogood
+1  A: 

You can use a variable conditionally in your situation:

<?php
if($bool)
{
?>
     <div class="success">Email Sent Successfully !!</div>
<?php
}
?>
Sarfraz
+5  A: 

Yes and no.

As per the scenario in your question, when a form is submitted and evaluated server side, it is possible to show or hide parts of the rendered html.

But to the 'like jQuery' part, the answer is no. PHP is a language that is executed server side and requires that a request is made. I.e. a link is clicked or a form is submitted.

For client side evaluation you need to use javascript, flash or similar technologies.

nikc
A: 

I think you're confusing client-side and server-side operations. If you simply want to change the appearance of your form page upon submission, you can indeed handle that entirely on the client side.

If you want to stay on that form page, but change its appearance based on information returned from your server (as in your sample php code), then you'll need to do an asynchronous form submission. You can use jQuery's $.ajax() function or its relatives, then change your page in the callback function based on the success/failure information you return from your server-side script.

Simple example:

$.post(
  'your_php_handler.php', 
  $("#your_form").serialize(), // send all your form fields
  function(data) { // data = what comes back from your php handler script
    $('#thing_you_want_to_change').html(data);
  }
);
Ken Redler