dsf5989 2012-11-10 17:37
浏览 18
已采纳

PHP + AJAX - 传递当前的$ _GET变量

I have a page where users fill out $_GET data for some options. I'd like to pass these $_GET variables using AJAX to a .php script. But my issue is how do I pass those $_GET variables they filled out so far, without refreshing the page?

Here is my code so far.

$.ajax({
type: "GET",
url: "serverside script to process on data",
data:{name:youwant}, // Here is where I want to take what the user has filled out so 
                     // far, and place it here all without refreshing the page
success: function(data){
     alert("return here if success")

}
})
  • 写回答

2条回答 默认 最新

  • douxie3625 2012-11-10 18:54
    关注

    First of all, drop this task into small ones:

    1) Get/process variables in JavaScript

    2) Send them to PHP

    3) Parse/handle the ones

    4) Depending on result send respond back to JavaScript

    5) Handle that respond and display a message to user

    Take a look at this example, Let's assume that jquery.js is loaded. Assume that we want to send the values of the inputs we have - email and password.

    <script type="text/javascript">
    
      $("#Send").click(function(){
    
         $.ajax({
    
             type : "GET", 
             //Look carefully:
             data : {
               // it'll be PHP vars       // This is JS vars
               email                :   $("#email").val(),
               password             :   $("#password").val()
             },
    
             success : function(respondFromPHP){
    
                  alert(respondFromPHP);
             }
          });
    
    
      });
    
    </script>
    
    <input type="text" id="email" />
    <input type="password" id="password" />
    
    <br />
    <button id="Send">Send to php</button>
    

    In your php script, just handle vars you get, like this:

    <?php
    
    print_r($_GET); // will print smth like Array("email" => "foo", "password" => "bar")
    
    // Then create function so that you can simplify handling of the vars.
    // Like this:
    function validate_password($password){}
    function validate_email($email){}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器