2

I have two pages:

one.php

<?php if($_POST && $_POST['captcha'] == 'thisisrandom'){
    echo 'KEY = ' . uniqid();
} ?>
<form action="one.php" method="POST">
    NAME: <input type="text" name="name"> <br />
    CAPTCHA: <input type="text" name="captcha"> <br />

    <input type="submit">
</form>

two.php

<?php
function get_web_page($url)
{
        //echo "curl:url<pre>".$url."</pre><BR>";
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "spider", // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 15,      // timeout on connect
        CURLOPT_TIMEOUT        => 15,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects

    );

    $ch      = curl_init($url);
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch,CURLINFO_EFFECTIVE_URL );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;

    //change errmsg here to errno
    if ($errmsg)
    {
        echo "CURL:".$errmsg."<BR>";
    }
    return $content;
}
print_r(get_web_page('http://localhost/one.php'));
?>

and i open page two.php. This show me form from one.php. This is ok, but if i submit form then this redirect me to one.php. How can i fill in this form on page two.php, submit form and receive data on page two.php from one.php without redirect?

1 Answers1

0

You can make an AJAX Post call via Jquery to prevent redirecting when form is submitted.

First remove action attribute from form element. HTML should look like this:

<form method="POST">
    <label for="name">NAME:</label><input type="text" name="name" />
    <label for="captcha">CAPTCHA:</label><input type="text" name="captcha" />
    <input type="submit" />
</form>

Second, listen form submit button in one.php and make ajax post call when it is clicked. You can use jquery serialize function to create a data set from input values. And as one.php and two.php are not on the same domain, you should use jquery getjson option to get data.

$(document).ready(function() {
    $('form input[type=submit]').click(function(event) {
        event.preventDefault();
        var formData = $(this).closest('form').serialize();
        $.ajax({
                type: 'POST',
                url: 'one.php',
                data: formData,
                dataType: 'json',
                success: function(data) {
                   // do your work here
                }
            });
     });
});

As the last thing to do, you need to format your php output by using php function json_encode. So one.php should look like this:

<?php if($_POST && $_POST['captcha'] == 'thisisrandom'){
    $arr["key"]= "KEY";
    $arr["value"]= uniqid();
    echo json_encode($arr);
} ?>
halilb
  • 4,055
  • 1
  • 23
  • 31
  • well, that information change my answer! check out this question to use JSONP: http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain – halilb Nov 13 '12 at 14:31
  • hey @JoeChroock, i improved my answer for cross domain issue, try it. – halilb Nov 13 '12 at 21:35