views:

1253

answers:

4

Dear developer,

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!

[SOLVED]

In this case the AJAX solution is the best way! Thanks!

+4  A: 

I suggest you read up on AJAX.

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

Cerebrus
Thanks for your reply! But I can't fetch my database directly from with java! I need to post to page2 (php) and than post back to page1 where should be a javascript that receive the post back!
BitDrink
@BitDrink: As the tutorials should illustrate, you can send an asynchronous hit from page1.php to page2.php which returns the result of your email validation and you can display that result on page1.php without (appearing to) post back or moving away from page1.php.
Cerebrus
... that is what AJAX is all about! :-)
Cerebrus
A: 

Hi,

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!

hookd
+1  A: 

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 :

Pascal MARTIN
Thanks for your right!
BitDrink
You're welcome :-)
Pascal MARTIN
+1  A: 

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 !

ArneRie
thanks! Your solution is fine but! There is no way to send the result to the search to a script java? Because, I'm also trying to animate a spinning indicator when the serch start and I would like to stop it when the post back data arrive!
BitDrink