Ajax脚本不保存电子邮件输入到数据库

时间:2014-02-28 10:10:28

标签: php jquery mysql ajax foreach

下面的脚本使用产品名称和ID存储用户电子邮件,但是电子邮件未传递/存储到数据库。由于它是在一个循环中,我通过将产品ID添加到每个实例来使输入/模态ID唯一。

该代码适用于各个产品页面。

HTML:

    <?php foreach($searchresults["products"] as $product): ?>
    <?php
    $pidForSubscribe = $product['id'];
    $priceForSubscribe = $product['price'];
    ?>

    <!-- modal button -->
    <button class="btn btn-lg btn-subscribe" data-toggle="modal" data-target="#subscribe-<?php print $product['id'];?>">


    <!-- Modal -->
<div class="modal fade" id="subscribe-<?php print $product['id'];?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel"><?php print $product["name"]; ?></h4>
      </div>
      <div class="modal-body row">

      <?php require("subscribe.php") ?>

        </div>

      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

      </div>
    </div>
  </div>
</div>
<?php endforeach; ?>

Subscribe.php

<script type="text/javascript">
function subscribeEmail(pid,price)
{
    var email = document.getElementById('subemail-<?php print $product['id'];?>').value;

    if (this.email == '')
    {
        alert('Please enter email');
        return false;
    }
    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            //document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
            alert(xmlhttp.responseText);
        }
      }
      var url = "html/subscribeAjax.php?pid="+pid+"&price="+price+"&email="+email;
    xmlhttp.open("GET",url,true);
    xmlhttp.send();
}


</script>

<div class="row subscribe">
    <form name="form2" id="form2">
        <div class="col-md-10 col-sm-10 col-xs-10 nopadding">
            <input type="text" name="subemail" id="subemail-<?php print $product['id'];?>" class="form-control" />
        </div>

        <div class="col-md-2 col-sm-2 col-xs-12 nopadding">
            <button name="subbtn" id="subbtn" class="btn btn-primary" onclick="subscribeEmail(<?php echo $pidForSubscribe;?>,<?php echo $priceForSubscribe;?>)" type="button">Subscribe</button>
        </div>
    </form>
</div>

subscribeAjax.php

<?php
require("../includes/common.php");


$pid = $_GET['pid'];
$email = $_GET['email'];
$price = $_GET['price'];

$squery = "SELECT * FROM `".$config_databaseTablePrefix."updateprice` WHERE `product_id` =".$pid." AND `email` = '".$email."'";

$numRows = database_querySelect($squery,$rows);

if($numRows)
{
    echo "You are already subscribed!!";
}
else
{
    mysql_query("INSERT INTO `".$config_databaseTablePrefix."updateprice` (`product_id`,`price`,`email`) VALUES ($pid,'$price','$email')") or die(mysql_error());
    $insertedId = mysql_insert_id();

    if($insertedId)
    {
        echo "You are successfully subscribed!!";
    }

    mysql_query("UPDATE `".$config_databaseTablePrefix."updateprice` SET `price` = '$price' WHERE `product_id` = ".$pid) or die(mysql_error());
}

?>

1 个答案:

答案 0 :(得分:0)

你需要像这样更改你的ajax代码。 XMLHttpRequest Object必须在ajax函数之外创建

<script type="text/javascript">
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

function subscribeEmail(pid,price)
{
    var email = document.getElementById('subemail-<?php print $product['id'];?>').value;

    if (this.email == '')
    {
        alert('Please enter email');
        return false;
    }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            //document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
            alert(xmlhttp.responseText);
        }
      }
      var url = "html/subscribeAjax.php?pid="+pid+"&price="+price+"&email="+email;
    xmlhttp.open("GET",url,true);
    xmlhttp.send();
}
</script>