无法将一个项目导入MySQL数据库

时间:2013-09-21 13:44:59

标签: php jquery ajax

我遇到了一个令人困惑的问题 - 我正在设法获取一个值,从文本框中提取,成功插入到我的表中,但另一个(也来自文本框)不会进入。在AJAX之前调用,我已经提醒我的数据字符串,以确保两个值都正确,它们是。但是,当我查看数据库时,只输入名称值并且电子邮件为空。

HTML:

<div id="basic-modal-content">
    <h3>Please Alert me when this is Available</h3>


    <p>Your Name: <input type="text" id="alert_name"/></p>
    <p>Email Address: <input type="text" id="alert_email"/></p>
    <button id="alert_submit">Submit</button>
</div>

使用Javascript:

$('#alert_submit').click(function(){
    var datastring = 'name='+ $('#alert_name').val() + '&email=' + $('#alert_email').val();
    alert(datastring);
        $.ajax({  
          type: "POST",  
          url: "process_email.php",  
          data: datastring,  
          success: function(data) {  

          }  
        });  
      alert ("We've received your request and will alert you once the directory is available. Thank you.");
      $.modal.close();
});

PHP:

$name = $_POST['name'];
$email = $_POST['email'];
try {

        $conn = new PDO('mysql:host=blah;dbname=blah', '-', '-');
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  

        $stmt = $conn->prepare("INSERT INTO email_alerts(name, email) VALUES(':name, :email)");
        $stmt->execute(array('name' => $name, 'email' => $email));
        //$affected_rows = $stmt->rowCount();
        $conn = null;
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
    $conn = null;
}

1 个答案:

答案 0 :(得分:0)

请尝试此代码

$('#alert_submit').click(function(){

            $.ajax({  
              type: "POST",  
              url: "process_email.php?name="+$('#alert_name').val() +"&email="+ $('#alert_email').val(),  

              success: function(data) {  

              }  
            });  
          alert ("We've received your request and will alert you once the directory is available. Thank you.");
          $.modal.close();
    });