我在jQuery中具有以下功能:
问题是我无法在函数外部传递变量 branchaddress 。只能显示在$ .get();内部;有什么方法可以传递值,还是需要寻找另一种方法?
这是我的完整代码
function.js
function approveData(name, branch,email){
var branchaddress = "";
$.get('php/ajax-get-branch-address.php', {branch_address: branch}).done(function(data){
ar = JSON.parse(data);
branchaddress = ar.BRANCH_ADDRESS;
// alert(branchaddress);
});
// alert(branchaddress);
$.get("php/ajax-mail.php",
{
"type": 'APPROVE',
"name": name,
"branchaddress": branchaddress,
"email" : email
}
);
}
ajax-get-branch-address.php
<?php
$connStr = "";
$conn = oci_connect("", "", $connStr);
$branch_address = $_GET['branch_address'];
$query = "select * from table where branch_name = '".$branch_address."' ";
$result = oci_parse($conn, $query);
oci_execute($result);
$row = oci_fetch_array($result);
echo json_encode($row);
?>
ajax-mail.php
<?php
$to = $_GET['email'];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: Sender <noreply@mail.com>";
if($_GET['type'] == "APPROVE"){
$message =
"<html>
<head></head>
<body>
<table style='font-family:Century Gothic'>
<tr>
<td style='padding-bottom:5%;'>Dear $_GET[name],</td>
</tr>
<tr>
<td style='padding-bottom:5%;'>Thank you. Detail as below :</td>
</tr>
<tr>
<td style='padding-bottom:5%;'>
<table style='margin:0 10%;font-family:Century Gothic;'>
<tr>
<td>Officer Name</td>
<td style='min-width:10px'></td>
<td>:</td>
<td style='min-width:10px'></td>
<td>$_GET[name]</td>
</tr>
<tr>
<td>Branch Address</td>
<td style='min-width:10px'></td>
<td>:</td>
<td style='min-width:10px'></td>
<td>$_GET[branchaddress]</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>Thanks and regards;</td>
</tr>
</table>
</body>
</html>";
}
echo $message;
mail($to, "Sender", $message, $headers, "-f noreply@mail.com");
?>
感谢有人可以帮助解决此问题。
P / S:我已经搜索了这个问题,但是很难理解建议的答案。
答案 0 :(得分:1)
尝试一下
function approveData(name, branch,email){
var branchaddress = "";
$.get('php/ajax-get-branch-address.php', {branch_address: branch}).done(function(data){
ar = JSON.parse(data);
branchaddress = ar.BRANCH_ADDRESS;
$.get("php/ajax-mail.php",
{
"type": 'APPROVE',
"name": name,
"branchaddress": branchaddress,
"email" : email
}
);
});
}
您没有在第二个ajax get中获得价值,因为,第一个请求尚未完成,如果您要这样做,可以在第一个请求中使用适当的ajax请求使用“ async:false”,而不是获取请求如下,
function approveData(name, branch, email) {
var branchaddress = "";
$.ajax({
url: 'php/ajax-get-branch-address.php'),
type: "get",
data: {branch_address: branch},
async: false,
success: function (data) {
ar = JSON.parse(data);
branchaddress = ar.BRANCH_ADDRESS;
// alert(branchaddress);
}
});
// alert(branchaddress);
$.get("php/ajax-mail.php",
{
"type": 'APPROVE',
"name": name,
"branchaddress": branchaddress,
"email": email
}
);
}
答案 1 :(得分:1)
欢迎使用异步调用(AJAX)!这里的问题是,当您的函数执行时,它从上到下运行。但是,您正在执行对外部服务的两个异步调用,顾名思义,它们是异步的。所以在您的代码中,它是这样的:
Name Settings
Andre 1
Andre 1
Andre 1
Betty 2
Charles 3
Charles 3
; function pdf_download($client_id)
{
$styles[] = array(
'type' => 'file',
'media' => 'all',
'data' => pdf_css_path(),
'group' => CSS_DEFAULT,
'every_page' => false,
'weight' => 0,
'preprocess' => false,
'browsers' => array(),
);
$html1 = $html2 = drupal_get_css($styles);
$booking = entity_load_single('booking_summary', $client_id);
$booking_summary_info = booking_summary_details($booking, false);
$html1 .= theme('space_booking_summary_pdf_pg1', array('info' => $booking_summary_info, 'order' => $booking));
$html2 .= theme('space_booking_summary_pdf_pg2', array('info' => $booking_summary_info, 'order' => $booking));
// Initialize pdf instance
$tcpdf = tcpdf_get_instance();
// Assign header and footer
$tcpdf->DrupalInitialize(array(
'footer' => array(
'html' => '<div style="text-align: center;"><strong>This is a computer generated pdf.</strong></div>',
),
'header' => array(
'callback' => 'booking_summary_pdf_header',
),
));
// Write html to pdf
$tcpdf->writeHTML($html1, true, false, true, false, '');
// add a page
$tcpdf->AddPage();
$tcpdf->writeHTML($html2, true, false, true, false, '');
// reset pointer to the last page
$tcpdf->lastPage();
// Download pdf
$bk_created = date('j F Y', $booking_summary_info['created']);
$filename = "BK{$booking_summary_info['booking_id']}";
$filename .= "_" . str_replace(' ', '_', $booking_summary_info['billing_user']['fullname']);
$filename .= "_" . str_replace(' ', '_', $bk_created);
// Supply a filename including the .pdf extension
$filename = "{$filename}.pdf";
// Change the path to whatever you like, even public:// will do or you could also make use of the private file system by using private://
$uri = 'public://tmp/';
$stream = file_stream_wrapper_get_instance_by_uri($uri);
$path = DRUPAL_ROOT . '/' . $stream->getDirectoryPath() . '/' . file_uri_target($uri);
// Create the full path
$full_path = "{$path}/{$filename}";
$tcpdf->Output($full_path, 'I');
}
进行异步调用,并在完成后 ,调用$nextTick
方法 this.$nextTick(function() {
this.checkInputs().then(function(results) {
console.log(results);
});
});
处再次调用,并在完成后 ,调用branchaddress
方法。您现在看到问题了吗?您想在第一个请求完成之前使用php/ajax-get-branch-address.php
,这就是为什么该值尚未准备好的原因。您在这里有几种选择,但最简单的选择是使用回调函数在第一次调用之后 之后进行第二次调用,如下所示:
.done()
您也可以使用Promises使其看起来更整洁。但是我们的想法是在第一次调用完成后而不是在调用发生之前访问值。