0

I'm having a perplexing problem--I'm managing to get one value, extracted from a text box, successfully inserted into my table but the other (also from a text box) is not going in. Before the AJAX call, I've alerted my datastring to make sure both values are correct, and they are. When I look in the database, however, only the name value is entered and email is blank.

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;
}
4

1 回答 1

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();
    });
于 2013-09-21T13:49:25.090 回答