不能通过ajax在实现onchange()方法时发布数据

huangapple go评论66阅读模式
英文:

Can not post data through ajax when implements onchage() method

问题

我尝试使用ajax和onchange方法发布数据,但代码没有发布数据。

<input type="number" name="number" id="number" value="<?php echo $row['quantity']; ?>" min="0" max="10" step="1" onchange="updateQ(<?php echo $row['quantity']; ?>, <?php echo $_SESSION['user_id']; ?>, '<?php echo $row['date']; ?>', '<?php echo $row['date']; ?>');">

这是脚本...

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
if (window.history.replaceState) {
    window.history.replaceState(null, null, window.location.href);
}
function updateQ(quantity, cid, date, time){
    $.ajax({
        type: "POST",
        url: "cartqtyupd.php",
        data: {  
            'qty': quantity,
            'cid': cid,
            'date': date,
            'time': time
        },
        success: function (response) {
            alert("success");
            // window.location.href = "b.php";
            // next_page_url_here?key3=value3&amp;key4=value4
        }
    });
}
</script>

请你给予一些帮助来解决这个问题。我尝试了很多次,但找不到解决方案。我阅读了很多stackoverflow的问题和答案,但是我无法弄清楚该怎么办。

英文:

I try to post data using ajax with onchange method but the code doen't post data.

&lt;input type=&quot;number&quot; name=&quot;number&quot; id=&quot;number&quot; value=&quot;&lt;?php echo $row[&#39;quantity&#39;];?&gt;&quot; min=&quot;0&quot; max=&quot;10&quot; step=&quot;1&quot; onchange=&quot;updateQ(&lt;?php echo $row[&#39;quantity&#39;];?&gt;,&lt;?php echo $_SESSION[&#39;user_id&#39;];?&gt;,&lt;?php echo $row[&#39;date&#39;]; ?&gt;,&lt;?php echo $row[&#39;date&#39;]; ?&gt;);&quot;&gt;

Here is the script..

&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;
        &lt;script&gt;
        if ( window.history.replaceState ) {
            window.history.replaceState( null, null, window.location.href );
        }
        function updateQ(quantity,cid,date,time){
           
          
        $.ajax({
        type:&quot;POST&quot;,
        url:&quot;cartqtyupd.php&quot;,
        data: 
        {  
           &#39;qty&#39; :quantity,
           &#39;cid&#39; :cid,
           &#39;date&#39; :date,
           &#39;time&#39; :time
        },
        success: function (response) 
        {
            alert(&quot;success&quot;);
         
           //window.location.href = &quot;b.php&quot;;
           //next_page_url_here?key3=value3&amp;key4=value4

        
        }
    });
    }
&lt;/script&gt;

Please can you give some assitant to solve this problem. I tried so much times but couldnt get a solution. I read lot of stakoverflow problems and answers but I couldnt figure out what to do.

答案1

得分: 2

你传递给updateQ()函数的参数值格式不正确(字符串值缺少引号)。这将导致JavaScript函数执行时出现语法错误。

尝试这样做:

<input type="number" name="number" id="number" value="<?php echo $row['quantity'];?>" min="0" max="10" step="1" onchange="updateQ(this.value, <?php echo $_SESSION['user_id'];?>, '<?php echo $row['date'];?>', '<?php echo $row['time'];?>');">
英文:

The values that you are passing as parameters to the updateQ() function are not properly formatted (missing quotes around string values). This will result in a syntax error when the JavaScript function tries to execute.

try this:

&lt;input type=&quot;number&quot; name=&quot;number&quot; id=&quot;number&quot; value=&quot;&lt;?php echo $row[&#39;quantity&#39;];?&gt;&quot; min=&quot;0&quot; max=&quot;10&quot; step=&quot;1&quot; onchange=&quot;updateQ(this.value, &lt;?php echo $_SESSION[&#39;user_id&#39;];?&gt;, &#39;&lt;?php echo $row[&#39;date&#39;]; ?&gt;&#39;, &#39;&lt;?php echo $row[&#39;time&#39;]; ?&gt;&#39;);&quot;&gt;

huangapple
  • 本文由 发表于 2023年3月9日 13:05:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75680622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定