form that calls itself

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
joshuamichaelsanders
Forum Newbie
Posts: 14
Joined: Thu Jul 21, 2011 4:48 pm

form that calls itself

Post 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?
Last edited by Benjamin on Fri Aug 05, 2011 3:38 am, edited 1 time in total.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: form that calls itself

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: form that calls itself

Post by Jonah Bron »

Oops, I think you meant isset, not is_set :)
joshuamichaelsanders
Forum Newbie
Posts: 14
Joined: Thu Jul 21, 2011 4:48 pm

Re: form that calls itself

Post 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.
joshuamichaelsanders
Forum Newbie
Posts: 14
Joined: Thu Jul 21, 2011 4:48 pm

Re: form that calls itself

Post 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.
Last edited by Benjamin on Fri Aug 05, 2011 3:38 am, edited 1 time in total.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: form that calls itself

Post 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 :)
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: form that calls itself

Post by califdon »

Jonah Bron wrote:Oops, I think you meant isset, not is_set :)
:o You're right, of course! Thanks.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: form that calls itself

Post 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.
Last edited by califdon on Fri Jul 22, 2011 2:27 pm, edited 2 times in total.
Reason: To add my afterthought.
joshuamichaelsanders
Forum Newbie
Posts: 14
Joined: Thu Jul 21, 2011 4:48 pm

Re: form that calls itself

Post by joshuamichaelsanders »

There's a manual? Who knew!?
/sarc
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: form that calls itself

Post by califdon »

joshuamichaelsanders wrote:There's a manual? Who knew!?
/sarc
:lol:
adbertram
Forum Newbie
Posts: 10
Joined: Sun Jul 31, 2011 10:18 pm

Re: form that calls itself

Post by adbertram »

I've used the PHP_SELF global as the form action to call itself.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: form that calls itself

Post 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
Last edited by califdon on Mon Aug 01, 2011 1:55 pm, edited 1 time in total.
Reason: Added URL reference
Post Reply