CURL表格回复

时间:2017-05-25 04:41:00

标签: php jquery curl

我的表单存在问题。问题是,当单击提交时,jQuery在response.length > 0语句中不起作用,但是表单正在工作并导入数据但是我需要它来更新div并加载感谢页面而不是像它那样做没事:)​​

应为response.length > 0 - 然后加载错误并更新div else转到感谢页面

jQuery的:

 $('#mountianForm').submit(function(e) { // catch the form's submit event
        $.ajax({ // create an AJAX call...
            data: $(this).serialize(), // get the form data
            type: $(this).attr('method'), // GET or POST
            url: $(this).attr('action'), // the file to call
            success: function(response) { // on success..
                if (response.length > 0) {
                        $('#mountianFormResponse').html(response); // update the DIV
                } else {
                           window.location.href = 'https://example.com/thank-you/';               

                }
            }
        });
        e.preventDefault();
    });

流程代码:

<?php

error_reporting(E_ALL); ini_set('display_errors', 1);


$url = 'https://id.infusionsoft.com/app/form/iframe/formID';
$userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36';

if (!empty($_GET))
{
    $url .= '?'.http_build_query($_GET);
}

$isPOST = !empty($_POST);


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_POST, $isPOST);
if ($isPOST)
{
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
}
$response = curl_exec($ch);
curl_close($ch);

if(preg_match('/(<ul>.*<\/ul>)/is', $response, $match)){
    $match[1];
}else{
$error = "Error";    
$file = fopen("mountianProxyerror.php", 'w+'); // Create a new file, or overwrite the existing one.
fwrite($file, $error);
fclose($file);

}

?>

1 个答案:

答案 0 :(得分:0)

我认为您没有从流程代码中返回响应。

if(preg_match('/(<ul>.*<\/ul>)/is', $response, $match))
{
    $match[1];
    $resp = array('status' => TRUE,'msg' => 'success','resp' => $response);
}
else
{
    $error = "Error";    
    $file = fopen("mountianProxyerror.php", 'w+'); // Create a new file, or overwrite the existing one.
    fwrite($file, $error);
    fclose($file);
    $resp = array('status' => FALSE,'msg' => 'Failed');
}

echo json_encode($resp);

在你的ajax中

$.ajax({ // create an AJAX call...
    data: $(this).serialize(), // get the form data
    type: $(this).attr('method'), // GET or POST
    url: $(this).attr('action'), // the file to call
    dataType : 'json',
    success: function(response) { // on success..
        alert(response.msg);
        if (response.status == true) {
            $('#mountianFormResponse').html(response.resp); 
            // Do whatever with the success code
        } 
        else 
        {
            window.location.href = 'https://example.com/thank-you/';
        }
    }
});