Harry
Harry

Reputation: 319

Pass a PHP Variable to the URL from a form

Im trying to pass a php variable, which is created from a form, to a url/ for use on another page. I might be going about it the completely wrong way, Im not sure.

//THE FORM

<form><input style="height:35px; width:520px; font-family:Arial, Helvetica, sans-serif; font-size:24px; text-align:center;" type="text" name="fbid" /></form>

//PHP TO GET THE TEXT FROM THE TEXT INPUT BOX

<?php
$id = $_GET['fbid'];
?>

//THE IMAGE/BUTTON THAT SENDS THE USER TO THE VARIABLE URL

<img src="images/FacebookSite_32.gif" width="333" height="59" alt="" onclick="location.href='page.php?id=<?php echo $id; ?>'">

So, I'm trying to get the text that was typed in the text input box, and pass it to the URL, for use on another page. Thanks for any help!

Upvotes: 0

Views: 2214

Answers (3)

Gareth
Gareth

Reputation: 5719

PHP runs on the server, so by the time the user sees the form the PHP will have already run and put an empty string into your javascript.

You might be better off just using javascript for this:

<form>
    <input style="some style stuff here..." type="text" name="fbid" />
</form>

<img src="images/FacebookSite_32.gif" width="333" height="59"
     onclick="location.href='page.php?id='+document.forms[0].elements[0].value" />

This code assumes that there is only one form on the page and that the value you want is in the first (0th) element.

Upvotes: 1

Tom
Tom

Reputation: 3034

Try this:

//THE FORM

<form action="" method="GET">
<input style="height:35px; width:520px; font-family:Arial, Helvetica, sans-serif; font-size:24px; text-align:center;" type="text" name="fbid" />
<input type="submit">
</form>

//PHP TO GET THE TEXT FROM THE TEXT INPUT BOX

<?php
$id = (int)$_GET['fbid'];
?>

//THE IMAGE/BUTTON THAT SENDS THE USER TO THE VARIABLE URL

<img src="images/FacebookSite_32.gif" width="333" height="59" alt="" onclick="location.href='page.php?id=<?php echo $id ?>'">

Upvotes: 0

sudeep cv
sudeep cv

Reputation: 1027

Try this:

   <form action="<?php echo $_SERVER['PHP_SELF']; ?>" methode="GET" > 
  <input style="height:35px; width:520px; font-family:Arial, Helvetica, sans-serif; font-size:24px; text-align:center;" type="text" name="fbid" />
  <input type="submit" value="submit" name="submit" />
    </form>

<?php

if(isset($_GET['fbid'])){
   $id = $_GET['fbid'];
   echo $id;
 }
   ?>

Upvotes: 0

Related Questions