[英]Execute curl function after sending data with php and ajax
我有一個包含5個字段的表單和一個帶有ajax的提交按鈕的錨點。 我需要執行一個函數來檢索某些消息,具體取決於表單中插入的內容。 此功能根據電話號碼和代碼從另一個主機(鏈接)檢索不同的消息。 當我將php和ajax的數據插入數據庫以顯示來自該主機(鏈接)的答案時,如何使用此功能?
這是我的代碼:
驗證代碼的函數:
function hit_check_code_new($phone, $code){
$url = 'mycustom_path_not_displayed_for_security_reasons/filejson.php';
$fields = array(
'phone' => urlencode($phone),
'code' => urlencode($code)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$what_i_received_from_testmail = json_decode($result,true);
if (!isset($what_i_received_from_testmail['Respond'])) return 'Invalid response!';
if ($what_i_received_from_testmail['Respond'] == 'ACCESDENIED') return 'Error: IP invalid!';
if ($what_i_received_from_testmail['Respond'] == 'INVALIDPARAMS') return 'Error: invalid input params';
if ($what_i_received_from_testmail['Respond'] == 'TIMEOUT') return 'Error: SQL is not responding!';
return $what_i_received_from_testmail['Respond'];
}
add_action("wp_ajax_enter_code", "enter_code");
add_action("wp_ajax_nopriv_enter_code", "enter_code");
function enter_code(){
global $wpdb;
$code = (isset($_POST['code']))?htmlspecialchars(trim($_POST['code'])):'';
$fname = (isset($_POST['fname']))?htmlspecialchars(trim($_POST['fname'])):'';
$lname = (isset($_POST['lname']))?htmlspecialchars(trim($_POST['lname'])):'';
$phone = (isset($_POST['phone']))?htmlspecialchars(trim($_POST['phone'])):'';
$email = (isset($_POST['email']))?htmlspecialchars(trim($_POST['email'])):'';
if(isset($_POST['code']) && !empty($_POST) && is_user_logged_in()){
$table = 'tst_contest';
$data = array(
'code' => $code,
'fname' => $fname,
'lname' => $lname,
'phone' => $phone,
'email' => $email
);
$format = array('%s', '%s', '%s', '%s', '%s');
$success = $wpdb->insert( $table, $data, $format );
}
}
而且我還有一個ajax代碼,可以很好地通過最后一個函數將數據插入數據庫。
我的最后一個問題是: 如何將第一個功能實現到第二個功能中,以達到我一開始所說的? 提前致謝。
根據Fresher的建議,第二個ajax (我在第一個ajax成功函數中使用了它):
$.ajax({ type: "POST", url: ajaxurl, data: {action: 'hit_check_code_new', phone: phone, code: code}, success: function(msg){ console.log("SECOND AJAX REQUEST"); } })
查看代碼段將以某種方式幫助您解決
<?php
/*
* Plugin Name: Testing
*
*/
add_action('wp_ajax_hit_check_code_new', 'curl_response');
add_action('wp_ajax_nopriv_hit_check_code_new', 'curl_response');
function curl_response() {
$ch = curl_init();
$ajax_url = 'http://localhost/fantastic/wp-admin/admin-ajax.php';
curl_setopt($ch, CURLOPT_URL, $ajax_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('action' => 'ajaxTester', 'param1' => $_POST['phone'])));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
exit();
}
add_action('wp_head', 'button_ajax');
function button_ajax() {
?>
<input type="text" name="fresher_text_field" class="fresher_text_field" value="124578989"/>
<input type="submit" name="fresher_click_me" id="fresher_click_me" value="Submit Me" />
<script type="text/javascript">
jQuery(function () {
jQuery('#fresher_click_me').click(function () {
jQuery.ajax({
type: "POST",
url: "<?php echo admin_url('admin-ajax.php'); ?>",
data: {action: 'hit_check_code_new', phone: jQuery('.fresher_text_field').val()},
success: function (msg) {
console.log(msg);
}
});
});
});
</script>
<?php
}
function handle_ajaxTester() {
if (isset($_POST['param1']) && $_POST['param1'] != '') {
echo '<br>Response:<br>';
print_r($_POST);
exit();
}
}
add_action('wp_ajax_ajaxTester', 'handle_ajaxTester');
add_action('wp_ajax_nopriv_ajaxTester', 'handle_ajaxTester');
編輯:
請無需在curl代碼中直接指向ajax函數,而是像我所說的那樣做最后一個函數。
function hit_check_code_new($phone, $code){
// Your Code
}
function second_ajax_function() {
$phone = $_POST['phone'];
$code = $_POST['code'];
echo hit_check_code_new($phone,$code);
exit();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.