PHP results to a text box

JBJB Carlsbad, CA
edited December 2003 in Internet & Media
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!

Comments

  • ShortyShorty Manchester, UK Icrontian
    edited December 2003
    Post the scripts and we can troubleshoot it for you :)

    .. and use the PHP tags available .. so syntax is coloured :) (see attachment).
  • JBJB Carlsbad, CA
    edited December 2003
    well, right now here is the form, and i want the data entered in the first text box(baseten) to be converted by convert.php

    form.jpg
    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.
  • ShortyShorty Manchester, UK Icrontian
    edited December 2003
    First obvious problem is that form variables.

    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?
  • EnverexEnverex Worcester, UK Icrontian
    edited December 2003
    It would be much more effective to do this as a client side program using something like Java or VB script as then the page doesn't have to be sent back, it can just convert it immediately when the user presses the button.
  • JBJB Carlsbad, CA
    edited December 2003
    shorty, thats right, i want the user to enter a baseten number, click 'To Binary' and have the results in that other box. right now those variable names are working for me, since the form's action value goes to convert.php...i can change them to the method described and i should still get the same results. i modified this code a tad...right now i have it print the base 10 number again and the binary string. both values are correct, but it loads it on a seperate page.
    This is all there is to the form, its just a simple example for a class to show a converion to a binary string.
  • JBJB Carlsbad, CA
    edited December 2003
    enverex,
    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
  • ShortyShorty Manchester, UK Icrontian
    edited December 2003
    [php]
    <?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.
  • JBJB Carlsbad, CA
    edited December 2003
    Thanks shorty! I have to go to class but i will try it out this evening. i didnt know even about that isset...what a newbie:D
    :respect::respect:
  • ShortyShorty Manchester, UK Icrontian
    edited December 2003
    The concept will work just fine but I would suggest you check your vars & statements as they dont read right to my eyes :)

    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 :)
  • EnverexEnverex Worcester, UK Icrontian
    edited December 2003
    Hey, I'm a talented coder too! My entire site is coded in PHP from scratch. Though lately I haven't had time to code for anyone else or even my own site due to Uni, but i'm off till Feburary now so I have some time....
  • ShortyShorty Manchester, UK Icrontian
    edited December 2003
    Post updated, apologies for leaving you out there! :)
  • JBJB Carlsbad, CA
    edited December 2003
    thanks shorty, that works! but can i assign that result to a text box. right now that prints at the top of the page, but can i assignt that to the value of a text box?
    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]
  • ShortyShorty Manchester, UK Icrontian
    edited December 2003
    Yes :)

    Il re-write it for you in a mo ;)
  • ShortyShorty Manchester, UK Icrontian
    edited December 2003
    [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 ;
    }
    $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 :)
  • JBJB Carlsbad, CA
    edited December 2003
    thank you so much!!! thats just the ticket i need!:respect::respect:
  • ShortyShorty Manchester, UK Icrontian
    edited December 2003
    Glad I could help :)
  • EnverexEnverex Worcester, UK Icrontian
    edited December 2003
    Strange, I just pasted that all into a page and stuck it on my server to try it, and it doesn't work - http://atomnet.co.uk/asd.php
  • JBJB Carlsbad, CA
    edited December 2003
    you dont have a value set for the second text box Enverex
  • ShortyShorty Manchester, UK Icrontian
    edited December 2003
    I think he has calls to other functions in the rest of the script.. bits that we haven't seen ;)
  • EnverexEnverex Worcester, UK Icrontian
    edited December 2003
    Radeon_Man had this to say
    you dont have a value set for the second text box Enverex

    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.
Sign In or Register to comment.