输入数据并从jquery事件运行php脚本

输入数据并从jquery事件运行php脚本

问题描述:

I am having a problem getting data from jquery into a php script. I am trying to load a php script (i.e. send an email) with a variable (email address) from jquery event without leaving the original page and going to a confirmation page. Please help!

Here is my jquery code:

<script>
  $("#test").click(function() { 
    var id = 1;
    $("#target").load("javascript_test2.php", id);

  });
</script>
<div id="target">hmmm did it work?</div>
</body>

This is the php I would like to receive and process the code:

<div id="S1">
<?php 
$id = htmlspecialchars($_POST['id']);

for ($i=1; $i<=2; $i++) {
echo $id . 'Hello world ' . $i . '<br>'; 
}

require_once('lawyeralertemail.php');

?>
</div>

Thanks!

我在将数据从jquery转换为php脚本时遇到问题。 我试图从jquery事件加载带有变量(电子邮件地址)的PHP脚本(即发送电子邮件),而不离开原始页面并转到确认页面。 请帮助! p>

这是我的jquery代码: p>

 &lt; script&gt; 
 $(“#test”)。click(  function(){
 var id = 1; 
 $(“#target”)。load(“javascript_test2.php”,id); 
 
}); 
&lt; / script&gt; 
&lt; div id  =“target”&gt; hmmm是否有效?&lt; / div&gt; 
&lt; / body&gt; 
  code>  pre> 
 
 

这是我想要接收和处理的php 代码: p>

 &lt; div id =“S1”&gt; 
&lt;?php 
 $ id = htmlspecialchars($ _ POST ['id']); 
  
for($ i = 1; $ i&lt; = 2; $ i ++){
echo $ id。  '你好,世界 ' 。  $ i。  '&LT峰; br&GT;';  \ N} 
 
require_once( 'lawyeralertemail.php'); 
 
&GT;吗?
&LT; / DIV&GT;!
 代码>  PRE> 
 
 

感谢 P > div>

You can use jQuery ajax to post data into your PHP. Here is a link: http://api.jquery.com/jquery.post/

This will allow you to post data without reloading the page and give your user a better experience.

You can then use $_POST to capture the data inside your PHP.