flunn Posted September 27, 2011 Share Posted September 27, 2011 I have limited php skills but I thought it would be easy to write a script that would allow a user to use a html form to go from one page to another. I thought I could do this by simply inserting a php variable into the function tag but after many attempts I've had to give up. Here is my most recent attempt. <?php print '<form action= "flash_card_'.$_POST['number'].'.php" method= "post">'; print '<input type= "text" name= "word" value= "'.$_POST['number'].'" />'; print '<input type= "submit" name="submit" value="enter"/>'; print '</form>'; ?> My html editor says the syntax here is OK and the variable in the value attribute of the first input tag does what I want it to do. But the variable in the form tag does nothing. (If the user enters "2" in the text box, for example, the script attempts to send the user to the nonexistent "flash_card_.php" rather than to "flash_card_2.php" which is what I want.) any advice would be much appreciated regards to all from Forrest Quote Link to comment https://forums.phpfreaks.com/topic/247981-inserting-php-variable-into-function-attribute-of-form-tag/ Share on other sites More sharing options...
Pikachu2000 Posted September 27, 2011 Share Posted September 27, 2011 Where is the value of $_POST['number'] supposed to be coming from? It would seem it's empty. Quote Link to comment https://forums.phpfreaks.com/topic/247981-inserting-php-variable-into-function-attribute-of-form-tag/#findComment-1273344 Share on other sites More sharing options...
WebStyles Posted September 27, 2011 Share Posted September 27, 2011 $_POST variables will only exist when the form is actually submitted, not before. Unless you arrived at that page from another page that submitted an element called 'number' (or submitted the page to itself) that will not work. Quote Link to comment https://forums.phpfreaks.com/topic/247981-inserting-php-variable-into-function-attribute-of-form-tag/#findComment-1273346 Share on other sites More sharing options...
dougjohnson Posted September 27, 2011 Share Posted September 27, 2011 The $_POST values need to be set first. So one way to do this would be to have the user enter the number value in the form (without the $_POST's) and then post the data to the same script. (action = formpagename.php) You could then use a redirect after you get the posted 'number' value. If 'number' equaled 1, then redirect to flash_card_1.php. Or something like that. Anyway, your post values don't contain anything they way you have it written now. Quote Link to comment https://forums.phpfreaks.com/topic/247981-inserting-php-variable-into-function-attribute-of-form-tag/#findComment-1273347 Share on other sites More sharing options...
flunn Posted September 27, 2011 Author Share Posted September 27, 2011 I really appreciate the three quick answers I got to my question but I guess I should have made it more clear that the POST values are not empty. I know they're not because when I submit the form, what I enter in the text box reappears there; in other words the form is 'sticky' because of the POST value in the first input tag. Also if I add another line after the form, <?php " print '$_POST['word'];" ?>, what I put into the text box is printed onto the screen. Quote Link to comment https://forums.phpfreaks.com/topic/247981-inserting-php-variable-into-function-attribute-of-form-tag/#findComment-1273363 Share on other sites More sharing options...
PFMaBiSmAd Posted September 27, 2011 Share Posted September 27, 2011 Slightly off topic, but you should not be making a series of separate pages - flash_card_1.php, flash_card_2.php, flash_card_x.php. You should make one page - flash_card.php that has a GET parameter on the end of the URL - flash_card_2.php?card=2 so that you only have one page of code to write and maintain. You can access the value from the URL using $_GET['card'] Quote Link to comment https://forums.phpfreaks.com/topic/247981-inserting-php-variable-into-function-attribute-of-form-tag/#findComment-1273364 Share on other sites More sharing options...
Pikachu2000 Posted September 27, 2011 Share Posted September 27, 2011 I still say $_POST['number'] is empty. Add this and see exactly what is being sent. echo '<pre>'; print_r( $_POST ); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/247981-inserting-php-variable-into-function-attribute-of-form-tag/#findComment-1273368 Share on other sites More sharing options...
flunn Posted September 28, 2011 Author Share Posted September 28, 2011 When I insert the code recommended by Pikachu I get the following: Array ( [number] => hello [submit] => enter ) Doesn't that mean that the POST variable is not empty? As to the suggestion from dougjohnson, it sounds like a good idea but I'm afraid I don't know enough about using "GET" to implement it. I'm going to have to do some more studying I guess. PFMaBiSmAd's idea of using just one card would be good if I really were making flash cards but in fact those words were in the url only by accident. What I'm trying to do is to make a "page turner" like the ones you see in pdf readers etc. (I mean something that allows the user to go to any one of many pages of text by submitting the page number.) Quote Link to comment https://forums.phpfreaks.com/topic/247981-inserting-php-variable-into-function-attribute-of-form-tag/#findComment-1273384 Share on other sites More sharing options...
michaelburt Posted September 28, 2011 Share Posted September 28, 2011 I don't entirely agree with the approach- you could do all this in one page using either conditional $_GET or $_POST statements. But this should work: <?php if (isset($_POST["word"])) { $target = "flash_card_".$_POST["word"].".php"; if (is_file($target)) header("Location: $target"); } ?> <form action="<?php echo basename(__FILE__); ?>" method="post"> <input type="text" name="word" /> <input type="submit" value="Enter" /> </form> Note that the <?php ?> section of the code above MUST be inserted before anything is outputted to the page. On a side note, the reason for the "<?php echo basename(__FILE__); ?>" is because I don't know what the file name is that you're actually working from. Quote Link to comment https://forums.phpfreaks.com/topic/247981-inserting-php-variable-into-function-attribute-of-form-tag/#findComment-1273389 Share on other sites More sharing options...
PFMaBiSmAd Posted September 28, 2011 Share Posted September 28, 2011 something that allows the user to go to any one of many pages of text by submitting the page number That's called pagination. It doesn't matter what the content is that you display on the page, you would still use only one actual page name and pass the desired page number as a get parameter on the end of the URL. See this link - http://www.phpfreaks.com/tutorial/basic-pagination Quote Link to comment https://forums.phpfreaks.com/topic/247981-inserting-php-variable-into-function-attribute-of-form-tag/#findComment-1273391 Share on other sites More sharing options...
flunn Posted September 29, 2011 Author Share Posted September 29, 2011 I decided to try the code offered by michaelburt rather than try the solution suggested by PFMaBiSmAd. I can see that it would be more elegant to use the one-page solution but from my point of view it seemed impracticable because I have no experience with accessing data from urls or with databases — which seem to be necessary according to the tutorial which PFMaBiSmAd links to. michaelburt's code actually contained a couple of minor syntax errors, but after fiddling with it I managed to come up with something that seems to work perfectly. I'll paste it in below. I've included all the code in the file in order to emphasize the importance of putting the php section at the top. It seems that the "header" function must be placed not just before any info being uploaded but at the very beginning, even before the "DOCTYPE." Many thanks to michaelburt, PFMaBiSmAd and all the others who responded to my original posting. Forrest <?php if (isset ($_POST['word'])){ $target="flash_card_".$_POST['word'].".php"; if (is_file ($target)) header ("Location: $target"); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title></title> </head> <body> <form action="<?php echo basename(flash_card_1b) .".php"; ?>" method="post"> <input type="text" name="word" /> <input type="submit" value="Enter" /> </form> <?php print $target; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/247981-inserting-php-variable-into-function-attribute-of-form-tag/#findComment-1274196 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.