php中如何给ajax返回值,如何从ajax调用的php返回值?(How to return values from php called by ajax?)...

如何从ajax调用的php返回值?(How to return values from php called by ajax?)

所以,我有一个网格框。 如果用户点击进入,则gridbox会创建一行并加载用户插入的数据。 程序应该运行一个mysql查询,其中包含用户在输入时插入的数据。

现在我有一个带有javascript的html页面,用于控制gridbox操作。 当用户点击进入时调用此js :

var dbs=0;

var val=[];

function myFunction(e) {

if(e.keyCode == 13){

var newId = (new Date()).valueOf();

var value=gridbox.cells(1,0).getValue();

if(value.substring(0,5)=="JrLbl")

{

//the getfn(value) should returns the value of the mysql query

gridbox.addRow(newId,[value.substring(5,(value.indexOf(".")-4)),getfn(value)]);

}

val.push(value);

gridbox.cells(1,0).setValue('');

gridbox.editCell(1,0);

dbs=dbs+1;

}

}

function upload(){

var jsonString = JSON.stringify(val);

var jsonData = $.ajax({

type: "POST",

url: "upload.php",

data: { 'data' : jsonString},

dataType:"json",

async: false

}).responseText;

}

function getfn(str){

xmlhttp = new XMLHttpRequest();

var lblid= str.substring(str.indexOf(".")+6,str.length);

alert(lblid);

xmlhttp.open("GET","getfn.php?q="+lblid,true);

xmlhttp.send();

}

并在getfn.php中运行mysql查询:

header('Content-Type: text/html; charset=UTF-8');

require_once($_SERVER['DOCUMENT_ROOT']."globalfunctions/globalClasses.php");

require_once($_SERVER['DOCUMENT_ROOT'].'globalfunctions/GlobalParameters.php');

error_reporting ( E_ALL ^ E_DEPRECATED);

$q = intval($_GET['q']);

$db_jira = new indotekDbConnection("jira");

$sql=mysql_query("SELECT FajlNev

from indoteklabels

where ID='$q'");

$FajlNev=mysql_fetch_row($sql);

return $FajlNev[0];

$db_jira->Close();

unset($db_jira);

?>

它返回应该标记的数据

Aveuu.png

So, I have a gridbox. If user hits enter, gridbox create a row and load datas inserted by user. Program should run a mysql query with those datas what user insert when he hits enter.

Now I have a html page with a javascriptthat controls the gridbox actions. This js is called when user hits enter:

var dbs=0;

var val=[];

function myFunction(e) {

if(e.keyCode == 13){

var newId = (new Date()).valueOf();

var value=gridbox.cells(1,0).getValue();

if(value.substring(0,5)=="JrLbl")

{

//the getfn(value) should returns the value of the mysql query

gridbox.addRow(newId,[value.substring(5,(value.indexOf(".")-4)),getfn(value)]);

}

val.push(value);

gridbox.cells(1,0).setValue('');

gridbox.editCell(1,0);

dbs=dbs+1;

}

}

function upload(){

var jsonString = JSON.stringify(val);

var jsonData = $.ajax({

type: "POST",

url: "upload.php",

data: { 'data' : jsonString},

dataType:"json",

async: false

}).responseText;

}

function getfn(str){

xmlhttp = new XMLHttpRequest();

var lblid= str.substring(str.indexOf(".")+6,str.length);

alert(lblid);

xmlhttp.open("GET","getfn.php?q="+lblid,true);

xmlhttp.send();

}

And in the getfn.php runs the mysql query:

header('Content-Type: text/html; charset=UTF-8');

require_once($_SERVER['DOCUMENT_ROOT']."globalfunctions/globalClasses.php");

require_once($_SERVER['DOCUMENT_ROOT'].'globalfunctions/GlobalParameters.php');

error_reporting ( E_ALL ^ E_DEPRECATED);

$q = intval($_GET['q']);

$db_jira = new indotekDbConnection("jira");

$sql=mysql_query("SELECT FajlNev

from indoteklabels

where ID='$q'");

$FajlNev=mysql_fetch_row($sql);

return $FajlNev[0];

$db_jira->Close();

unset($db_jira);

?>

And it returns the data what should go the marked place

Aveuu.png

原文:https://stackoverflow.com/questions/39222855

更新时间:2020-01-17 12:28

最满意答案

而不是使用return $FajlNev[0]; 使用$data = $FajlNev[0]; echo json_encode($data); $data = $FajlNev[0]; echo json_encode($data);

function getfn(str){

xmlhttp = new XMLHttpRequest();

var lblid= str.substring(str.indexOf(".")+6,str.length);

//alert(lblid);

xmlhttp.open("GET","getfn.php?q="+lblid,true);

xmlhttp.responseType = 'text';

xmlhttp.onload = function () {

if (xmlhttp.readyState === xmlhttp.DONE) {

if (xmlhttp.status === 200) {

var newId = (new Date()).valueOf();

gridbox.addRow(newId,[str.substring(5,(str.indexOf(".")-4)),xmlhttp.response]);

gridbox.cells(1,0).setValue('');

gridbox.editCell(1,0);

//console.log(xmlhttp.response);

//console.log(xmlhttp.responseText);

}

}

};

xmlhttp.send();

}

2016-08-30

相关问答

是的,可能而且非常难以实施。 假设您的HTML看起来像这样(这显然是丑陋的;)):

0

1

2

Text:

...

将所有复选框放在表单中,然后在表单上尝试$(“#form”)。serialize()。 第二件事,我认为它会发送复选框仅作为检查。 我想这可能会对你有所帮助。 我不明白的第三件事是为什么有重复的复选框,我的意思是所有都是两次,具有相同的名称和价值? Put all checkboxes in a form and try $("#form").serialize() on form. And 2nd thing, I think it will send checkboxes as on only

...

在generate.php将它们添加到数组并编码为JSON并发送。 在AJAX中获取JSON,打印出来。 让你的AJAX类似于: $.ajax({

type:'post',

url:'generate.php',

data: $(this).val(),

dataType: 'json', //Added this

success: function(data){ //value to data

$('#fileurl'

...

list()函数不是您要找的。 您想使用JSON或XML。 要生成JSON输出,请将所有值放在数组中,然后使用json_encode()输出: $output = array(

'name' => $b3name,

'icon' => $b3icon,

'traffic' => $b3traffic

);

echo json_encode($output);

您可能需要确保也正确设置了标头。 The list() function is not what you are

...

而不是使用return $FajlNev[0]; 使用$data = $FajlNev[0]; echo json_encode($data); $data = $FajlNev[0]; echo json_encode($data); function getfn(str){

xmlhttp = new XMLHttpRequest();

var lblid= str.substring(str.indexOf(".")+6,str.length);

...

在user.php页面上,您需要执行以下操作: - function init() {

$arr = array(

array(

"region" => "valore",

"price" => "valore2"

),

array(

"region" => "valore",

"pr

...

而不是尝试从构造函数返回值,而是使用另一个函数,如下所示。 class insert{

/**

* insert constructor.

* @param $name

* @param $uname

*/

private $res;

function __construct($name, $uname)

{

$conn = pg_connect("host=localhost dbname=test us

...

假设你的问题是没有'返回错误'; 或submit处理程序中的e.preventDefault()尝试此代码: $("#iForm").submit( function(e){

e.preventDefault();

var user = $("input:[name=username]").val();

var password = $("input:[name=password]").val();

var dbName = $("input:[name=dbNam

...

您的代码位于以下if语句中 if (result == '') {

// all your code is here

}

你回来了 echo ' ';

所以代码永远不会运行,因为它不是一个空字符串,它是一个空格,删除if语句 然后就是 var result = result;

删除那一行,没有意义! Your code is in the following if statement if (result == '') {

// all your code is here

}

...

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值