duanpo7354 2013-04-26 04:07
浏览 37
已采纳

将表单输入存储在javascript变量中,然后将其值传递给php变量而不提交表单? [重复]

This question already has an answer here:

what I want to do is whenever a user fills his address in text field input I want to grab it on blur with javascript which I am able to do .. now how to pass this variable in a php variable so that i can use it for some operations ? the javascript file is called inside php file ..

</div>
  • 写回答

2条回答 默认 最新

  • douchensou6495 2013-04-26 04:25
    关注

    Pull the data from the form elements first

    var dataObject = {};
    // #search is the form with search params
    $.each($('#form name').serializeArray(), function(i, field) {
    dataObject[field.name] = field.value;
    });
    

    then POST the data to the php

        $.ajax({  
             type: "POST",  
             url: "./api.php",  
            data: dataObject,
            success: function(dataout) {
                //dataout is what is returned from php
                //process it how you like from here
            }
        });
    

    in php do something with POST data

    <?php
       $element1 = $_POST["form_element_name1"];
       $element2 = $_POST["form_element_name2"];
       ...do something
       print $result;
    ?>
    

    the printed result is output to dataout in the ajax function.. process from there

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错