0

I have the following code:

<?php
... 
?>

<script>
....
</script>

<html>
 ...
</html>

After displaying HTML form, JavaScript should validate and then PHP should save in database and give a confirmation message.. but what happens is, after PHP is executed and success message is echoed, the HTML form also displays as it is below message.. What can I do to avoid this?

JD3
  • 410
  • 1
  • 5
  • 14
Sanjay Malhotra
  • 73
  • 1
  • 4
  • 12
  • Only show the html when it's needed. – Femaref Jul 03 '13 at 17:21
  • 2
    I'm assuming that you're posting a form page back to the page it exists on. If so, just wrap your HTML in a PHP condition to check for the existence of a POST (or GET) variable. – j08691 Jul 03 '13 at 17:21
  • Use an if clause to display the HTML form only if no form data was sent to the page. – Shomz Jul 03 '13 at 17:21

6 Answers6

1

I prefer to set a variable if a form submission was successful. Something like the following:

<?php
$success = false;

if (isset($_POST['submit'])) {
    // process form submission
    // if submission validates; set $success to true
}
?>
<!DOCTYPE html>
<html>
  <head>
    …
  </head>
  <body>
<?php if ($success): ?>
    <p>Thank you for your submission!</p>
<?php else: ?>
    <form action="" method="post">
      …
    </form>
<?php endif; ?>
  </body>
</html>
Martin Bean
  • 38,379
  • 25
  • 128
  • 201
0

You could end the script after progressing the data with exit(); (See docs)

Like @Shomz said, you could wrap your HTML output in a if-statement to prevent it from being printed after processing your form data.

Jeff Noel
  • 7,500
  • 4
  • 40
  • 66
cybrox
  • 250
  • 4
  • 17
  • Yep, use exit(), everyone loves half-rendered pages – Damien Pirsy Jul 03 '13 at 17:27
  • @DamienPirsy http://www.cplusplus.com/reference/cstdlib/exit/ exit (or more appropriate, the concept behind it) has been around far longer than php is perfectly valid to use. Of course, you can abuse nearly every function, but exit() itself doesn't have ANYTHING to do with half-rendered pages as you call them. – griffin Jul 03 '13 at 17:36
0
  • put PHP in a different file (you can include HTML files)

  • return;

  • die();

  • exit();

  • Use ob_start() and functions alike to buffer HTML and _clean to flush it to nirvana when you don't need it.

  • Submit to a different file (nearly the same as first point).

  • Use prepend in php.ini to start ob before script, and append in php.ini to kill output under certain circumstances (maybe using isset() on a variable to check if there has been a submit or just $_POST / $_GET ).

You might also want to look at PRG pattern which someone else has already asked about for php here: Simple Post-Redirect-Get code example

Community
  • 1
  • 1
griffin
  • 1,261
  • 8
  • 24
  • "If you need more options, there's probably tons of them." In what way does that help. –  Jul 03 '13 at 17:24
  • edited and changed, though I don't really get why you downvoted an answer which provides multiple solutions as well as stating that there are more (I could go out and list them all, but it won't help the OP) – griffin Jul 03 '13 at 17:25
  • i don't want in another file. also i tried die() n exit() at end of php. but den nothing loads, not even html. – Sanjay Malhotra Jul 03 '13 at 17:28
  • Well your question was about how to not output the html ; if you only want to not output the form:
    ...
    – griffin Jul 03 '13 at 17:28
  • That's because both die() and exit() end the execution of the script, and therefore any subsequent parsing/elaboration. Don't know why people keep suggesting here to use those – Damien Pirsy Jul 03 '13 at 17:29
0
if(isset($_POST['name'])){

//your insert query 
//your thanks message

}
else{
// Your html form
}
Moeed Farooqui
  • 3,604
  • 1
  • 18
  • 23
0

Simply use a flag variable :-

if(isset($some_var))
{

// do something 

}
else{
//  show html form
}
Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49
  • You should add how to set the "flag variable" by something he submits ($_GET/$_POST in most cases) – griffin Jul 03 '13 at 17:28
0

You can use the 'action' of your form to send the post to another file where there is the confirmation message.

Or you cant put a condition if(!$_POST) before your html

Susana Santos
  • 312
  • 1
  • 6
  • 18