尝试使用AJAX和cURL提交表单

时间:2018-10-01 09:33:41

标签: php twitter-bootstrap forms curl

我正在尝试通过cURL提交表单,POST方法似乎运行良好。 我唯一的问题是表格尚未提交。该呼叫来自home.php网站,用于在另一个网站上填写新闻通讯表单。样式是通过Bootstrap完成的。

cURL命令:

if(!empty($_POST['p-firstname'])){
    $post_data['name'] = $_POST['p-firstname'];
}

if(!empty($_POST['p-lastname'])){
    $post_data['lastname'] = $_POST['p-lastname'];
}

if(!empty($_POST['p-email'])){                                 
    $post_data['email'] = $_POST['p-email'];
}

if(!empty($_POST['submit1'])){                                 
    $post_data['submit'] = $_POST['submit1'];
}

if(!empty($_POST['p-firstname'])  &&  (!empty($_POST['p-lastname'])) && (!empty($_POST['p-email'])) && (!empty($_POST['submit1']))){ 
    foreach ( $post_data as $key => $value) 
    {
        $post_items[] = $key . '=' . $value;
    }
    $post_string = implode ('&', $post_items);

    $curl_connection = curl_init('http://www.xx.xxxx-xxxxx.com/xxxxx/contents/mail/register.php');
    curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 1000);
    curl_setopt($curl_connection, CURLOPT_TIMEOUT, 0);
    curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
    curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl_connection, CURLOPT_POST, 1);
    curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
    $result = curl_exec($curl_connection);                                   
    error_log($post_string);
    print_r(curl_getinfo($curl_connection));
    echo curl_errno($curl_connection) . '-' . 
    curl_error($curl_connection);
    curl_close($curl_connection);
}

<form action="register.php" method="post">
<?php   if ($error) { ?>
    <div class="error">There was an error subscribing you to the newsletter.</div>
    <?php } ?>

  <div class="form-group">
    <label for="name">Your first name</label>
    <input type="name" class="form-control<?php check('name')?>" name="name" id="name" placeholder="Max" value="<?= htmlentities($fields['name']); ?>">
    <div class="invalid-feedback">
      Please provide a proper first name.
    </div>
  </div>

   <div class="form-group">
    <label for="name">Your last name</label>
    <input type="name" class="form-control<?php check('lastname')?>" name="lastname" id="lastname" placeholder="Mustermann" value="<?= htmlentities($fields['lastname']); ?>">
    <div class="invalid-feedback">
      Please provide a proper last name.
    </div>
  </div>


    <div class="form-group">
    <label for="email">Email address</label>
    <input type="email" class="form-control<?php check('email')?>" name="email" id="email" placeholder="xxx@xxxxx.com" value="<?= htmlentities($fields['email']); ?>">
    <div class="invalid-feedback">
      Please provide a proper email-address.
    </div>
  </div>

  <button name="submit" value ="submit" type="submit" class="btn btn-primary">Submit</button>
</form>

我将POST字符串记录为:name=test&lastname=test&email=xxxx.xxxx@xxxx.com&submit=submit

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

对于遇到类似问题的任何人,最好再执行一次Ajax调用。我在register.php中包含的文件有问题,这就是为什么通话时间太长且超时的原因。

$.ajax({
       type: "POST",
       async: false,
       cache: false,           
       url: "https://www.xxx.xxx/xxxx/contents/mail/register.php",
       data: { firstname:name, lastname:lastname, email:email,}, 
       });      

实际上知道如何通过浏览器工具调试XHR请求非常有帮助。