Forum Moderators: coopster

Message Too Old, No Replies

Submitting a form from outside the <form> tags

         

princeofvegas

7:01 pm on Jul 3, 2010 (gmt 0)

10+ Year Member



Ok so here is what I am trying to accomplish. I want to submit a form from outside the form tags. This is thew workflow if it.

At the top of the page of listings, a user fills out there address.

Now in the listings, within each listing there is a link that says "Get Directions". This link goes to get_directions.php?listing_id=105

I want that link to also take information that was entered into the form and pass those variables along with the listing_id to the get_directions.php page. Is this at all possible?

eelixduppy

3:28 am on Jul 4, 2010 (gmt 0)



When you post the form, you can add the listing_id to the POSTed variables, perhaps using a hidden text input.


<form action="get_directions.php" method="post">
<input type="text" name="listing_id" value="<?php echo $_GET['listing_id']; ?> " />
<input type="text" name="address" />
</form>


Something along those lines, no? Or do you have to send the listing_id as a URI parameter?

Readie

10:27 am on Jul 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



JavaScript is an option, when the link is pressed you call a function which changes the form's action attribute to "/get_directions.php?listing_id=105" (or whatever id for that link) and submit the form.

Other than with JavaScript I don't think you can do this exactly the way you want it though.

----

Of course, another option is is changing the links to to submit buttons, and having a big form over everything. Each submit button has a different name, like:

<input type="submit" value="Get Directions" name="subbut_105">


And you post this data to a script to handle that info.
<?php

if(count($_POST)) {
$hidden = '';
foreach($_POST as $key => $value) {
if(substr($key, 0, 6) === "subbut") {
$num = end(explode("_", $key));
}
$hidden .= '<input type="hidden" name="' . $key . '" value="' . htmlentities($value, ENT_QUOTES) . '">';
}
}

?><form method="post" action="/get_directions.php?listing_id=<?php=$num ?>">
<?php=$hidden ?>
<input type="submit" value="View search results">
</form>
But I personally think the JS option is more elegant. Could tie the two together though: use JavaScript to hide (and disable) the submit buttons and replace them with links to call the redirect function. Then you have your functionality and a backup for non JS users.