dounan9070 2013-08-06 02:16
浏览 60
已采纳

如何从jquery .post访问$ _POST变量?

I am trying to using jquery post to try to send form data asynchronously to a php file that will upload the data to my database.

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
  <div id="contact_form">  
    <form id="contact" name="contact" method="post">  
  <fieldset>  
    <label for="name" id="name_label">Name</label>  
    <input type="text" name="name" id="name" size="30" value="" class="text-input" />  

    <label for="email" id="email_label">Return Email</label>  
    <input type="text" name="email" id="email" size="30" value="" class="text-input" />  

    <label for="phone" id="phone_label">Return Phone</label>  
    <input type="text" name="phone" id="phone" size="30" value="" class="text-input" />  

    <br />  
    <input type="submit" name="submit" class="button" id="submit_btn" value="Submit" />  
  </fieldset>  
</form>  
</div>  
  <div id="result"></div>

<script>
$(document).ready(function(){
/* attach a submit handler to the form */
$("#submit_btn").submit(function(event) {

  /* stop form from submitting normally */
  event.preventDefault();

 $.post("process.php", $("#contact").serialize());


  return false;
});
});
</script>

</body>
</html> 

I know it is serial encoding the data but my php script isn't picking it up, I am not sure what I am doing wrong. I know it is simple, but I have been working a few days on this and can't seem to figure it out. I understand how to do this without jquery but I really need it in this case. Here is my php:

if(isset($_POST['submit']))
{
    $var_name = $_POST['name'];
    $var_email = $_POST['email'];
    $var_phone = $_POST['phone'];

//This establishes a connection to the database. It also says what to do if the connection fails.
try {
    $db_handle = new PDO("mysql:host=$server;dbname=$database", $db_username, $db_password);
    $db_handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $db_handle->prepare('INSERT INTO contact(name, email, phone) VALUES(:name, :email, :phone)');   
    $stmt->execute(array(
        ':name' => $var_name,
        ':email' => $var_email,
        ':phone' => $var_phone,
        ));
    } catch(PDOException $e) {
    echo 'Error: ' . $e->getMessage();
    }
}
  • 写回答

3条回答 默认 最新

  • doucheng3811 2013-08-06 03:25
    关注

    When you serialize a form with jQuery, submit button is not included so you should replace

    if(isset($_POST['submit']))
    

    by

    if ($_SERVER['REQUEST_METHOD'] === 'POST')
    

    EDIT

    Change $("#submit_btn").submit(...); by $("#contact").submit(...);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计