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

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

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

问题

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

  1. <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']; ?>');">

这是脚本...

  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  2. <script>
  3. if (window.history.replaceState) {
  4. window.history.replaceState(null, null, window.location.href);
  5. }
  6. function updateQ(quantity, cid, date, time){
  7. $.ajax({
  8. type: "POST",
  9. url: "cartqtyupd.php",
  10. data: {
  11. 'qty': quantity,
  12. 'cid': cid,
  13. 'date': date,
  14. 'time': time
  15. },
  16. success: function (response) {
  17. alert("success");
  18. // window.location.href = "b.php";
  19. // next_page_url_here?key3=value3&amp;key4=value4
  20. }
  21. });
  22. }
  23. </script>

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

英文:

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

  1. &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..

  1. &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;
  2. &lt;script&gt;
  3. if ( window.history.replaceState ) {
  4. window.history.replaceState( null, null, window.location.href );
  5. }
  6. function updateQ(quantity,cid,date,time){
  7. $.ajax({
  8. type:&quot;POST&quot;,
  9. url:&quot;cartqtyupd.php&quot;,
  10. data:
  11. {
  12. &#39;qty&#39; :quantity,
  13. &#39;cid&#39; :cid,
  14. &#39;date&#39; :date,
  15. &#39;time&#39; :time
  16. },
  17. success: function (response)
  18. {
  19. alert(&quot;success&quot;);
  20. //window.location.href = &quot;b.php&quot;;
  21. //next_page_url_here?key3=value3&amp;key4=value4
  22. }
  23. });
  24. }
  25. &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函数执行时出现语法错误。

尝试这样做:

  1. <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:

  1. &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:

确定