Php 尝试使用ajax表单而不是表单操作(解决)
我想提高我的网站的用户体验。所以我尝试改变动作ajax的形式,并且我已经尝试了一些教程,但是我仍然被卡住了 我正在使用一个php论坛程序/源代码调用Php 尝试使用ajax表单而不是表单操作(解决),php,ajax,ajaxform,Php,Ajax,Ajaxform,我想提高我的网站的用户体验。所以我尝试改变动作ajax的形式,并且我已经尝试了一些教程,但是我仍然被卡住了 我正在使用一个php论坛程序/源代码调用!Discuz,它来自中国。下面是我现在的代码 在html中 <form method="post" id="jnfarm_pop" action="plugin.php?id=cc&do=shop"> <input type="hidden" name="shopsubmit" value="yes"> <!-
!Discuz
,它来自中国。下面是我现在的代码
在html中
<form method="post" id="jnfarm_pop" action="plugin.php?id=cc&do=shop">
<input type="hidden" name="shopsubmit" value="yes">
<!--first item-->
<input type="checkbox" name="jsid[1]" value="1">
<input type="number" style="width:3em;" name="qty[1]">
<!--second item-->
<input type="checkbox" name="jsid[2]" value="1">
<input type="number" style="width:3em;" name="qty[2]">
...continue 50 item
<button type="submit" class="layui-btn layui-btn-fluid" name="submitbutn">submit</button>
</form>
上面的脚本在使用表单操作时工作正常,并重定向到我的输出页面。如果我想改用ajax,我该如何调整下面的源代码
<script type="text/javascript">
function login() {
$.ajax({
type: "POST",
dataType: "json",//? is it can use json? since my form data can get as array
url: "plugin.php?id=cc&do=shop" ,//url
data: $('#jnfarm_pop').serialize(),
success: function (result) {
console.log(result);
if (result.resultCode == 200) {
alert("SUCCESS");
}
;
},
error : function() {
alert("ERROR");
}
});
}
</script>
<form method="post" id="jnfarm_pop" action="plugin.php?id=cc&do=shop">
<input type="hidden" name="shopsubmit" value="yes">
<!--first item-->
<input type="checkbox" name="jsid[1]" value="1">
<input type="number" style="width:3em;" name="qty[1]">
<!--second item-->
<input type="checkbox" name="jsid[2]" value="1">
<input type="number" style="width:3em;" name="qty[2]">
...continue 50 item
<button type="submit" class="layui-btn layui-btn-fluid" name="submitbutn" onclick="login()">submit</button>
</form>
PHP
<?php
if($_GET['id'] == 'cc'){
if(submitcheck('shopsubmit')){ //core function in !Discuz
for($x=1;$x<=50;$x++){
if($_GET['jsid'][$x] == '1'){
$qty[$x] = intval($_GET['qty'][$x]);
//process....
}
}
$final = 'message here';
echo json_encode(['final' => $final]);
}
}
?>
您不能像处理同步请求那样,在ajax请求上使用服务器端代码启动直接浏览器重定向。相反,您必须返回要重定向到的URL,然后在结果回调中执行类似于location.href=result.link的操作
对于ajax请求,最简单的选项如下所示
$.get('plugin.php?id=cc&do=shop', $('#jnfarm_pop').serialize(), result => {
//alert('success');
console.log(result); // check the result in console and if you can see it as a JS object you don't need to parse
result = JSON.parse(result); // Parse is required if you return the result as plain text otherwise you can omit this step in case you are returning the result as content type json
let final = result.final;
location.href = result.link;// if you need to get redirected
}).fail(result => {
alert('fail');
});
现在,在服务器端代码中,不是从PHP创建重定向,而是返回如下内容
return json_encode(['link' => 'somlink']);
当然,只需像往常一样返回成功消息。在Ajax中更改get的方法。php希望接收get参数,您将它们作为get传递到url中。url:$(''jnfarm_pop')。attr('action'),
Hi,在Ajax提交后,我只需要显示一条弹出消息,如success或error。无需再次重定向页面..$.get('plugin.php?id=cc&do=shop',$('jnfarm_pop').serialize(),(result)=>{alert('success');}).fail(result=>{alert('fail');});更新答案除此之外,是否可用于设置超时?我只是发现如果使用我的弹出窗口,它会很快消失。$.get是实现目标的最快方法。$.ajax通常在需要更大灵活性时使用,这在您的案例中是不需要的。我不明白你说的弹出窗口消失是什么意思。你使用js警报还是使用任何自定义弹出窗口?我使用一个名为Layui的自定义ui。但是没关系,现在我面临最后一个问题。。在我的plugin.php
,
中,如何在(结果)
中获得此$final
?
$.get('plugin.php?id=cc&do=shop', $('#jnfarm_pop').serialize(), result => {
//alert('success');
console.log(result); // check the result in console and if you can see it as a JS object you don't need to parse
result = JSON.parse(result); // Parse is required if you return the result as plain text otherwise you can omit this step in case you are returning the result as content type json
let final = result.final;
location.href = result.link;// if you need to get redirected
}).fail(result => {
alert('fail');
});
return json_encode(['link' => 'somlink']);