简体   繁体   English

如何为jquery / ajax发布结果分配值?

[英]How do you assign values to the results of a jquery/ajax post?

I'm posting data to a php page using jquery. 我正在使用jquery将数据发布到php页面。 I want to then have the response from the page be able to be put into php variables on the original page. 然后,我希望页面的响应能够放入原始页面的php变量中。 I don't want to append a DIV or show the results in the HTML. 我不想添加DIV或在HTML中显示结果。 (This seems to be the only thing people do with post.) (这似乎是人们对邮寄的唯一选择。)

Here's the jquery: 这是jQuery:

$(document).ready(function () 
{
    $('#drpdown').change(function() 
    {
        drpdownval=$('#drpdown').val();
        $.post("page2.php",{incomingval:drpdownval},function(result))};

    });
});

PHP page2.php: PHP page2.php:

<?php
$valtoworkwith = $_REQUEST['incomingval'];
......../// do something
?>

I want to then do something on page2.php then send specific values back to the jquery to work with. 然后,我想在page2.php上执行一些操作,然后将特定值发送回jquery进行处理。 How? 怎么样? How do I assign them a name or separate them out?? 如何给他们分配名称或将他们分开??

EDIT 编辑

My jquery needed a number and not text. 我的jQuery需要一个数字而不是文本。

Here's the jquery: 这是jQuery:

$(document).ready(function () 
{
    $('#drpdown').change(function() 
    {
        drpdownval=$('#drpdown').val();
        $.post("page2.php",{incomingval:drpdownval},function(result){
          if(result != 1){ ///could not put result !="hello" or != hello
          $("#signup").overlay().load();  
          }
        )};
    });
});

Use JSON to get your inter-connectivity going, meaning the data exchange between client and server entirely through AJAX. 使用JSON可以实现相互连接,这意味着完全通过AJAX在客户端和服务器之间进行数据交换。 Here's a link to a StackOverflow answer which goes into detail about how to make that happen, using PHP for the back end: 这是一个指向StackOverflow答案的链接,该答案详细介绍了如何使用PHP作为后端来实现这一点:

jQuery AJAX Call to PHP Script with JSON Return jQuery AJAX使用JSON返回对PHP脚本的调用

Simply go here, it's as clear as it can get... 只需走到这里,就可以清楚地知道...

https://api.jquery.com/jQuery.ajax/ https://api.jquery.com/jQuery.ajax/

That's the documentation page for $.ajax, and if you scroll down you will have excellent examples you can start from. 这是$ .ajax的文档页面,如果向下滚动,您将有很好的示例。

Are you trying to do something like that? 您是否正在尝试做类似的事情?

if (!empty($_POST['incomingval'])){
  $valtoworkwith = $_POST['incomingval'];
  //do some stuff
  echo $valtoworkwith;
}

Or if you want to sent back a JSON 或者,如果您想发送回JSON

<?php

  function doSomeStuff($input) {
    //instead of that do some stuff and return back something that makes sense
    $output = $input;
    //
    return $output;
  }

  if (!empty($_POST['incomingval'])){
    header('Content-Type: application/json');

    $valtoworkwith = $_POST['incomingval'];
    $res = doSomeStuff($valtoworkwith);

    $data = array('incomingval' => $valtoworkwith, 'response' => $res);
    echo json_encode($data);
  }

?>

I hope that helped you because the answer it self was kind of vague. 希望对您有所帮助,因为答案本身含糊不清。

Tested with postman 经过邮递员测试

The in JS you can do with a promise 您可以在JS中做一个承诺

$(document).ready(function () 
{
    $('#drpdown').change(function() 
    {
        drpdownval=$('#drpdown').val();

        $.post("page2.php",{incomingval:drpdownval})
        .then(function(data){ //look how awesome the promise looks
        console.log(data.response); //do something with it
        })

    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM