Jump to content

Write PHP Output Back to Form Textbox


supratwinturbo
 Share

Recommended Posts

Hi All,

 

      I have a web form composing of textboxs for users to enter numbers into. After they press submit, I have a php script that processes the numbers and now echos back the output onto the page. I would like the php output to "echo" into a textbox of the form. How can I do this? Any help would be greatly appreciated.

 

Thank you.

Link to comment
Share on other sites

It depends on how you are generating the page.  If your page comes from the .php file then it is slightly easier as you can just do...

<?php
$numbers1 = $_POST['numbers1'];
$numbers2 = $_POST['numbers2'];
?>
<form method="POST">
<label for="n1">Numbers #1 :</label><input type="text" id="n1" name="numbers1" value="<?php echo $numbers1; ?>" /></br>
<label for="n2">Numbers #2 :</label><input type="text" id="n2" name="numbers2" value="<?php echo $numbers2; ?>" /></br>
</form>

 

Or if you are using templates, you will need to flag up the globals scope, i.e.

<?php
globals $numbers1, $numbers2;

$numbers1 = $_POST['numbers1'];
$numbers2 = $_POST['numbers2'];

include(form.php);
?>

 

But without any more info, that's as far as I can help you, what is it exactly you are trying to do?

Link to comment
Share on other sites

Hi Scotty,

 

Here is an example of what I am trying to do:

 

<head>
<title>Year/Age Calculator</title>
</head>
<body>

<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="GET">
<label for="Age">Age :</label><input type="text" id="n1" name="age" value="<?php echo $age; ?>" /></br><br>
<label for="Year">Year Born :</label><input type="text" id="n2" name="year" value="<?php echo $year; ?>" /></br><br>
<input type="submit" name="YearBorn" value="Find Year Born" />
<input type="submit" name="FindAge" value="Find Your Age" />
</form>

<?php

$age = $_GET["age"];
$year = $_GET["year"];

$YearBorn = $_GET["YearBorn"];
$FindAge = $_GET["FindAge"];

if(isset($YearBorn))
{
$year = 2007 - $age;
echo "Year Born = $year";
}

if(isset($FindAge))
{
$age =  2007 - $year;
echo "Your Age = $age";
}

?>

</body>
</html>

 

So in the above code, there are two textboxes, one for entering a person's Age and one for entering a person's Year of Birth. For example, if I enter 30 into the Age TextBox, I would click on the "Find Year Born" button and the php script will return the year born which will be 2007-30 = 1977. Right now the code just echos the year born onto the screen. I would like it to echo the output into the Year Born Textbox instead. So Basically, if I enter a number into the Age Textbox and Click on "Find Year Born" button, the answer will appear in the Year Born Textbox. On the other hand, if I enter a number into the Year Born Textbox and Click on "Find Your Age" button, the answer will appear in the Age Textbox. I hope I made my question clearer and that you or someone can help me.

 

Thank you.

Link to comment
Share on other sites

use it like this

 


<?php
$age = '';
$year = '';

$FindAge ='';
$YearBorn ='';

if(isset($_GET["age"]))
     $age= $_GET["age"];

if(isset($_GET["Year"]))
     $Year= $_GET["Year"];

if(isset($_GET["FindAge"]))
     $FindAge = $_GET["FindAge"];

if(isset($_GET["YearBorn"]))
     $YearBorn = $_GET["YearBorn"];


if(isset($YearBorn))
{
$year = 2007 - $age;
echo "Year Born = $year";
}

if(isset($FindAge))
{
$age =  2007 - $year;
echo "Your Age = $age";
}

?>


<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="GET">
<label for="Age">Age :</label><input type="text" id="n1" name="age" value="<?php echo $age; ?>" /></br><br>
<label for="Year">Year Born :</label><input type="text" id="n2" name="year" value="<?php echo $year; ?>" /></br><br>
<input type="submit" name="YearBorn" value="Find Year Born" />
<input type="submit" name="FindAge" value="Find Your Age" />
</form>



Link to comment
Share on other sites

Hi itsmeARRY,

 

    Thank you for the help. I had to modify your code to get it to work, but you pointed me in the right direction. Here is the code I used:

<head>
<title>Year/Age Calculator</title>
</head>
<body>

<?php
$age = '';
$year = '';

$FindAge ='';
$YearBorn ='';
$Operation=$_GET["Operation"];

if(isset($_GET["age"]))
     $age= $_GET["age"];

if(isset($_GET["year"]))
     $year= $_GET["year"];

if(isset($_GET["FindAge"]))
     $FindAge = $_GET["FindAge"];

if(isset($_GET["YearBorn"]))
     $YearBorn = $_GET["YearBorn"];

if($Operation == "YearBorn")
{
$year = 2007 - $age;
}
else
if($Operation == "FindAge")
{
$age =  2007 - $year;
}

?>

<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="GET">
<label for="Age">Age :</label><input type="text" name="age" value="<?php echo $age; ?>" /></br><br>
<label for="Year">Year Born :</label><input type="text" name="year" value="<?php echo $year; ?>" /></br><br>
<input type="submit" name="Operation" value="YearBorn" />
<input type="submit" name="Operation" value="FindAge" />
</form>

</body>
</html>

 

I am now trying to make the form remember which color (a select list) they selected before pressing any of the submit buttons. I entered this code into the form:

 

<select name="color">
<option value="red">red</option>
<option value="blue">blue</option>
</select></br>><br>

 

For example, if the user selects blue and then clicks on either one of the "submit" buttons, I want the form to remember/show the blue option. Right now the select list always goes back to red, the first choice. I tried doing a <?php echo $red; ?> and <?php echo $blue; ?> for the option value, but this does not work since the select list needs a value.

 

Thank you.

Link to comment
Share on other sites

I wrote a function "fillInFormValues()" that makes this kind of thing easy.

 

You put the form in a PHP string:

$formHTML =<<<FORM_END

<form....>

  ... all the form stuff...

</form>

FORM_END;

 

And then you call:

$filledInHTML = fillInFormValues($formHTML, $_GET);

 

... and magic happens.  Your <selects> will be selected properly (based on what's in $_GET), your checkboxes will be checked properly...

 

Grab it from: http://www.skypaint.com/gavin/code/

(online examples are there, too).

 

Link to comment
Share on other sites

Hi Gavinandresen,

 

      Congratulations on the wonderful php function you have created. However, I wish to learn how to solve my problem with some basic php code that I can understand...I tried looking at how your code works and I am lost....sorry I am a total newbie to this. If it is possible can you please explain to me how to make php remember which <select> a user clicked?

 

Thank you.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

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.

 Share

×
×
  • 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.