简体   繁体   中英

Post Back response from PHP to javascript

I'm new to forms and post data ... so I don't know how solve this problem!

I've a php page (page1) with a simple form:

<form method="post" action="/page2.php">
<input type="search" value="E-Mail Address" size="30" name="email" />
<input type="submit" value="Find E-Mail" />
</form>

How you can notice ... this form post the 'email' value to the page2. In the page2 there is a small script that lookup in a database to check if the email address exist.

$email = $_POST['email'];

$resut = mysql_query("SELECT * FROM table WHERE email = $email");
.   
.
.
/* do something */
.
.
.

if($result){
    //post back yes
}
else{
   //post back no
}

I don't know how make the post back in php! And how can I do to the post back data are read from a javascript method that shows an alert reporting the result of the search?

This is only an example of what I'm trying to do, because my page2 make some other actions before the post back.

When I click on the submit button, I'm trying to animate a spinning indicator ... this is the reason that I need to post back to a javascript method! Because the javascript function should stop the animation and pop up the alert with the result of the search!

Very thanks in advance!

I suggest you read up on AJAX .

Here's a PHP example on W3Schools that details an AJAX hit.

Hi i think you can handle it in two ways.

First one is to submit the form, save the data in your session, check the email, redirect back to your form and display the results and data from session.

Like

session_start();

// store email in session to show it on form after validation
$_SESSION['email'] = $_POST['email'];   

// put your result in your session
if ($results) { 
    $_SESSION['result'] = 'fine';
    header(Location: 'yourform.php'); // redirect to your form
} 

Now put some php code in your form:

<?php 

session_start();

// check if result is fine, if yes do something..
if ($_SESSION['result'] == 'fine) {
    echo 'Email is fine..';
} else {
    echo 'Wrong Email..';
}

?> 

More infos : Sessions & Forms

And in put the email value back in the form field

<input type="search" 
 value="<?php echo $_SESSION['email']; ?>" 
 size="30" 
 name="email" />

Please excuse my english, it is horrible i know ;)

And the other one the ajax thing some answers before mine !

As a sidenote, you definitly should escape your data before using it in an SQL request, to avoid SQL injection

As you are using mysql_* functions, this would be done with one of those :

You would not be able to post in this situation as it is from the server to the client. For more information about POST have a look at this article .

To answer your question you would want to do something like this when you have done your query:

if(mysql_num_rows($result)){ //implies not 0
   $data = mysql_fetch_array($result);
   print_r($data);
}
else{
//no results found
echo "no results were found";
}

The print_r function is simply printing all the results that the query would have returned, you will probably want to format this using some html. $data is just an array which you can print a single element from like this:

echo $data['email'];

I hope this helps!

<?php 

echo " alert('Record Inserted ');" OR echo " document.getElementByID('tagname').innerHtml=$result;" ?>

OR include 'Your Html file name'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM