回复从PHP到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!

5个回答



我建议你阅读 AJAX 。</ p>

这是一个PHP示例 W3Schools详细介绍了AJAX。</ p>
</ div>

展开原文

原文

I suggest you read up on AJAX.

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

douyanning3724
douyanning3724 ......这就是AJAX的全部意义! :-)
大约 11 年之前 回复
douche3244
douche3244 正如教程应该说明的那样,您可以从page1.php向page2.php发送异步命中,返回您的电子邮件验证结果,您可以在page1.php上显示该结果,而不会(显示)回发或离开page1 .PHP。
大约 11 年之前 回复
douzhuan4406
douzhuan4406 感谢您的回复! 但我无法直接从java获取我的数据库! 我需要发布到page2(php),然后回发到page1,其中应该是一个接收回帖的javascript!
大约 11 年之前 回复

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 !

dongshuo6185
dongshuo6185 谢谢! 你的解决方案很好但是! 有没有办法将结果发送到脚本java的搜索? 因为,我也试图在serch开始时设置旋转指示器的动画,我想在回发数据到达时停止它!
大约 11 年之前 回复



作为旁注,你绝对应该在SQL请求中使用它之前转义数据,以避免 SQL注入 </ p>

当您使用 mysql _ * </ code>函数时, 这可以通过其中一个来完成:</ p>

展开原文

原文

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 :



您无法在这种情况下发布,因为它是从服务器发送到客户端。 有关POST的更多信息,请查看这篇文章。</ p> \ n

要回答你的问题,你可以在完成查询后做这样的事情:</ p>

  if(mysql_num_rows($ result)){// 暗示不是0 
$ data = mysql_fetch_array($ result);
print_r($ data);
}
else {
//未找到结果
echo“未找到结果”;
}
< / code> </ pre>

print_r函数只是打印查询将返回的所有结果,您可能希望使用某些html格式化它。 $ data只是一个可以打印单个元素的数组,如下所示:</ p>

  echo $ data ['email']; 
</ code> </ pre>

我希望这会有所帮助!</ p>
</ div>

展开原文

原文

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'

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问