AJAX不会POST到PHP(非jQuery)

时间:2014-03-21 14:39:42

标签: php ajax function post

我已经搜索并搜索了所有我发现的jQuery问题和解决方法。我有一个脚本设置工作正常,但这个不会发布3个值到PHP脚本。我甚至试图强制将3个设置值放入我正在放置时间戳的位置,但这不起作用。我有点失落。

此脚本生成不同的字段/输入以运行updateCoupon()

        function updateForm(formNum,id){
            var str = 'edit-contents'+id;
            switch( formNum )
            {
                case 1:
                    document.getElementById(str).innerHTML = "<div class='title-input'>Promo Title:<br><input type='text' size='25' maxlength='100' id='ptitle' name='ptitle'><input name='Edit' type='button' class='EditCoup' id='EditCoup' value='Edit' onClick='javascript:updateCoupon("+id+",1);'></div>";
                    break;

                case 2:
                    document.getElementById(str).innerHTML = "<div class='description-input'>Promo Description:<br><textarea maxlength='1256' cols='40' rows='5' id='pdescription' placeholder='Use <br> to insert a line break' name='pdescription'></textarea><input name='Edit' type='button' class='EditCoup' id='EditCoup'  value='Edit' onClick='javascript:updateCoupon("+id+",2);'></div>";
                    break;

                case 3:
                    document.getElementById(str).innerHTML = "<div class='Disclaimer-input'>Promo Disclaimer:<br><textarea maxlength='1256' cols='40' rows='5' id='pdisclaimer' placeholder='Use <br> to insert a line break' name='pdisclaimer'></textarea><input name='Edit' type='button' class='EditCoup' id='EditCoup' value='Edit' onClick='javascript:updateCoupon("+id+",3);'></div>";
                    break;

                case 4:
                    document.getElementById(str).innerHTML = "<div class='expDate'>Promo Experation:<br><input type='text' size='25' maxlength='100' id='datepicker'  placeholder='DD-MM-YYYY' name='datepicker'><input name='Edit' type='button' class='EditCoup' id='EditCoup' value='Edit' onClick='javascript:updateCoupon("+id+",4);'></div>";
                    break;

                case 5:
                    document.getElementById(str).innerHTML = "<div class='image-input'>Select a Promo Image:<br><select id='picture' name='picture'><option value='' Selected>Select Image...</option><option value='http://cws.dealerconnection.com/images/MAX.png'>Battery MAX</option><option value='http://cws.dealerconnection.com/images/PLUS.png'>Battery MAX</option><option value='http://cws.dealerconnection.com/images/brake.gif'>Brakes</option><option value='http://cws.dealerconnection.com/images/checkup.png'>Check List</option><option value='http://cws.dealerconnection.com/images/CreditCards.gif'>Ford Credit Card</option><option value='http://cws.dealerconnection.com/images/lptg.png'>Low Price Tire Gaurentee</option><option value='http://cws.dealerconnection.com/images/michelin.gif'>Michelin</option><option value='http://cws.dealerconnection.com/images/works.png'>The Works</option><option value='http://cws.dealerconnection.com/images/4tires.gif'>Tires</option><option value='http://cws.dealerconnection.com/images/TR99.png'>Tire Brand Logos</option><option value='http://cws.dealerconnection.com/images/wipers.png'>Wiper Blades</option><option value='http://cws.dealerconnection.com/images/Fpseal.png'>Warranty Ford Parts</option><option value='http://cws.dealerconnection.com/images/Mcseal.png'>Warranty Motocraft</option></select><input name='Edit' type='button' class='EditCoup' id='EditCoup' value='Edit' onClick='javascript:updateCoupon("+id+",5';'></div>";
                    break;

                default:
                    alert("Error:"+formNum);
                break;
            }
        }
        </script>

AJAX:

        var xmlhttp = '';

        function createXMLHttpRequest() {
          if (window.ActiveXObject) { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } 
          else if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); }
        }

        function updateCoupon(id,field)
        {

            var tempId = id;

            switch( field )
            {
                case '1':
                    var updateField = "headline";
                    var title = form.ptitle.value;
                    var queryStr = "id=" + tempId + "&change=" + title + "&field=" +updateField;
                    break;

                case '2':
                    var updateField = "subhead";
                    var desc = form.pdescription.value;
                    var queryStr = "id=" + tempId + "&change=" + desc + "&field=" +updateField;
                    break;

                case '3':
                    var updateField = "disclaimer";
                    var disc = form.pdisclaimer.value;
                    var queryStr = "id=" + tempId + "&change=" + disc + "&field=" +updateField;
                    break;

                case '4':
                    var updateField = "couponexpires";
                    var date = form.datepicker.value;
                    var queryStr = "id=" + tempId + "&change=" + date + "&field=" +updateField;
                    break;

                case '5':           
                    var updateField = "graphics";
                    var pic = form.picture.value;
                    var queryStr = "id=" + tempId + "&change=" + pic + "&field=" +updateField;
                    break;
            }

                createXMLHttpRequest();

              var url = "/coupon_testing/Ford_coupon_tool/php/couponEdit.php?timeStamp=" + new Date().getTime();
              xmlhttp.open("POST", url, true);
              xmlhttp.onreadystatechange = function () {
                    if(xmlhttp.readyState == 4) {
                    if(xmlhttp.status == 200) {
                        var jResponse = xmlhttp.responseText;
                        if(jResponse != '')  {
                            alert(jResponse);
                        }
                    }
                  } 
              };

              xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");   
              xmlhttp.send(queryStr);
        }

PHP:

        <?php
            $ret = "Error";
            $id = $_POST['id'];
            $change = trim($_POST['change']);
            $field = trim($_POST['field']);

            include 'configuration.inc.php';
            include 'connection.inc.php';
            include 'db_functions.inc.php';
        //set variables from the coupon setup form

            if( $id != '' && $change != '' && $field != '' )
            {
                //Saving the coupon to the database.
                $sql = "UPDATE ford_coupons SET " . $field . "='" . addslashes($change)."' WHERE id='".addslashes($id)."' LIMIT 1";
                execute_query($sql);
                $ret = $field." has been updated!";
            }
            // print $ret;
            $str = "id=".$id.", change=".$change.", field=".$field;
            print $str;
        ?>

谁能告诉我这里的问题是什么?一直在绞尽脑汁和另一位程序员朋友,我们都不知道为什么不发布。感谢

1 个答案:

答案 0 :(得分:0)

您正在使用数字值调用updateCoupon(id,field),例如

javascript:updateCoupon("+id+",1);

但是在你的switch语句中,你只是检查字符串值,比如

case '1':

您需要使用&#39;字段的字符串调用函数&#39;参数,或改变你的案例使用整数。

在执行期间,没有任何switch语句会被命中,因此空的AJAX请求......