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

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条)

报告相同问题?