doujiang1993 2011-04-12 17:13
浏览 120
已采纳

cURL + PHP:提交具有相同表单名称的重复表单的表单

I am trying to POST to a page that has two forms with duplicate name elements. The problem is that one form gets the password value and the other form gets the login value. (I can see this by printing out curl_exec($ch);) I will include my code for the target URL and the formdata. How do I fix this?

// my target url and form data
$target = "http://www.example.com/login";
$formdata = "id=$login&password=$password&Submit=Log In";

Forms:

<form id="login" name="login" method="post" action="login">

            <label for="id">LOGIN ID</label>  <input type="text" value="" name="id" maxlength="50" size="30"><br>
            <label for="password">Password ID</label>  <input type="password" name="password" maxlength="12" size="30">
            <div align="center"><button class="siteSprite signInSm" value="Log In" name="Submit" type="submit"></button></div>
</form>


<form section="login" id="loginform" name="loginform" action="http://www.example.com/login" method="post">
<input type="text" size="20" value=" Log-in" onfocus="this.value=''" name="id"></td>
<input type="password" value="Password" maxlength="15" size="12" onfocus="this.value=''" name="password">
<input type="submit" class="siteSprite signInSm" value="Sign-In">
</form>
  • 写回答

2条回答 默认 最新

  • duanhan8757 2011-04-12 17:19
    关注

    You'll have to do something to indicate which of the two forms got submitted. You can either submit a field with the same name but different values in each one, or use the submit button:

    <form ...>
        <input type="hidden" name="whichform" value="1" />
        <input type="submit" name="Submit" value="form 1" />
    </form>
    
    <form ...>
        <input type="hidden" name="whichform" value="2" />
        <input type="submit" name="Submit" value="form 2" />
    </form>
    

    and then

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if (($_POST['Submit'] == 'form 1') || ($_POST['whichform'] == '1')) {
            .... handle form #1 ....
        }
        if (($_POST['Submit'] == 'form 2') || ($_POST['whichform'] == '2')) {
            .... handle form #1 ....
        }
    

    using either method works the same, just pick the one that makes most sense/is easiest and go from there.

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

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛