PHP - Using The Variable Generated From A Radio Button
Hey all,
I'm almost too embarrased to post this as I'm sure it is very simple - but for the life of me I can't get this to work. I am a complete newbie to PHP so please bear with me.. What I'm trying to do is to write a program that will go on to calculate a user's Body Mass Index (BMI) based on user inputted data of height and weight. However, I want to be able to accept heights and weights in a number of different units for maximum ease of use. I am trying to write some PHP code that will handle this , the main goal is to convert everything into 'cm' and 'kg' before going on and doing the simple BMI calculation later on. However I am stuck at this point: I have a text field for users to input their weight into. This is immediately followed by two radio buttons for the user to select which units they are inputting their weight in (kilograms or pounds). A third option to input their weight is given after this for those wishing to input their weight in 'stones and pounds'. My problem is as follows: I can't get my code to recognise which radio button (either 'kg' or 'lbs') has been pressed. What should happen is that my code can tell something has been inputted in the text box AND which of the radio buttons has been selected. From this it does one of the two; for the kilogram option it leaves the value as it is, but if the weight has been inputted in pounds (and the 'lbs' radio button selected) then I want the code to convert this into kilos by multiplying by 0.4535. Here is what I have so far (sorry its a bit messy - like i say I am a newbie): <?php //convert.php $ft = $cm = $inches = $weight = $stones =$pounds = $kilos = $units = ""; if(isset($_POST['cm'])) $cm = sanitizeString($_POST['cm']); if(isset($_POST['ft'])) $ft = sanitizeString($_POST['ft']); if(isset($_POST['inches'])) $inches = sanitizeString($_POST['inches']); if(isset($_POST['weight'])) $weight = sanitizeString($_POST['weight']); if(isset($_POST['stones'])) $stones = sanitizeString($_POST['stones']); if(isset($_POST['pounds'])) $pounds = sanitizeString($_POST['pounds']); if(isset($_POST['kilos'])) $kilos = sanitizeString($_POST['kilos']); if(isset($_POST['units'])) $units = sanitizeString($_POST['units']); if ($ft != '') { $height = intval(($ft * 30.48) + ($inches * 2.54)); $out = "you are $height cm tall"; } elseif($cm != '') { $height = intval($cm); $out = "you are $height cm tall"; } else $out = ""; if ($stones != '') { $kilos = intval((($stones * 14) + $pounds) * 0.45359237); $out2 = "you weigh $kilos Kg"; } elseif($weight != '' AND $units = "kg") { $kilos = $weight; $out2 = "you weigh $kilos Kg"; } elseif($weight != '' AND $units = "lbs") { $kilos = ($weight * 0.45359237); $out2 = "you weigh $kilos Kg"; } else $out2 = ""; echo <<<_END <html><head><title>Height & Weight Converter</title> </head><body><pre> Please enter your details below <b>$out$out2</b> <form method="post" action="convert.php"> Height: <input type="text" name="cm" size="3"> cm OR <input type="text" name="ft" size="1">ft <input type="text" name="inches" size="2">inches Weight: <input type="text" name="weight" size="4" /> Kg<input type="radio" name="units" value="kg" /> lbs<input type="radio" name="units" value="lbs" /> OR <input type="text" name="stones" size="2">stone <input type="text" name="pounds" size="2">pounds <input type="submit" value="Submit" /> </form></pre></body></html> _END; function sanitizeString($var) { $var = stripslashes($var); $var = htmlentities($var); $var = strip_tags($var); return $var; } ?> Similar TutorialsI am trying to allow the user to update a variable he chooses by radio buttons, which they will then input text into a box, and submit, to change some attributes. I really need some help here. It works just fine until I add the second layer of variables on top of it, and I can't find the answer to this question anywhere. <?PHP require('connect.php'); ?> <form action ='' method='post'> <select name="id"> <?php $extract = mysql_query("SELECT * FROM cars"); while($row=mysql_fetch_assoc($extract)){ $id = $row['id']; $make= $row['make']; $model= $row['model']; $year= $row['year']; $color= $row['color']; echo "<option value=$id>$color $year $make $model</option> ";}?> </select> Which attribute would you like to change?<br /> <input type="radio" name="getchanged" value="make"/>Make<br /> <input type="radio" name="getchanged" value="model"/>Model<br /> <input type="radio" name="getchanged" value="year" />Year<br /> <input type="radio" name="getchanged" value="color" />Color<br /><br /> <br /><input type='text' value='' name='tochange'> <input type='submit' value='Change' name='submit'> </form> //This is where I need help... <?PHP if(isset($_POST['submit'])&&($_POST['tochange'])){ mysql_query(" UPDATE cars SET '$_POST[getchanged]'='$_POST[tochange]' where id = '$_POST[id]' ");}?> what would be the $_POST variable name for a radio button option? Hi All, I have been trying validating radio buttons that were generated dynamically for 2 days now. I can validate when a group of 2 or more than 2 radio buttons are created. But I am unable to validate if a single radio button is generated. So this is where I am generating radio buttons echo "<tr>"."<td class='tablewhitebg'>"."<input type = 'radio' name= 'userid' value= '".$userid. "'></td>"."<td class='tablewhitebg'>$userid</td><td class='tablewhitebg'>$userloginid</td><td class='tablewhitebg'>$fname</td><td class='tablewhitebg'>$surname</td><td class='tablewhitebg'>$locked</td><td class='tablewhitebg'>$mailbox</td><td class='tablewhitebg'>$domainid</td><td class='tablewhitebg'>$timestamp</td><td class='tablewhitebg'>$emailaddress</td><td class='tablewhitebg'>$classofservice</td></tr>\n"; Now this code is generating radio buttons, all is good but when i want to validate it using javascript, Its not working at all <script> function validate_form(thisform) { chosen = ""; len = document.form1.userid.length for (i = 0; i <len; i++) { if (document.form1.userid.checked) { chosen = document.form1.userid.value; } } if (chosen == "") { alert("Please select a record to go ahead!!"); return false; } } </script> Helps please Hi everyone, first of all let me thank you for this wonderful forum I come back here always when i need help... i'm trying to learn so soon i hope, i'll be helping others... I have a question for now, i have a code which goes like this: Code: [Select] <? $b = time (); $date1 =date( "Y-m-d;h:i:s" , mktime(date("h")+6, date("i"), date("s"), date("m") , date("d"), date("Y"))); $str_time = "&receivedtimestamp="; $str_msg = "&msg=bkt"; $str_from = "from="; ?> <a href="http://testext.i-movo.com/api/receivesms.aspx?<?echo $str_from;?><?=$getuser[0]['phone'];?><?echo $str_time;?><?echo $date1;?><?echo $str_msg;?>">Get a Cupon</a> What i need is that when the URL is generated, one of those variables to be hidden, for example the $str_from to be in the link but not visible... I must tell you that the link on the Get Cupon goes to an external link so i don't know if i can do it with .httpaccess as i've heard... hey all so I have this bit down: Code: [Select] $query="SELECT `2010 Region Code` AS codes FROM locations"; $results = mysql_query($query); $options=""; $options = "<select location='codes'>"; while($nt=mysql_fetch_assoc($results)) { $thing=$nt["codes"]; $options.="\r\n<option value ='{$nt['codes']}'> {$nt['codes']}</option>"; } $options .="\r\n</select>"; echo $options; what I'm trying to do is grab the selection from the drop down and display it as a table (the sql query would be extended should we manage to figure this one out I've tried Code: [Select] echo"<form name='LOCATIONS' action='".$_SERVER['PHP_SELF']."' target='iframe' method='post'>"; any ideas? Hello there!!! I am taking my first steps into php and html code and i am facing a problem. I am making a form in order for the user to create a new category in my page but i don't know how many Attributes the category will have. So i made a field for the Attribute followed by a radio tha chooses if the Attribute is measurable or unmeasurable. After that I have set a Button in order to regenerate it with JavaScript as many times as necessary. this is the html code: Code: [Select] <form name="Create Category" align="center" action="Assets/register_test.php" method="post"> <table style="margin-left: 40%;" id="Attributes"> <tr> <h2 align="center">Attributes</h2> </tr> <tr class="feed" align="left"> <td> <input style="margin-top: 3px;" type="text" name="Attribute" size="30"> <br/> <form align="center"> <input type="radio" name="var" value="measurable" /> measurable <input type="radio" name="var" value="unmeasurable" /> unmeasurable </form> </td> </tr> </table> <div id="attr" style="display:none"> <div class="feed"> <input type="text" name="Attribute[]" value="" size="30"> <form> <input type="radio" name="var" value="measurable" /> measurable <input type="radio" name="var" value="unmeasurable" /> unmeasurable </form> </div> </div> <a style="height: 25px; margin-left:48%; margin-top: 20px;" href="javascript:add_feed()">Add New </a> <input style="height: 25px; margin-left:48%; margin-top: 20px;" type="submit" value="Submit" onclick="validateFields()"> <input style="height: 25px; margin-left:48%; margin-top: 20px;" type="reset" value="Reset"> <br/> </form> this is my JavaScript: Code: [Select] function add_feed() { var div1 = document.createElement('div'); // Get template data div1.innerHTML = document.getElementById('attr').innerHTML; // append to our form, so that template data //become part of form document.getElementById('Attributes').appendChild(div1); } The problem is the Reset button. When i hit the Reset button everything gets erased in all the text-inputs But the radios (except the first one) don't. Is there is anyone who can help please? I have a submit.php file that includes the following jQuery code: <script> [...] request.done(function( json ) { jQuery('input[name=thumbnail-url]').val(json.thumbnail_url); jQuery('input[name=job_title]').val(json.title); jQuery('textarea[name=htmlcode]').val(json.html); [...] </script> I need to remove jQuery('textarea[name=htmlcode]').val(json.html); and pass the "json.html" value into a PHP variable in another php file (functions.php). The code in the functions.php file is already there. The thing i need to do (and i am seriously struggling with it) is placing the value of json.html into a $phpvariable that i can then call from the function.php file. Do you need any more info for this issue? Thanks. What am I doing wrong with this selected radio button? <form action="Process/privacy.php" method="post" enctype="multipart/form-data" id="privacy"> <div id="privacy_vis">Your profile is visible to:</div> <div id="privacy_select"> <label> <input name="RadioGroup1" type="radio" id="RadioGroup1_0" value="1" <?if($visible=="1")echo 'checked="checked"';?> /> Everyone</label> <label> <input name="RadioGroup1" type="radio" id="RadioGroup1_1" value="0"/> Members Only</label> </div> <br /> <div id="privacy_vis">Your Password to view your private pictures is:</div> <div> <input name="pictures_pw" type="text" id="pictures_pw" value="<? echo "$xxx_pw" ?>" size="50" maxlength="20" /> </div> <br /> <div> <input name="submit" type="submit" class="button" id="submit" value="Save Changes" /> </div> </form> </div> I have a function to display some images and allow a user to select a choice via a radio button.The value is then passed to another function. I have 2 problems My selection is not being passed back and I cant assign the value to a query sring in a link to another page. On the form do i need to add checked? Any help appreciated. Code: [Select] function cover() { echo'<h2>Please upload your book cover or choose one from the selection</h2> <p>All book ideas require a cover. Your choice can be changed at a later date.</p>'; //process selection of cover if (isset($_POST['submit'])) { $coverID=$_POST['cover']; echo $coverID; ?>   <ul><li class="demo-menu-link"><a href="?page=submenu7&coverID=$coverID">Finnished Cover</a></li></ul><?php exit(); } else{ echo "not Submitted"; } global $wpdb; $query = "select * from wp_cover"; $result = mysql_query($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error()); echo mysql_error(); if (!$result){ return false; } echo'<div class="wrap"><p>choose from one of the covers below</p></div>'; ?><form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST" id="cover" enctype="multipart/form-data"> <table><tr><?php /* display picture and radio button */ while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $coverID=$row['coverID']; echo"<img src=\"/wordpress_3/wp-content/plugins/Authors2/jackets/{$row['pix']}\" />"; echo"<td>"; echo $coverID; echo "<Input type = 'Radio' Name ='cover' value= '$coverID'/>"; ?> </tr></table> <input type=submit value=submit> </form><?php } } Hello, I had 3 radio buttons with the same name, well, I had created 3 text box that are next to those 3 radio buttons. When I click save to the form, I need to know which radio button had been pressed, so that I can detect & retrieve its value. Any help? Thanks I am trying to have the text alongside the radio button trigger the radio button (cannot use < a href ) e.g. () Trigger with this text The closest I have found is <label for="r1"><input type="radio" name="group1" id="r1" value="1" /> button one</label> But it does not seem to work? Any help greatly appreciated Good Day, I juz like to know if there any other alternative way for the code below, coz i can't update my form when i use this method for radio the button. It's not passing the value. Code: [Select] Edit.php <tr> <td height="36" align="center" valign="middle" bgcolor="#990000"><div align="left" class="style4">Destroy :</div></td> <td align="center" valign="middle" bgcolor="#990000"><div align="left"> <label> <input type=radio name=destroyR value="Yes" <?php echo ($rows['destroy']=='Yes') ?'checked="checked"':''; ?> />Yes</label> <label> <input type=radio name=destroyR value="No" <?php echo ($rows['destroy']=='No') ?'checked="checked"':''; ?> />No</span></label> </div></td> </tr> how do you make it so that a radio button is either checked or unchecked based on database? This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=346803.0 i would like to display the value of the selected radio button on the same page... what i want is just reload the page and if there is a radio button selected displays it but i cant display it <?PHP if (isset($_GET["submit"])) { echo $_GET["myradio"]; } ?> <html> <head> </head> <body> <form action="<?PHP $_SERVER["PHP_SELF"]; ?>" method="get"> One <input type="radio" name="myradio" value="one"> Two <input type="radio" name="myradio" value="two"> <a href="<?PHP $_SERVER["PHP_SELF"]; ?>" name="submit">test </a> </form> </body> </html> please help guys...thanks Hi, I have 2 radio button's within a table and need to pass this array. All other rows within my table can be sent as array's and captured using $_POST['field_name'][$ROW 1 value etc]. However for radio buttons this doesn't work. As an example, a standard textbox I can send an array using the following: echo "<td width='33%'><input type='Textbox' width='100%' id='test' name='test[]' value='" .$row['test']. "'></td>"; I can then capture it using: $test = $_POST['test'][$iLine]; This works, no problem, however for radio buttons, I can't use a static array name, as presumably it dumps all rows into the array, if I do this, only the last row has a checked button, hence I need to use the following (where $checked includes the logic to decide whether the button should be checked or not, this works correctly): echo "<input type='radio' name='".$row['venue_id']."[]' value='1'".$checked."/>P1" echo "<input type='radio' name='".$row['venue_id']."[]' value='0'".$checked2." />P2"; I tried using the following: var_dump($_POST[$row]['game_id'][$iLine]); But this just returns a null value. Any ideas? I am trying to make a set of radio buttons that when you make a selection, it displays a sorted database from MYSQL. I have the MYSQL part figured out but I cannot figure out how to properly display it. I have the following code and I made a select screen just to test: index.html <html> <body> <form action="select.php" method="post"> <input type="radio" name="table" value="table1"> Table 1 <br> <br> <input type="radio" name="table" value="table2">Table 2 <br> <br> <input type="radio" name="table" value="table3">Table 3 <br> <br> <input type="radio" name="table" value="table4">Table 4 <br> <br> <input type="radio" name="table" value="table5">Table 5 <br> <br> <input type="Submit" Name = "submit" value="Submit"> <input type="Reset" value="Reset"> </form> </body></html> select.php <?PHP $selected_radio = $_POST["table"]; if ($selected_radio = = 'table1) { echo "Table 1 Here."; } else if ($selected_radio = = 'table2') { echo "Table 2 Here."; } else if ($selected_radio = = 'table3') { echo "Table 3 Here."; } else if ($selected_radio = = 'table4') { echo "Table 4 Here."; } else if ($selected_radio = = 'table5') { echo "Table 5 Here."; } ?> These are two separate files. When I go to run the code, no matter what I pick, the next page is always blank. Again, the select.php is just suppose to be a check, the real code will display the table instead of echoing a message. I have a group of 4 radio buttons. my mysql database knows which one the user selected previously when they stored some information. I now wish to display the same buttons but have radio button checked that the user chose before. I know this can be done but having one of those mind melt moments where I cannot think of the solution so I have 4 buttons that look like this Code: [Select] <input type="radio" name="clarity" id="clarity" value="Opaque"> Opaque<br> <input type="radio" name="clarity" id="clarity" value="Deep"> Deep<br> <input type="radio" name="clarity" id="clarity" value="Mid"> Mid<br> <input type="radio" name="clarity" id="clarity" value="Pale"> Pale<br></td> I just need to attached a checked field to the correct item This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=317717.0 I am wondering if this is posible. I have a group of radio butons. I would like to make them as variables so that I can use an "if then" statment in another part of the page to show an image depending on which button is selected. <form id="form1" name="form1" method="post" action=""> <p> <label> <input type="radio" name="color" value="red" id="color_0" /> red</label> <br /> <label> <input type="radio" name="color" value="blue" id="color_1" /> blue</label> |