doupuzhimuhan9216 2016-02-26 19:27
浏览 65
已采纳

使用提交按钮调用curl post函数后重定向到感谢页面

So i was working on a php web application. Here is the form(trackit.php) i am working on.

<?php
function curlPOST($fields) {
$url = 'http://example.com/mailit4.php';

    //url-ify the data for the POST^M
    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,1);
    //execute post
    $result = curl_exec($ch);
    //close connection
    curl_close($ch);
    return $result;
}
$lid = $_GET["lid"];
$list_id = $_GET["list_id"];
$phone=$_GET["phone"];
/*
$filename = 'leadids.txt';
$contents = file($filename);
$myfile = fopen("leadids.txt", "a") or die("Unable to open file!");
*/
$fields = array('lid' => $lid, 'list_id' => $list_id, 'phone'=> $phone);


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Client Appreciation Weekend 2016</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>


<table width="986" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><img src="images/example" alt="example.com" width="600" height="133" style="margin-left:180px;" border="0" usemap="#Map" /></td>
  </tr>
</table>



<table width="986" border="0" align="center" cellpadding="0" cellspacing="0">


          <form action="<?curlPOST($fields)?>" method="post" >
          <tr>
                <td>
            <label>Phonenumber:</label>
            </td>

                <input  type="hidden" name="lid" id="lid" size="40" value= "<?=$lid?>"> 
                <input  type="hidden" name="list_id" id="list_id" size="40" value= "<?=$list_id?>"> 
                <td >
                <input  type="text" name="phonenumber" id="phonenumber"  size="40" value= "<?=$phone?>"> <br>
                </td>
            </tr>
            <tr align="center">
                <td>

            <input type="submit" name="btnSubmit" value="Submit Data"> 
            </td>
            </tr>

          </form>

</table>







</body>
</html>
?>

I am posting these three values to mailit4.php using curl post. When i click submit it calls that function and run mailit4.php script which is on the other server. Everything is working fine but when i am trying to redirect my page to thanks.html which is on this server(where trackit.php is). Its doing nothing. Can anyone please how i can redirect to thanks.html

here is my code for mailit4.php

<?php

$db = mysqli_connect('localhost', 'example', 'example', 'example');


$lid = $_POST['lid'];
$list_id = $_POST['list_id'];
$phone=$_POST['phone'];

$sql = "SELECT list_id FROM exampleWHERE lead_id=$lid";
$result = mysqli_query($db,$sql);
$value = mysqli_fetch_row($result);
if ($value[0] != $list_id){
$body = "Leadid " . $lid . " : " . $list_id . "

";
$subject = "Zombie Drip:" . $lid . " : " . $list_id;

mail("example@gmail.com", $subject, $body); 
}
$query = mysqli_query($db, "UPDATE example SET list_id=$list_id,called_since_last_reset='N',phone_number=$phone WHERE lead_id=$lid");
if($query){
header('Location: http://example.com/thanks.html/');
exit;
} 
else{

$body = "Leadid " . $lid . " : " . $list_id . "

";
$subject = "It could not update the example due to some reasons " .$query ;
mail("example@gmail.com", $subject, $body);




}

?>

Thanks

  • 写回答

2条回答 默认 最新

  • dongzhuo1958 2016-02-26 19:59
    关注

    This code is not working perfectly as <form action="<?curlPOST($fields)?>" method="post" > is wrong.

    you are not echoing curlPOST($fields) so the when you are loading this page, your mailit4.php is loading one time and the action of this form are set to self like <form action="" method="post" > and you are thinking your code is working as each time you submit it calles this page as well as mailit4.php. your correct action will be <form action="" method="post" > and put this code after calling function curlPOST if(!empty($_POST)) curlPOST($fields);

    another problem in your code is,

    $lid = $_GET["lid"];
    $list_id = $_GET["list_id"];
    $phone=$_GET["phone"];
    

    method of your form is post and you are trying to catch this data by get

    in your mailit4.php you are using mail means you are sending data to somewhere that means, data flushed already, and then you are calling header. instate use file_get_contents() to get contents of thanks.html as this script will not out anything to your browser.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)