Tasos
Tasos

Reputation: 1642

How to echo on same html page after form submit?

When the form is submitted, my page is redirected to the PHP and the echo is displayed. How can I display the echo without being redirected to the PHP page? So the echo should be displayed in the html page (where the html form is located).

<form method="post" action="../form.php">
<input type="submit" name="1" value="YES" />
<input type="submit" name="1" value="NO" />
</form>

-

<?php 
$answer = $_POST['1'];  
if ($answer == "YES") {          
    echo 'Good!!!';      
}
else {
    echo 'Try Again!';
} 
?>

Upvotes: 3

Views: 33960

Answers (5)

Andrew
Andrew

Reputation: 1

You can set the action to the same page for example:

If I am working on a file called index.php

that will be my form:

<form action="index.php" method="post">
   <input type="text" name="Example" placeholder="Enter Text">

Upvotes: 0

Jagmohan Singh
Jagmohan Singh

Reputation: 1

<html>
<body>
<form method="post" action="">
<input type="text" maxlength="30" name="nm">
<input type="email" maxlength="30"  name="em"><br>
<input type="checkbox" name="chkbox1" value="male">Male
<input type="checkbox" name="chkbox2" value="male">Male
<input type="checkbox" name="chkbox3" value="male">Male
<input type="checkbox" name="chkbox4" value="male">Male
<input type="checkbox" name="chkbox5" value="male">Male
<input type="Submit" name="submit">
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
echo "Welcome :".$_POST['nm']."<br>";
echo "Your email Address is: ".$_POST['em']."<br>";
}
$count=0;
if(isset($_POST['chkbox1']))
$count++;
if(isset($_POST['chkbox2']))
$count++;
if(isset($_POST['chkbox3']))
$count++;
if(isset($_POST['chkbox4']))
$count++;
if(isset($_POST['chkbox5']))
$count++;
echo" Number of checkboxes ticked are:".$count;
?>

Upvotes: 0

Bogdan
Bogdan

Reputation: 863

I guess you can use a combination of <iframe> and javascript to get the results.

<form method="post" action="submit.php" target="myIframe" >
    <input type="submit" name="1" value="YES" />
    <input type="submit" name="1" value="NO" />
</form>

<iframe name="myIframe">
</iframe>

Upvotes: 6

Max Snijders
Max Snijders

Reputation: 374

You can't do it on the same page - at least not without asynchronous client-server communication techniques such as AJAX. Otherwise, you can use the following:

<form action="<?php echo $_SERVER['PHP_SELF']; ? method=....>"

as your form opening tag, then put the PHP processing code at the top of the document, like this:

<?php
  if(isset($_POST['form_field_name'])){
       //process your data here
?>

HTML in case of Form filled in goes here

<?php
}else{
?>

HTML in case of Form not filled in goes here

<?php
  }
?>

HTML in any case goes here

This way you can change the layout of your page depending on whether the form was filled in or not. the $_SERVER['PHP_SELF'] contains the reference to the currently requested page, and therefore is always set to the correct page even if it is renamed. This can save you time when bug-tracking.

Upvotes: 5

Marcus Recck
Marcus Recck

Reputation: 5065

<?php

if(isset($_POST['1'])){
    echo ($_POST['1'] == 'YES') ? 'Good!!!' : 'Try Again!';
}

?>

<!-- your HTML goes here -->

You can combine HTML and PHP on the same page.

Upvotes: 2

Related Questions