Page 1 of 1

form that calls itself

Posted: Thu Jul 21, 2011 4:54 pm
by joshuamichaelsanders
OK, that is worded very poorly. I have a page, ensemble.php, that should start with a form with a list of ensembles. Once a user picks a particular ensemble, I would like to have the form call ensemble.php with the ensemble name and then produce a roster based on that variable. I just can't seem to get it right. I would think it would be something like this.

Code: Select all

$ensemble_code=$_POST['ensemble'];

if ($ensemble_code == '0') {;
	echo $varname;
	echo "<form action='ensemble.php' method='post'>";
	echo "Which Ensemble?";
	echo "<select name='ensemble'>";
	echo "<option value='1'>Ensemble 1</option>";
	echo "<option value='2'>Ensemble 2</option>";
	echo "<input type='submit' value='submit'>";
	echo "</select>";
	echo "</form>";}
else  
    build table from SQL query
Is this correct?

Re: form that calls itself

Posted: Thu Jul 21, 2011 8:09 pm
by califdon
joshuamichaelsanders wrote:OK, that is worded very poorly. I have a page, ensemble.php, that should start with a form with a list of ensembles. Once a user picks a particular ensemble, I would like to have the form call ensemble.php with the ensemble name and then produce a roster based on that variable. I just can't seem to get it right. I would think it would be something like this.

$ensemble_code=$_POST['ensemble'];

if ($ensemble_code == '0') {;
echo $varname;
echo "<form action='ensemble.php' method='post'>";
echo "Which Ensemble?";
echo "<select name='ensemble'>";
echo "<option value='1'>Ensemble 1</option>";
echo "<option value='2'>Ensemble 2</option>";
echo "<input type='submit' value='submit'>";
echo "</select>";
echo "</form>";}
else
build table from SQL query

Is this correct?
Well, you're sort of on the right track, although you have some errors there, and you haven't gotten to the part that will do the work. First, your code tests whether $ensemble_code equals zero, but it can never equal zero, because the only two values it can ever have would be "1" or "2". Your test should be on whether $ensemble_code has been set or not. So your test should be:

Code: Select all

if(!is_set($ensemble_code)) {
followed by the display of your form. You have a semicolon following the opening curly bracket, which should not be there. Then, what did you have in mind about echoing $varname? Finally, in the else block is where nearly everything happens; you will have a value of either "1" or "2" stored in $ensemble_code, so you will use that to form your query, after connecting to your database, then, as you indicated, build your html table in the while loop that fetches the rows from the database table.

Re: form that calls itself

Posted: Thu Jul 21, 2011 11:19 pm
by Jonah Bron
Oops, I think you meant isset, not is_set :)

Re: form that calls itself

Posted: Fri Jul 22, 2011 11:30 am
by joshuamichaelsanders
The echo $varname was just some testing I was doing earlier that never got removed and the else statement was simplified so the board's moderators wouldn't think I was posting a how to question in a non how to question forum. :) I had figured out the building of the table just couldn't figure out how to test for $ensemble_code. Thanks.

Re: form that calls itself

Posted: Fri Jul 22, 2011 11:44 am
by joshuamichaelsanders
So I should have something like this right?

Code: Select all

$ensemble_code=$_POST['ensemble'];

if(!isset($ensemble_code)) {
    echo "<form>";
}
else {
    echo "<table>";
}
I'm getting an error in the $_POST statement of an undefined variable.

Re: form that calls itself

Posted: Fri Jul 22, 2011 12:43 pm
by Jonah Bron
No, more like this:

Code: Select all

if (isset($_POST['ensemble'])) {
    $ensemble = $_POST['ensemble'];
    // output table
} else {
    // output form
}
If you read in the manual, you'll see that isset() checks if a variable or array index has been defined. $_POST contains all data passed in the HTTP request; you just have to make sure the data you're looking for is there.

Also, remember to use the PHP Code button when posting code here on the forum, it makes it easier to read :)

Re: form that calls itself

Posted: Fri Jul 22, 2011 2:17 pm
by califdon
Jonah Bron wrote:Oops, I think you meant isset, not is_set :)
:o You're right, of course! Thanks.

Re: form that calls itself

Posted: Fri Jul 22, 2011 2:24 pm
by califdon
I agree, Jonah's example is the better approach, although what you posted in your last post should also work. But now is the best time to learn good practices.

[Edit:] On second thought, it might not, under some conditions. An alternate approach would be to use empty(), which tests for a variable being either undefined OR zero OR an empty string.

Re: form that calls itself

Posted: Fri Jul 22, 2011 3:03 pm
by joshuamichaelsanders
There's a manual? Who knew!?
/sarc

Re: form that calls itself

Posted: Fri Jul 22, 2011 3:56 pm
by califdon
joshuamichaelsanders wrote:There's a manual? Who knew!?
/sarc
:lol:

Re: form that calls itself

Posted: Mon Aug 01, 2011 11:44 am
by adbertram
I've used the PHP_SELF global as the form action to call itself.

Re: form that calls itself

Posted: Mon Aug 01, 2011 1:52 pm
by califdon
adbertram wrote:I've used the PHP_SELF global as the form action to call itself.
You can do that, or you can hard-code the script name, or you can just use action="", which is interpreted as meaning "this script". Just don't omit the action= attribute entirely. See http://stackoverflow.com/questions/1131 ... ck-to-self