Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Post方法和Ajax在表单中从一个表单接收数据到另一个表单_Javascript_Php_Jquery - Fatal编程技术网

Javascript 使用Post方法和Ajax在表单中从一个表单接收数据到另一个表单

Javascript 使用Post方法和Ajax在表单中从一个表单接收数据到另一个表单,javascript,php,jquery,Javascript,Php,Jquery,我尝试在处理文件中使用post方法获取数据,但无法。 我需要通过POST方法接收数据,并在process.php中显示我的数据 请记住,我的输入不在表单标记中。 我只是想用window.location.href代码转到相关页面。 我认为当用户被JavaScript转移到处理页面时。 post数据丢失 $(文档).ready(函数(){ $(“#smp”)。单击(函数(事件){ var name=$('#name').val(); $.ajax({ url:“process.php”, 方法:

我尝试在处理文件中使用post方法获取数据,但无法。 我需要通过POST方法接收数据,并在process.php中显示我的数据 请记住,我的输入不在表单标记中。 我只是想用window.location.href代码转到相关页面。 我认为当用户被JavaScript转移到处理页面时。 post数据丢失

$(文档).ready(函数(){
$(“#smp”)。单击(函数(事件){
var name=$('#name').val();
$.ajax({
url:“process.php”,
方法:“张贴”,
资料:姓名,,
成功:功能(数据){
window.location.href=“process.php”;
},
错误:函数(jqXHR,textStatus,errorshown){//如果process.php没有返回有效的响应(return),则会发生错误
警报(数据);
警报('发生错误…有关详细信息,请查看控制台(F12或Ctrl+Shift+I,控制台选项卡)!');
$(“#result').html(“状态代码:”+jqXHR.status+”

错误抛出:“+error抛出+”

”+jqXHR.responseText+”); log('jqXHR:'); console.log(jqXHR); log('textStatus:'); console.log(textStatus); log('error抛出:'); console.log(错误抛出); } }); }); });

提交

我在一篇评论中解释了代码中的内容。您没有理由发送JSON请求,因为您发送的是键中相同的值,而该值是无意义的

$(document).ready(function(){
        $("#smp").click(function(event){
         var name = $('#name').val();
         $.ajax({

                  url: "process.php",
                        method: "POST",
                        dataType: 'text',
                        data:{"name" : name}, // here you are sending process.php name too. if it returns something then it should go th the success function and redirect.
                        success: function (data) {
                            window.location.href = "process.php?name=" + name; // if the function returned something then it will redirect it to "process.php?name=" + name
                        },
                        error: function (jqXHR, textStatus, errorThrown) { // the error will accure if process.php did not return a valid response (return)
                            alert(data);
                            alert('An error occurred... Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information!');
                            $('#result').html('<p>status code: ' + jqXHR.status + '</p><p>errorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p><div>' + jqXHR.responseText + '</div>');
                            console.log('jqXHR:');
                            console.log(jqXHR);
                            console.log('textStatus:');
                            console.log(textStatus);
                            console.log('errorThrown:');
                            console.log(errorThrown);
                        }
                    });

        });
    });

需要补充的重要信息:如果坚持不重定向表单请求,可以使用
.preventDefault()

我在一条评论中解释了代码中发生的事情。您没有理由发送JSON请求,因为您发送的是键中相同的值,而该值是无意义的

$(document).ready(function(){
        $("#smp").click(function(event){
         var name = $('#name').val();
         $.ajax({

                  url: "process.php",
                        method: "POST",
                        dataType: 'text',
                        data:{"name" : name}, // here you are sending process.php name too. if it returns something then it should go th the success function and redirect.
                        success: function (data) {
                            window.location.href = "process.php?name=" + name; // if the function returned something then it will redirect it to "process.php?name=" + name
                        },
                        error: function (jqXHR, textStatus, errorThrown) { // the error will accure if process.php did not return a valid response (return)
                            alert(data);
                            alert('An error occurred... Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information!');
                            $('#result').html('<p>status code: ' + jqXHR.status + '</p><p>errorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p><div>' + jqXHR.responseText + '</div>');
                            console.log('jqXHR:');
                            console.log(jqXHR);
                            console.log('textStatus:');
                            console.log(textStatus);
                            console.log('errorThrown:');
                            console.log(errorThrown);
                        }
                    });

        });
    });

需要补充的重要信息:如果坚持不重定向表单请求,可以使用
.preventDefault()

为了使用ajax提交表单,您需要使用
preventDefault()
防止表单的默认提交行为

在代码中,您可以这样做:

$("#smp").click(function(event){
  event.preventDefault(); // prevent the default submit
  var name = $('#name').val();
  ...

一些旁注:

  • 与其使用submit按钮的
    单击事件
    ,不如使用表单本身的
    提交事件
    ,而不是使用给定的jQuery函数来收集要手动发送的输入数据。例如,您可以在表单中添加一个ID:
  • 然后

    $("#myForm").click(function(event){
      event.preventDefault();
      var $form = $(this); // the form that gets submitted
      var url = $form.attr('action'); // get the url from the forms action attribute
      var data = $form.serialize(); // get the form as url encoded string
      var method = $form.attr('method');
      $.ajax({
        url: url,
        method: method,
        data: data,
        success: function ( response ) {
          window.location.href = url + '?' + data;
        },
    });
    
    通过这样做,您现在可以将输入字段添加到表单中,而不必手动将它们添加到数据和url字符串中

  • 假设您在输入中输入“tony”,然后点击
    submit
    。您的代码现在执行以下操作:

  • 通过ajax将
    {“name”:“tony”}
    发布到process.php
  • php响应
    良好的POST
    ,并且完成了
  • success
    回调中,您将收到带有
    响应的回声
  • 使用get参数立即重定向到
    process.php
    页面
    name=tony
  • 由于
    $user\g
    变量的值为
    tony
    ,因此将调用process.php并不进行任何回显

  • 因此,您不会在任何地方看到
    Good的输出,因为您会立即重定向。

    为了使用ajax提交表单,您需要使用
    preventDefault()
    防止表单的默认提交行为

    在代码中,您可以这样做:

    $("#smp").click(function(event){
      event.preventDefault(); // prevent the default submit
      var name = $('#name').val();
      ...
    

    一些旁注:

  • 与其使用submit按钮的
    单击事件
    ,不如使用表单本身的
    提交事件
    ,而不是使用给定的jQuery函数来收集要手动发送的输入数据。例如,您可以在表单中添加一个ID:
  • 然后

    $("#myForm").click(function(event){
      event.preventDefault();
      var $form = $(this); // the form that gets submitted
      var url = $form.attr('action'); // get the url from the forms action attribute
      var data = $form.serialize(); // get the form as url encoded string
      var method = $form.attr('method');
      $.ajax({
        url: url,
        method: method,
        data: data,
        success: function ( response ) {
          window.location.href = url + '?' + data;
        },
    });
    
    通过这样做,您现在可以将输入字段添加到表单中,而不必手动将它们添加到数据和url字符串中

  • 假设您在输入中输入“tony”,然后点击
    submit
    。您的代码现在执行以下操作:

  • 通过ajax将
    {“name”:“tony”}
    发布到process.php
  • php响应
    良好的POST
    ,并且完成了
  • success
    回调中,您将收到带有
    响应的回声
  • 使用get参数立即重定向到
    process.php
    页面
    name=tony
  • 由于
    $user\g
    变量的值为
    tony
    ,因此将调用process.php并不进行任何回显

  • 因此,您不会在任何地方看到
    Good-its-POST的输出,因为您会立即重定向。

    ajax调用中的错误是什么?我觉得以前应该有一个类型错误that@Patch不显示POST方法的结果为什么要将其作为JSON请求发送?这似乎毫无意义,值和键是相同的,我建议像
    data:name
    那样发送它。我很确定,如果您想以
    ajax
    的形式发送表单,而不是以正常的提交方式发送表单,则需要使用
    event.preventDefault()防止表单的默认提交行为,那么错误就是您正在发送的数据。顺便说一句,php部分非常奇怪。您可以确定变量是否已发送