3

I am trying to automatically fill in a field and submit a form at another server. I do not have any control on the target page and server.

I tried writing running php code with Snoopy class to do the job, but it did not work (description here)

The page that I am trying to fill in and submit automatically is: http://example.com/?page=a and the source of the page is as follows:

<form action="" method="post" class="horizontal-form" role="form" >
<input type="hidden" name="submit_form" value="true" />
<input type="text" name="field_name" class="form-control" value="" >
<button type="submit" class="btn"><i class="icon-ok"></i> Send</button>
</form>

Any idea how I can fill in the "field_name" automatically using a script/php code from my own site and automatically submit this form? I guess I can use CURL to do it as well, but I am novice and don't know how. :(

Thank you for your help.

Community
  • 1
  • 1
cybergeek654
  • 290
  • 1
  • 5
  • 15

2 Answers2

2

You can't fill the form with php since php is a server-side language. You could fill in the form with javascript. There are some frameworks to do that (phantomjs, casperjs).

You can try to post the form data directly using curl in PHP.

<?php

$data = array(
    'submit_form' => 1,
    'field_name' => 'your value here',
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/post-url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$output = curl_exec($ch);
$info = curl_getinfo($ch);

curl_close($ch);

You can find the url by looking in the console of your browser:

form post

Your request might be blocked because it's coming from another server but you can give it a try. If you want to 'mimic' a normal visitor you can start with setting the user agent string, maybe modify the HTTP_REFERER as well.

curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36');
curl_setopt($ch, CURLOPT_REFERER, 'http://www.example.com/the-url-of-the-form');
Gerard
  • 3,108
  • 1
  • 19
  • 21
  • 1
    Thanks for the post. I tried this code, including the useragent and referrer as well. Could not get it to work. Maybe I need to set the cookies?! is this correct syntax to set cookies in CURL? curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: PHPSESSID=cookie1; PHPSESSID2=cookie2; ")); or this one: 4)curl_setopt($ch, CURLOPT_COOKIE, "PHPSESSID=cookie1; PHPSESSID2=cookie2; "); – cybergeek654 Jun 02 '15 at 12:20
  • 1
    What doesn't work exactly, are you able to make the request? What's te response? – Gerard Jun 02 '15 at 12:30
  • 1
    After running the code, the results is the html code of the target page, exactly as it is on the target website. This is not good, because if the POST was successful, the result page should have contained one div section saying that "field_name" was incorrect format or that submission was successful. – cybergeek654 Jun 04 '15 at 08:45
1

$(document).ready(function(){
    $('#form1').submit(ajax);
})
function ajax(){
        $.ajax({
            url : 'inserir.php',
            type : 'POST',
            data : $('form').serialize(),
            success: function(data){
                $('#resultado').html(data);
            }
        });
        return false;
}
...
window.onload=function(){
    setInterval(ajax, 5000);
}
check the link for more JQuery AJAX auto submit form
Community
  • 1
  • 1
Ananta Prasad
  • 3,655
  • 3
  • 23
  • 35