PHP results to a text box
JB
Carlsbad, CA
Ok, ive been at this for a while but cant find any help online...what i have is a text box where the user enters a base 10 number, and i have a PHP script to convert it to binary. That works well, but i dont know how to get it to send the results to a text box. right now its a form with action=convert.php(Which opens it in a new window). I have a text box where they can enter their value. I tired copying my function into file.php, but i cant get the click action linked to evaluating the function. Any help would be apreciated!
0
Comments
.. and use the PHP tags available .. so syntax is coloured (see attachment).
career.php which contains the form:
[PHP] <td>
<form action="convert.php" method="POST">
<font face="arial" size=3 color="336666">Number in Base 10: <input type="text" name="baseten">
<input type="submit" value="To Binary ->"> <input type="text" name = "result" value="<?php echo value here ?>">
</form>
</td>
[/PHP]
convert.php:
[PHP]
<?php
$basetenout = $baseten ;
$basetenout = number_format($basetenout);
$i = 0;
while($baseten > 0)
{
if( pow(2,$i) > $baseten)
{
$baseten -= pow(2,$i-1);
$binary += pow(10,$i-1);
$i=0;
}
else if( pow(2, $i) == $baseten)
{
$baseten -= pow(2,$i);
$binary += pow(10,$i);
$i=0;
}
$i += 1 ;
}
print "$binary" ;
?>
[/PHP]
my problem is i dont know what to put as the value in that second field(right now it echos test). Ive tried copying the convert.php to the career.php(where the form is located) and turning it into a function, and using a function call as the value, but that didnt seem to work either.
They are passed using the POST method to the handler (convert.php) .. but there is nothing identifying them.
$basetenout = $baseten;
should be:
$basetenout = $_POST;
There are several ways you can make this work (assuming the calculation statements work and provide you the result that you want).
Are there any other parts of this form?? or is this it?
Edit:// Looking at the way it's doing things.. I take it you want the end user to input a baseten, click the form to launch the popup.. then refresh the form to echo the value? Is that close?
This is all there is to the form, its just a simple example for a class to show a converion to a binary string.
yeah i was thinking of that...i guess i may just figure out the VB scripting, as thats a simple operation in VB.
thanks guys
<?php
if (isset($_POST))
{
$basetenout = $_POST;
$basetenout = number_format($basetenout);
$i = 0;
while($baseten > 0)
{
if( pow(2,$i) > $baseten)
{
$baseten -= pow(2,$i-1);
$binary += pow(10,$i-1);
$i=0;
}
else if( pow(2, $i) == $baseten)
{
$baseten -= pow(2,$i);
$binary += pow(10,$i);
$i=0;
}
$i += 1 ;
}
print "$binary" ;
} else {
?>
<td>
<form action="<?=$_SERVER?>" method="POST">
Number in Base 10: <input type="text" name="baseten">
<input type="submit" value="To Binary">
</form>
</td>
<?
}
?>
[/php]
Put's the two scripts together to do what you want. I haven't tested it, but as long as your calculation statements (and variables) are correct.. this will work.
Have any more problems, shout back. We are trying to promote our programming forum alot more. We have some really talented coders around us (Mr. Kwitko, Park_7677, Enverex and Josh are just some of them) .. so ask anytime
here is my script now(i dont need that format number any more)
[PHP]
<?php
if (isset($_POST))
{
while($baseten > 0)
{
if( pow(2,$i) > $baseten)
{
$baseten -= pow(2,$i-1);
$binary += pow(10,$i-1);
$i=0;
}
else if( pow(2, $i) == $baseten)
{
$baseten -= pow(2,$i);
$binary += pow(10,$i);
$i=0;
}
$i += 1 ;
}
print "$binary";
}
?>
[/PHP]
Il re-write it for you in a mo
<?php
if (isset($_POST))
{
while($baseten > 0)
{
if( pow(2,$i) > $baseten)
{
$baseten -= pow(2,$i-1);
$binary += pow(10,$i-1);
$i=0;
}
else if( pow(2, $i) == $baseten)
{
$baseten -= pow(2,$i);
$binary += pow(10,$i);
$i=0;
}
$i += 1 ;
}
$binaryfinal = $binary;
}
else { $binaryfinal = ""; }
?>
<td>
<form action="<?=$_SERVER?>" method="POST">
Number in Base 10: <input type="text" name="baseten">
<input type="submit" value="To Binary">
<input type="text" value="<?=$binaryfinal?>">
</form>
</td>
[/php]
That should do it. I just called a final variable called $binaryfinal .. which value is empty unless it's a page refresh
The number is sent to the box when you click the button, as the page refreshes with the new value, which is then used to run through the script, and then assigned to the box.
Just something else you may want to do though, is assign $_POST to the input box when you refresh, so you can see what number was entered initally, i.e.
[php]
<td>
<form action="<?=$_SERVER?>" method="POST">
Number in Base 10: <input type="text" name="baseten" value="<?php echo $_POST; ?>">
<input type="submit" value="To Binary">
<input type="text" value="<?=$binaryfinal?>">
</form>
</td>
[/php]
Just to make the users life easier.
Shorty: Yeah, he must be.