Jump to content

Dropdown


phpdev@dr

Recommended Posts

Hi!

 

I need help making a php app that allow me to enter a binary  number and convert it to decimal and viceversa. It will have a text box to enter the number, a drop down menu with: binary to dec and dec to binary. It will print on another page in a table the original number and the conversion. Lets say I enter:

 

255 ===== 11111111

 

Im new with php so please help!

 

thanks,

Link to comment
Share on other sites

Php has built in functions to achieve this

 

see here and here

 

a quick example would be to post the text box to a php file convert using php and output to the page..

 

<?php
$in = $_GET['bin'];
if($in){ 
echo "Original Binary - $in -- Decimal equivilent - ".bindec($in);
}
?>

 

 

 

Stuie

Link to comment
Share on other sites

Thanks,

 

I am using those two fuctions and the conversion is ok, the problem I am having is when using the dropdown menu. something I noticed is that you are using the $_get and I am using $_post, does it make a difference?, Do you then make to separate pages, one for bin to dec and another for dec to bin? could you use some if...else statement, select case or else? Please give me some example. :shrug:

Link to comment
Share on other sites

no GET and POST do the same job just slightly different..

 

the following example should get you started..

 

<?php
$meth = $_POST['meth'];
$value = $_POST['value'];

if(!$meth or !$value){
?>
<form name="convertor" id="convertor" action="" method="post">
<input name="value" id="value" type="textbox" />
<select name="meth" id="meth">
<option value="1">Binary to Decimal</option>
<option value="2">Decimal to binary</option>
</select>
<input type="submit" name="submit" id="submit" value="Convert" />
</form>
<?php
}elseif($meth and $value){
$out = $meth==1 ? bindec($value) : decbin($value);
echo "Original Value - $value -- converted value - $out";
}
?>

 

Stuie

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.