Log in

View Full Version : Fill in Fields with PHP



tech_support
09-28-2006, 08:03 AM
Hi,

How do you fill in fields automatically with PHP?

Example:



<a href="fillIn.php?name=Peter">Fill in name with Peter</a>


and then when you go into the fillIn.php page there's a form with a field called 'name' and the value "Peter"

Thanks,


Peter

djr33
09-28-2006, 08:07 AM
That's all you need on the first page with the link.

The next page, fillIn.php should be like this:

<input .... value="<?php echo $_GET['name']; ?>">

You can put that php code anywhere on the page and it will output "Peter" into the html. It will also output "" (blank string) if there is no value, so just like it wasn't there.


Note that you need php installed on the server and the second page (or both) to be of the extension .php as opposed to .htm etc.

tech_support
09-28-2006, 08:09 AM
Thanks!

And do you know how to do that for drop-down select menus? Like select the right one.

(Just noticed the "Phrase Center" in Quick Reply Section)

djr33
09-28-2006, 08:32 AM
"...like the right one."
Hmmm? Right? as opposed to left? As in correct? or something else?

With php and get variables, you can do that fairly easily.
The method depends on exactly what you want to happen.
I can see two options:
1. you create/add to a select menu. (Do this as you did the last one... just output, with php, a value into the select. Note that you can use strings, like "text".$var."moretext.$anothervar... and then you have your html)
2. If you mean send the chosen item from a select menu by php, then simply use ifs...
here's an example of one if:
<select>
.....
<option value="CA" <?php
if ($_GET['item'] == "CA") {
echo "selected";
}
?>>California</option>
.....
</select>
Do that for each option in the select, and it will go to the one in the URL.

The above, assuming the url was ?item=CA, would output:
<select>
<option value="CA" selected>California</option>
</select>


If the above php looked complex, it could be rewritten as:
<select>
.....
<option value="CA" <?php if ($_GET['item'] == "CA") { echo "selected"; } ?>>California</option>
.....
</select>

tech_support
09-28-2006, 08:54 AM
"...like the right one."
Hmmm? Right? as opposed to left? As in correct? or something else?


As in the correct one.

djr33
09-28-2006, 09:42 AM
k.
The above work for you?

Twey
09-28-2006, 05:50 PM
it will also output "" (blank string) if there is no value, so just like it wasn't there.Yes, but it will also throw a NOTICE. You should really check:
<?php echo(isset($_GET['name']) ? $variable : ''); ?>
<select>
.....
<option value="CA" <?php
if ($_GET['item'] == "CA") {
echo "selected";
}
?>>California</option>
.....
</select>Preferred would be:
<select>
<option value="CA" <?php
if (isset($_GET['item']) && $_GET['item'] === 'CA')
echo 'selected="selected"';
?>>California</option>
</select>

djr33
09-29-2006, 05:29 AM
$_GET['item'] === 'CA'
Or, just ==.... no need for the "exactly equal to" as there isn't much else that would be kinda equal to "CA" ;) But.. sure.

Ah, well, one of the servers I've been working on has a nice habit of not showing errors (which is out of my control, unfortunately, shoot me a PM, twey, if there's a way to do it in the script... I've tried, but failed...), so that's likely why I haven't bothered checking if it has no value.
good point, though.

Twey
09-29-2006, 08:55 AM
error_reporting(E_ALL) should do it.
no need for the "exactly equal to" as there isn't much else that would be kinda equal to "CA"Which is exactly why === should be used :) Since it doesn't have to convert the operands to a dozen different types before deciding they're not equal, === should be more efficient than ==.

djr33
09-29-2006, 09:05 AM
In what sense of efficient?
Personally, I'm happy sticking with my lazy two equals signs.
I suppose, though, that you're talking about server run time.

tech_support
10-02-2006, 05:41 AM
Im happy with two equal signs too.

Anyway, thanks for your help guys!

Prineshaz
10-05-2006, 01:44 AM
I learned a lot from this thread. Thanks for the insight guys :)