Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recursive calling of $HTTP_POST_VARS 1

Status
Not open for further replies.

Zbi

Programmer
Aug 26, 2002
9
0
0
US
My script consists of SELECT form and bellow data processing output depending on selected and submitted item.

Like this:

Select name:
<Joe> {other choices are <Peter> <Jane>}
[OK]
Joe’s age is 22.

The age is taken from database. For first run, the first item is selected, that’s fine.

For the first run the firsts item from SELECT is selected and processed.

Next time I choose <Jane>. Result:

Select name:
<Jane>
[OK]
Jane’s age is 31.

I am recursively calling the same php script and passing selected value through $HTTP_POST_VARS. Second and next runs, all is OKay. $HTTP_POST_VARS[name] is filled with name. This is dynamically used for SELECT for “selected item” and for database query.

The only problem is a warning during the first run when $HTTP_POST_VARS is not initialized but it is used for creating form SELECT and db query.
Can I manually found out if $HTTP_POST_VARS[name] exists isset()?
If it is not initialised/existing, can I set up default: $HTTP_POST_VARS[name] = “Joe” for the first run?

Thank you for advice in advance.
 
You should be able to set the default as Joe for you first run. Just set the $HTTP_POST_VARS[name] = “Joe” towards the start of your script. If you set this before you call the var, if it has nothing to collect except the defualt value you have set.


That's not the greatest explaination but it'll give you a clue no doubt..
 
<?php

$test=1;

$test=$_REQUEST['test'];

echo '$test';

echo'<form method=POST action=&quot;'.$PHP_SELF.'&quot;>';
echo '<input type=text name=test></input>';
echo '<input type=submit name=submit>';

?>

This should set the default value of test to 1 untill the user inputs a new test value and submits it...

I haven't tested this but I think it correct...
 
Suggestion:

Check for the existence of the array key that holds the variable. On the very first run there is no key since nothing is in $_POST.
Code:
$_POST['name'] = array_key_exists('name',$_POST)? $_POST['name']:'Joe';

If you use a version of PHP that only uses $HTTP_POST_VARS it's time to upgrade. $_POST is shorter and the long form is deprecated.
 
Thank you!

array_key_exists exactly solves my problem. Now the script works as I wish.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top