0

For the past few days, I've been wrecking my brain trying to get a php script to submit a form in PHP with CURL.

This is the source code of the site:

<form name="webform" action="mailto.php" method="post" onsubmit="return validateform(webform);">
<input type="hidden" name="submit" value="1">
<input type="hidden" name="name1" value="244959">
<input type="hidden" name="name2" value="<? echo $dogname; ?>">
<tr>
<td><font face="verdana" size="-1">Your Name: </td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td><font face="verdana" size="-1">Your Email Address: </td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td><font face="verdana" size="-1">Confirm Your Email Address: </td>
<td><input type="text" name="confirm_email"></td>
</tr>
<tr>
<td valign="top"><font face="verdana" size="-1">Your Message:</td>
<td><textarea name="comments" rows="8" cols="35"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><img src="captcha.php"></td>
</tr>
<tr>
<td><input type="text" name="vercode" /></td>
</tr>
<tr>
<td><input type="submit" class=button name="Send" value="submit">
<br>
<br></td>
</tr>
</form>
</table>

This is the code for the submission script:

function postform($id){
grab_image('path to/captcha.php', 'captcha.jpg');

$image = 'captcha.jpg';

$client = new DeathByCaptcha_SocketClient("user", "password");


if ($captcha = $client->decode($image, 10)) {

echo $captcha['text'] . "\n";
$url = "path to page.php";

$data = array();
$data['submit']='1';
$data['name1']=$id;
$data['name2']="name2";
$data['name']='MyName';
$data['email']='[email protected]';
$data['confirm_email']='[email protected]';
$data['comments']='Success Posting This Form';
$data['vercode']=$captcha['text'];

$post_str = '';

foreach($data as $key=>$value){
$post_str .= $key.'='.urlencode($value).'&';
}

$post_str = substr($post_str, 0, -1);


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to 
curl_setopt($ch, CURLOPT_POST, TRUE); // set POST method 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE); // return into a variable 

$result = curl_exec($ch); // run the whole process
curl_close($ch); 
echo $result; 
}
}
postform("page number");

Once I execute the code however, I get back as a result: "Security code missing. Please go back and try again."

Been tinkering around with this for a few days now and I'm a little stumped on how to proceed. Any help would be immensely appreciated!

6
  • In your form action, is mailto.php the same as http://www.breeders.net/mailto.php? Commented Mar 17, 2014 at 18:51
  • The form action is "mailto.php". The actual page on which the form is located however is : .../mailto.php??id=244959 Commented Mar 17, 2014 at 19:02
  • You can simplify this by having action='mail_curl.php' then on mail_curl.php, we can have the curl code to submit the data to ...../mailto.php. Since in your html page you have the breederid, we dont need to pass it to ...../mailto.php?id=244959 Commented Mar 17, 2014 at 19:07
  • Unless I'm miss-understanding you, I do not have access to modifying the properties of the form as it is an external site. I'm simply trying to have a the second php script submit successfully submit the data through the form. Commented Mar 17, 2014 at 19:31
  • Which form do you not have access to?, because if it is the php page, it will be hard to implement the curl. Commented Mar 17, 2014 at 19:33

1 Answer 1

1

The Html Page:

<!Doctype Html>
<html>
<head>
<title></title>
<script src="/qlib/qforms.js"></script>
</head>

<script language="JavaScript">
// set the path to the qForms directory
qFormAPI.setLibraryPath("/qlib/");
// this loads all the default libraries
qFormAPI.include("*");

</script>
<h2><img src="../../templates/images/mail-to/email.gif" width="60" height="48" alt="email" /><br />
Email Breeder</h2>
<p>Use the form below to contact Bernese Mountain Dog, Breeder ID 244959 <? echo $to; ?>:
<p><font color=#FF0000 face=verdana size=-2><b><? echo $msg; ?></b></font>
<p>
<table>
<form name="webform" action="mail_curl.php" onsubmit="return validateform(webform);">
<input type="hidden" name="submit" value="1">
<input type="hidden" name="breederid" value="244959">
<input type="hidden" name="dogname" value="<? echo $dogname; ?>">
<tr>
<td><font face="verdana" size="-1">Your Name: </td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td><font face="verdana" size="-1">Your Email Address: </td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td><font face="verdana" size="-1">Confirm Your Email Address: </td>
<td><input type="text" name="confirm_email"></td>
</tr>
<tr>
<td valign="top"><font face="verdana" size="-1">Your Message:</td>
<td><textarea name="comments" rows="8" cols="35"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><img src="captcha.php"></td>
</tr>
<tr>
<td><input type="text" name="vercode" /></td>
</tr>
<tr>
<td><input type="submit" class=button name="Send" value="Send Email to Breeder">
<br>
<br></td>
</tr>
</form>
</table>
<script language="JavaScript">
// initialize the qForm object
objForm = new qForm("webform");

// make these fields required
objForm.required("name, email, confirm_email, comments, vercode");
objForm.email.validateEmail();
objForm.email.validateSame("confirm_email");

</script>
</body>
</html>

mail_curl.php:

 <?php

$url = 'path to mailto.php';

 if(isset($_GET)){
 $breederid = $_GET['breederid'];
 $dogname= $_GET['dogname'];
 $name= $_GET['name'];
 $email= $_GET['email'];
 $confirm_email= $_GET['confirm_email'];
 $comments= $_GET['comments'];
 $vercode= $_GET['vercode'];


 $fields = array(
 'breederid ' =>     urlencode($breederid),
 'dogname' =>     urlencode($dogname),
 'name' =>     urlencode($name),
 'email' =>     urlencode($email),
 'confirm_email' =>     urlencode($confirm_email),
 'comments' =>     urlencode($comments),
 'vercode' =>     urlencode($vercode)   
 );

foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

$ch = curl_init();

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

$result = curl_exec($ch);

curl_close($ch);
}
else
{
echo 'No data to submit';
}
?>

mailto.php:

 <?php
 $breederid = $_POST['breederid'];
 $dogname= $_POST['dogname'];
 $name= $_POST['name'];
 $email= $_POST['email'];
 $confirm_email= $_POST['confirm_email'];
 $comments= $_POST['comments'];
 $vercode= $_POST['vercode'];

 //mysqli insert statement
 //$con is the mysqli database connection
 $result = mysqli_query($con,"INSERT INTO mails (breederid, dogname, name, email, comments, vercode) 
 VALUES ('$breederid', '$dogname', '$name', '$email', '$comments', '$vercode')");       

 if($result){
 echo 'Data was inserted successfully';
 } else {
 echo 'There was an error inserting the data';
 }

   ?>
Sign up to request clarification or add additional context in comments.

2 Comments

The html form is on an external server, so I can't change the action on it. Or can I manipulate it? If I do, will I still be able to get a valid captcha that will work when submitting it back?
Provided you know the action , you can manipulate the code in the action php page to resemble the one i posted above on mail_curl.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.