英文:
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&key4=value4
}
});
}
</script>
请你给予一些帮助来解决这个问题。我尝试了很多次,但找不到解决方案。我阅读了很多stackoverflow的问题和答案,但是我无法弄清楚该怎么办。
英文:
I try to post data using ajax with onchange method but the code doen't post data.
<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']; ?>);">
Here is the script..
<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&key4=value4
}
});
}
</script>
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:
<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']; ?>');">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论