Ajax 表单提交到 PHP 文件和 URL
ajax form submit to php file and to url?
我在wordpress中有一个订阅表单,(目前(只能将提交的电子邮件添加到自定义帖子类型(subscribers
(并在后端显示。我想知道如何也拥有此功能,并将此电子邮件发送到自定义 URL(具体到 APSIS(。
所以我有一个表格
<form id="subscribe" class="subscribe_form" name="subscribe_form" action="#" method="post">
<input name="subscriber_email" class="subscriber_email" placeholder="Your mail here">
<input class="submit" type="submit" value="Submit">
</form>
我的保存自定义帖子类型函数,通过 AJAX 执行
<?php
add_action('wp_ajax_save_subscriber', 'save_subscriber');
add_action('wp_ajax_nopriv_save_subscriber', 'save_subscriber');
if (!function_exists('save_subscriber')) {
function save_subscriber() {
if (isset($_POST['subscriber_email']) && is_email($_POST['subscriber_email'])) {
global $wpdb;
$post_data = array(
'post_type' => 'subscribers',
'post_status' => 'publish'
);
$published_id = wp_insert_post( $post_data );
add_post_meta($published_id, 'subscriber_email', $_POST['subscriber_email']);
$out = 'OK';
} else{
$out = 'ERROR';
}
die($out);
}
}
还有我的阿贾克斯
$('#subscribe').submit(function() {
'use strict';
var str = $(this).serialize() + '&action=save_subscriber';
var $form = $(this);
var $wrapper = $(this).parent();
$.ajax({
type: 'POST',
url: custom.ajaxurl,
data: str,
success: function(msg){
if( msg === 'OK' ) {
$form.animate({ height: '0px' }, 800, function() {
$form.hide();
});
$wrapper.find('.success_message').delay(400).html(custom.success).slideDown(600);
}else {
$wrapper.find('.subscriber_email').addClass('field_error').attr('placeholder', custom.error_mail).val('').focus();
}
}
});
return false;
});
现在我有更多的包装器,以及noonce字段等,但这在这里并不重要。
当您只想向 CPT 添加帖子时,这很好用,但我需要将其提交给
http://www.anpdm.com/public/process-subscription-form.aspx?formId=xxxxxxxxxxx
我从客户端获得了 id 和所有内容,现在我需要实现这一点。
我见过一些关于卷曲的东西,但我从来没有做过任何事情,所以我真的不知道从哪里开始。由于我的操作指向我的save_subscriber()
函数,因此我发现在该函数中,我还必须添加一种将此表单发送到所需 url 的方法。但是怎么做呢?
任何信息都会有所帮助,谢谢。
答
因此,按照白银提供的答案,我设法让它工作。
#Run CURL
$url = 'http://www.anpdm.com/public/process-subscription-form.aspx?formId=xxxxxxxxxx';
$request = curl_init();
curl_setopt_array( $request, array (
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_URL => $url,
CURLOPT_POST => 1,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13',
CURLOPT_POSTFIELDS => $custom_query_string,
));
$response = curl_exec($request);
$response_data = curl_getinfo($request);
curl_close($request);
#END CURL
if ($response_data['http_code'] == 200) {
$out = 'OK';
}
formId
是特定于此用户的表单编号,$custom_query_string
符合 APSIS 提供的表单,包含如下内容:
pf_Email=$_POST['email']&
Submit=Prenumerera&
pf_DeliveryFormat=HTML&
pf_MailinglistName1=xxxxx&
pf_FormType=OptInForm&
pf_OptInMethod=SingleOptInMethod&
pf_CounterDemogrFields=0&
pf_CounterMailinglists=1&
pf_AccountId=xxxx&
pf_ListById=1&
pf_Version=2
在那之后,我把我的订阅者放在wordpress后端,他们出现在APSIS控制台中,用户被:)
我想我需要user_agent正确的查询字符串。
如果我是你,我只会使用不同的ID和指向不同URL的操作复制当前表单,然后将其隐藏,根据firs表单自动填充它,然后静默地提交它 第一个表单 Ajax 请求的成功响应,
但是,是的,如果您需要使用 CURL-PHP 进行 POST URL,而没有任何形式,下面的示例代码可能会这样做,我将其挂在您当前的函数上。
function save_subscriber() {
if (isset($_POST['subscriber_email']) && is_email($_POST['subscriber_email'])) {
global $wpdb;
$post_data = array(
'post_type' => 'subscribers',
'post_status' => 'publish'
);
$published_id = wp_insert_post( $post_data );
add_post_meta($published_id, 'subscriber_email', $_POST['subscriber_email']);
#Run CURL
$url = 'http://www.anpdm.com/public/process-subscription-form.aspx'; // URL to be Posted
$request = curl_init(); //open connection
curl_setopt_array( $request, array (
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_URL => $url,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $_REQUEST, /* Since your form is already serialize, you don't need to build query string*/
));
$response = curl_exec($request); // Execute Curl
$response_data = curl_getinfo($request); // Array of curl response info
curl_close($request); // Close Connection
#END CURL
#$out = $response; // push curl response on Ajax Submit response
$out = $response_data['http_code']; /* 200 will be the value of this for successfully curl request, you can just replace your Ajax success code with 200 instead of OK or just ignore the response, though its better to know if curl request is successful*/
} else{
$out = 'ERROR';
}
die($out);
}
测试
$url = 'http://www.anpdm.com/public/process-subscription-form.aspx';
$request = curl_init();
curl_setopt_array( $request, array (
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_URL => $url,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => "id=noID&value=novalue",
));
$response = curl_exec($request);
$response_data = curl_getinfo($request);
curl_close($request);
print_r( $response_data );
$response_数据输出
Array
(
[url] => http://www.anpdm.com/public/process-subscription-form.aspx
[content_type] => text/html; charset=utf-8
[http_code] => 200
[header_size] => 222
[request_size] => 180
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 1.546
[namelookup_time] => 0.515
[connect_time] => 0.843
[pretransfer_time] => 0.843
[size_upload] => 21
[size_download] => 13048
[speed_download] => 8439
[speed_upload] => 13
[download_content_length] => 13048
[upload_content_length] => 21
[starttransfer_time] => 1.203
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 89.234.52.177
[certinfo] => Array
(
)
[primary_port] => 80
[local_ip] => 192.168.254.8
[local_port] => 53736
)
- 直接下载文件,而不是从window.open(url)
- CKFinder 3为所选文件返回错误的URL
- 将CSV文件从URL导入Node.js
- 如何从“”运行url;.js”;文件
- 如何在不在本地下载的情况下将url中提供的文件(pdf/doc)转换为json/string/base64格式
- 如何使用javascript或jquery mobile从url读取和显示XML文件
- gcloud节点-我如何确保通过签名URL上传到谷歌云存储的文件是公开可读的
- 将任何CDN配置为只传递一个文件,无论请求的url是什么
- jqueryajax调用是复制url,当在模态视图之后在外部js文件中进行调用时
- 生成的excel文件url
- 如何在PHP中使用url保护我的图像和文件夹免受直接访问
- 如何使用phonegap从android中的URL下载文件
- 读取不带fs的txt文件nodejs(来自url)
- 检查蒸汽配置文件url是否有效
- ExtJS:从受保护的url创建JSB3文件
- 将 MVC 3 应用程序部署到 URL 文件夹已损坏 JavaScript 和内容路径
- 如何获取JSON URL文件并使其不断更新
- 获取远程加载图像的url's文件大小
- JS智能感知不工作的外部URL文件在VS2012
- ASP.. NET - windows . open (URL) -文件被缓存,我如何停止它