开发者

submitting a form remotely (from a different page)

I have looked through related posts, but am not expert enough to tell if any response I've read applies to my situation.

I have a rather complex form -- the form is auto-generated on the fly from a database, and works -- fill it out, submit, and everything is fine.

Now, this form is basically (but not quite) an order form -- so imagine being able to fill in up to ten 10 different items you want (quantity, etc), hitting submit, and getting a receipt. For purposes of this example, this means there is a FORM page that sends the user to a RECEIPT page when they hit submit.

What I want to do is allow users to not have to deal with this form at all in certain circumstances. For example, if all they are doing is getting a widget (and nothing else), I want them to be able to click a 'buy widget' button on the widget profile page and go directly to the RECEIPT page, skipping the form entirely (since they don't care about anything but widgets).

So, basically, I want the Widget page to be able to call the form page pre-filled, AND pre-submitted - with the Widget related dat开发者_StackOverflowa filled out. (I apologize if I am using the wrong terminology -- this was built by someone else, and I was unfortunately left doing some cleanup/finishing touches but am not an expert in PHP).

Any advice? I'd really LOVE to keep the data OUT of the URL if at all possible, though if that increases complexity a hundred fold I'd go with a URL solution.

Thank you.


EDIT: (Including code from comment on @AJ's answer:)

ok, i think i'm part of the way there. i have the following code:

print '<form name="input" action="order.php" >';
$field_name = 'widget';
/* fill in */
$field_value = '10';
/* fill in */
echo '<input type="hidden" name="'.$field_name.'" value="'.$field_value.'">';
echo '<input type="submit" value="submit" name="submit" />';
print '</form>';

but this is NOT triggering the following in the form:

if(isset($_POST['submit'])) { print 'triggering'; } else { print 'not triggering'; } 


The "Buy Widget" button, on whatever page it is implemented, when clicked will need to be able to send a POST request to the form action script for your current form. There are a couple of options for this:

  • Deliver a "hidden" version of your existing form to integrators adding your "Buy Widget" button...it essentially implements all of the same fields, but with the input types set to hidden and all of the values filled in.

  • Use something like jQuery.ajax() to send the same type of request, but using javascript instead of hidden form fields


You can make your own forms with hidden elements (same as the real form, with your own values) that posts to the same place as the real form.


A form is just a method of gathering data. Behind the scenes, when a form is submitted, the browser gathers all the data in the form and submits it to your server as either a POST or a GET request.

To simulate the form submission, you simply need to assemble all of the data in a way that your server will recognize it.

The easy way to do it is like this:

<?php
    header( "Location: http://yourdomain.com/checkout.php?<data>" );
?>

Replace with your form data like this: If you have a form field named 'city' and the value is 'MyTown', and a 'zipcode' field with a value of '12345' will be:

city=MyTown&zipcode=12345

Your code will then be:

header( "Location: http://yourdomain.com/checkout.php?city=MyTown&zipcode=12345" );

In this way, you can submit your form data to the server without ever presenting a form to the user.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜