简体   繁体   中英

Submit form values without loading a new page

I have a form which i am using to pass values to another page and its working .

Is there anyway to do this without opening a page to which my form action is set?

I think it can be done using ajax and javascript

echo "<form name=\"android\"  method=\"post\" action=\"http://www.example.com\" target=\"_blank\">";
echo "<input type=\"hidden\" name=\"appid\" value=\"$appid\" />";
echo "<input type=\"hidden\" name=\"pushmessage\" value=\"$pushmessage\" />";
echo "<input type=\"hidden\" name=\"username\" value=\"$user\" />";
echo "<input type=\"hidden\" name=\"pass\" value=\"$pass\" />";
echo  "<input type=\"hidden\" name=\"publisherid\" value=\"createcoolapps\" />";
//echo "<input type=\"submit\" value=\"Push\" name=\"sub\" />";
echo "</form>";
echo "<script> document.forms[0].submit();</script>";

Try this:

$url = 'http://www.example.com/';
$fields = array(
    'appid' => urlencode($appid),
    'pushmessage' => urlencode($pushmessage),
    'username' => urlencode($user),
    'pass' => urlencode($institution),
    'publisherid' => urlencode("createcoolapps")
);

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

Yes, this can be done using Ajax, as you think. By far the easiest way to do this would be to use jQuery's AJAX libraries (since if nothing else, who doesn't use jQuery now) from here: http://api.jquery.com/jQuery.ajax/

An example might be as follows (untested, but somewhat right):

<form action="mypage.php" method="post"> <!-- Args for when JS is disabled -->
    <input type="text" id="form_text_1" value="" />
    <input type="submit" id="form_submit" />
</form>

<script>
    $('#form_submit').click(function() {
        jquery.ajax("mypage.php", {
            type: "post",
            data: {
                text1: $('#form_text_1').val()
            }
        });
        return false; // Stop the form being submitted in the foreground when JS works
    });
</script>

Of course, adapt that to your needs.

This is very easy if you can include the jQuery library: http://www.jquery.com

If you use jQuery and the code you have above, you can do the following:

$('input[name="sub"]').click( function() {
   $.post($(this).parents('form').attr('action'), $(this).parents('form).serialize());
   return false;
} );

This intercepts the click of the submit named "sub", then performs and AJAX post to the action of the form.

You would do well to look at your browser console to track the data that is posted and spot any issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM