【问题标题】:Try to using ajax form instead of form action (solve)尝试使用 ajax 表单而不是表单操作(解决)
【发布时间】:2020-08-30 15:09:00
【问题描述】:

我想改善我网站的用户体验。所以我尝试改变表单动作ajax,我一直在尝试一些教程,但我仍然卡住了。

我正在使用一个 php 论坛程序/源代码调用 !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>

PHP,文件名plugin.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....
      }
    }
    showmessage('message here','redirectlink');//this is !Discuz program function and it is fine.
  }
}
?>

上述脚本在使用form action 时运行良好,并重定向到我的输出页面。如果我想改成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>

而且是不是要调整plugin.php源代码?

已更新,以下对我有用,谢谢 fayis003。 html改成&lt;script&gt;&lt;/script&gt;

$.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
   alert(result.final);//alert message here
   location.href = result.link;// if you need to get redirected

}).fail(result => {
   alert('fail');
});

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 中将方法更改为 get.php 期望接收 get 参数,并且您在 url 中将它们作为 get 传递。
  • url: $('#jnfarm_pop').attr('action'),

标签: php ajax ajaxform


【解决方案1】:

您不能像使用同步请求一样在 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('plugin.php?id=cc&do=shop', $('#jnfarm_pop').serialize(), (result) => { alert('success'); } ).fail(result => { alert('fail'); });更新答案
  • 除此之外,是否可以设置超时?我只是发现如果使用我的弹出窗口,它会很快消失..
  • $.get 是实现您的目的的最快方法 $.ajax 通常在您需要更大的灵活性时使用,而您的情况不需要这种灵活性。我不明白弹出窗口消失是什么意思。您使用 js 警报还是使用任何自定义弹出窗口?
  • 我使用了一个名为 Layui 的自定义 ui。但没关系,现在我面临最后一个问题.. 在我的plugin.php&lt;?php //much code here; $final = 'custom wording'; ?&gt;,我如何在(result) 中得到这个$final
猜你喜欢
  • 1970-01-01
  • 2016-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多