Jump to content

inserting php variable into function attribute of form tag


flunn

Recommended Posts

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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']

 

 

 

 

Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.