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

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>

我正在尝试POST到具有两个具有重复名称元素的表单的页面。 问题是一个表单获取密码值,另一个表单获取登录值。 (我可以通过打印出curl_exec($ ch)来看到这个;)我将包含目标URL和formdata的代码。 我该如何解决这个问题? p>

  //我的目标网址和表单数据
 $ target =“http://www.example.com/login";
$  formdata =“id = $ login&amp; password = $ password&amp; Submit = Log In”; 
  code>  pre> 
 
 

表格: p>

 &lt; form id =“login”name =“login”method =“post”action =“login”&gt; 
 
&lt; label for =“id”&gt; LOGIN ID&lt; / label&gt;  &lt; input type =“text”value =“”name =“id”maxlength =“50”size =“30”&gt;&lt; br&gt; 
&lt; label for =“password”&gt;密码ID&lt; / label&gt  ;  &lt; input type =“password”name =“password”maxlength =“12”size =“30”&gt; 
&lt; div align =“center”&gt;&lt; button class =“siteSprite signInSm”value =“Log 在“name =”提交“type =”提交“&gt;&lt; / button&gt;&lt; / div&gt; 
&lt; / form&gt; 
 
 
&lt; form section =”login“id =”loginform“name =  “loginform”action =“http://www.example.com/login”method =“post”&gt; 
&lt; input type =“text”size =“20”value =“登录”onfocus =“this  .value =''“name =”id“&gt;&lt; / td&gt; 
&lt; input type =”password“value =”Password“maxlength =”15“size =”12“onfocus =”this.value ='  '“name =”password“&gt; 
&lt; input type =”submit“class =”siteSprite signInSm“value =”Sign-In“&gt; 
&lt; / form&gt; 
  code>  pre> \  n  div>

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.

$formdata = "id=$login&password=$password&Submit=Sign-In"; might do the trick; note the fact that the second form has a submit button with a value, and the first form has a <button> which won't send a value (or, maybe, sends a different value via script or something)

I just noticed that the submit button doesn't have a name; try passing it with NO submit parameter, i.e.:

$formdata = "id=$login&password=$password