如何使用'return'关键字而不是'echo'从PHP文件返回ajax请求响应? [英] How to return ajax request response from PHP file with 'return' keyword instead of 'echo' ?
本文介绍了如何使用'return'关键字而不是'echo'从PHP文件返回ajax请求响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想返回请求的数据,其中包含带有数组名称的表单元素,如name =desc []等等,当我从php文件回显结果时,它会给出错误注意:数组到字符串转换,所以如何克服这个问题?我将响应附加在表格中点击
我尝试过:
我的ajax电话
I want to return requested data which included form elements with array name like name="desc[]" and so on when I echo result from php file it gives error "Notice: Array to string conversion", so how to overcome this issue? I am appending the response in table tbody onclick
What I have tried:
My ajax call
$.ajax({
type: "POST",
url: "ajaxRequests/createVrEntriesRows.php",
data: "form_data=" + form_data +"&rows=" + rows,
success: function(response){
$('#VR_Entries').append(response);
}
});
我的Php文件
My Php file
<?php
require_once '../controllers/conn.php';
if(isset($_POST["form_data"])){
extract($_POST);
$parts = explode('/', $domain_class);
$class = isset($parts[0])?$parts[0]:'';
$sub_class = isset($parts[1])?$parts[1]:'';
$domain = isset($parts[2])?$parts[2]:'';
$sr_no = $_POST['rows']+1;
return '<tr>
<td>
<input type="hidden" name="sr_no[]" value="'.$sr_no.'">'.$sr_no.'
</td>
<td>
<input type="hidden" name="job[]" value="'.$job.'">'.$job.'
</td>
<td>
<input type="hidden" name="account[]" value="'.$account.'">'.$account.'
</td>
<td>
<input type="hidden" name="class[]" value="'.$class.'">'.$class.'
</td>
<td>
<input type="hidden" name="sub_class[]" value="'.$sub_class.'">'.$sub_class.'
</td>
<td>
<input type="hidden" name="domain[]" value="'.$domain.'">'.$domain.'
</td>
<td>
<input type="hidden" name="desc[]" value="'.$description.'">'.$description.'
</td>
<td class="debit">
<input type="hidden" name="debit[]" value="'.$debit.'">'.$debit.'
</td>
<td class="credit">
<input type="hidden" name="credit[]" value="'.$credit.'">'.$credit.'
</td>
<td>
<button type="button" class="btn" onclick="DeleteRowFunction(this)">x</button>
<button type="button" class="btn">
</button>
</td>
</tr>';
}
?>
推荐答案
.ajax({
类型: POST,
url:ajaxRequests / createVrEntriesRows.php,
data:form_data =+ form_data +& rows =+ rows,
success:function(response) {
.ajax({ type: "POST", url: "ajaxRequests/createVrEntriesRows.php", data: "form_data=" + form_data +"&rows=" + rows, success: function(response){
('#VR_Entries')。append(response);
}
});
('#VR_Entries').append(response); } });
我的Php文件
My Php file
<?php
require_once '../controllers/conn.php';
if(isset(
_POST [form_data])){
extract(
_POST["form_data"])){ extract(
)
这篇关于如何使用'return'关键字而不是'echo'从PHP文件返回ajax请求响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文