douqiaolong0528 2015-05-05 13:56
浏览 64
已采纳

PHP:在PHP中使用Curl将值发送到HTML表单

Excuse me for my question ... This has been discussed many times but i didn't notice... :( we have this html form :

<!DOCTYPE html>
<html>
<head>
<title>Comment</title>
</head>
<body>
<fieldset>
<legend align="center">Comments</legend>
<form action="server.php" method="post">
Name: <input type="text" name="name" /><br><br>
Email: <input type="email" name="email" /><br><br>
Web: <input type="url" name="web" /><br><br>
Message:<br>
<textarea name="comment"></textarea><br><br>
<input type="submit" name="send" value="send" />
</form>
</fieldset>
</body>
</html>

and this php file:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$web = $_POST['web'];
$comment = nl2br($_POST['comment']);
//database variables;
$host = "localhost";
$user = "root";
$password = "";
$db = "ashiyane";
//connect to mysql & insert data into table;
$connection = mysql_connect($host,$user,$password);
if ($connection){
    echo "Connected Successfully...";
    $select_db = mysql_select_db($db);
    if ($select_db && $name !== "" && $web !=="" && $email !=="" && $comment !==""){
        echo "<br>Database Selected...";
        $run_query = mysql_query("INSERT INTO comments (name,email,web,comment) VALUES              ('$name','$email','$web','$comment')");
        if ($run_query){
            echo "<br>Data Have Been Insert ...<br>";
        }else{
            echo "<br>Inserting Data Failed!";
        }
    }else{
        echo "<br>Database No Selected!";
    }
}else{
    echo "<br>Connecting Failed!";
}
//show results;
$show_query = mysql_query("SELECT * FROM comments");
while ($results = mysql_fetch_array($show_query)){
    echo $results['name']." : ".$results['email']." : ".$results['web']." : ".$results['comment']."<br>====================<br>";
}
?>

Now we want our values to send HTML form with curl functions ... please help me ... Once again I apologize for asking this question... :) Thanks All Friend !! ;)

  • 写回答

1条回答 默认 最新

  • dongtiao0279 2015-05-05 14:33
    关注

    Assuming curl is installed and enabled on the server, you should be able to do something like:

    <?php
    $post_data = array(
        'name' => 'Your Name',
        'email' => 'user@example.com',
        'web' => 'http://www.example.com',
        'comment' => 'your comment',
    )
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL,"http://www.example.com/path/to/comment/post/script");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    
    curl_exec ($ch);
    curl_close ($ch); 
    ?>
    

    Another alternative, and possibly a more flexible one would be to use Guzzle which will handle the http request and response for you.

    As a side note, the script you posted is currently dangerously written, you are directly inserting user provided data into the database without escaping it (e.g. by using mysql_real_escape_string), ideally you should be using a prepared statement via PDO or similar

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集