PHP - How To Store The Value Of A Select Drop Down Menu Into A Variable Within
the same page?
Similar TutorialsHi, I'm no pro at PHP but I am trying to get a drop down menu to a authenticate before moving to the next part of the form. What I want is once a selection has been made, ONLY THEN can the user move on, OTHERWISE a message echo appears. This is the html menu box <select size="1" name="title"> <option>Please Select</option> <option value="Mr">Mr</option> <option value="Mrs">Mrs</option> <option value="Miss">Miss</option> <option value="Ms">Ms</option> <option value="Dr">Dr</option> </select> Then this is what I have in the form PHP: $visitortitle = $_POST['visitortitle']; if ( HOW DO I GET THIS PART TO AUTHENTICATE AN OPTION HAS BEEN SELECTED? ) { echo "<p>Please enter a title correctly<br />before you try submitting the form again.</p>\n"; die ( '<a href="pef.html">click here go back and try again</a>' ); echo $id;} If anyone can help me sort out this part of the form I can move on as the rest is working fine? Thanks Gary I am using jquery .change function to perform an operation when a month is selected from a drop down menu. The change works but I am unable to update the value of the drop down menu with the updated month. My drop down shows the starting value as default even on change. Can anyone help. Following is the code snippet that does change and then the drop down menu form. Code: [Select] $("#monthName").change(function() { alert($("#monthName").val()); if ($("#post").val() == 1) { $("#monthselect").submit(); } }); Code: [Select] <form id="monthselect" action="<?=$_SERVER['PHP_SELF']?>" method="get"> <input id="post" type="hidden" name="post" value="1"> <label>SELECT MONTH</label> <select id="monthName" name="monthName"> <option value="January">January</option> <option value="February">February</option> <option value="March">March</option> <option value="April">April</option> <option value="May">May</option> <option value="June">June</option> <option value="July">July</option> <option value="August">August</option> <option value="September">September</option> <option value="October">October</option> <option value="November">November</option> <option value="December">December</option> </select> </form> Even after the change, January shows up by default even if I select say June or July. I tried something like following but did not work. Code: [Select] $("#monthName option[value=" + $("#monthName").val() +"]").attr("selected","selected") ; Hi there, i am relatively new to php, mysql, css etc but learning fast. My problem is such; i have a php file which is doing a SELECT mysql_query, WHILE results to strings, then ECHO the resulting rows to produce a list formatted using <table> and finally this <table> is inside a <form> which will POST the changes back to the specific database.tble.row. I wish to have a drop down menu within the <form><table> which will be populated from a separate database.table. I have accomplished the drop down menu outside the <?php ?> tags inside <form><table> which POSTS to a php file but my problem is to add the populating drop down menu inside <?php ?> an already ECHOing resulting rows from the sql query. i.e <?php blurb and stuff ?> <form><table><tr><td> <select name etc> <?php $result = mysql_query("SELECT * FROM tbl WHERE string = tble.rw ORDER BY column"); while($row=mysql_fetch_array($result)){ echo "<OPTION VALUE=".$row['column'].">".$row['column']."</OPTION>"; } ?> </select> WORKS!!!! but placing this inside <?php $x =mysql_query[select] while {strings = conditions; echo ("<form><table><tr><td> insert populated drop menu here </td> etc "); echo"";}?> doesnt work and just leaves the select drop menu blank Hoep you understand my problem. I do not think i can attached the population WHILE loop to a string and just insert the string to the form but maybe i am wrong. thanks in advance and if you go tthis far reading you must be on lots and lots of coffee zark Hi all I am trying to get my drop down menu to use a session variable as the selected menu option. Here's my code: echo "<option value=\"\">Delivery Area</option>"; $selected = ""; if(isset($_SESSION['postal_area']) && $_SESSION['postal_area'] == "England") { $selected = "selected"; } elseif(isset($_SESSION['postal_area']) && $_SESSION['postal_area'] == "Wales") { $selected = "selected"; } elseif(isset($_SESSION['postal_area']) && $_SESSION['postal_area'] == "Scotland") { $selected = "selected"; } else { $selected = ""; } echo " <option value=\"England\" selected=\"".$selected."\">England</option>"; echo "<option value=\"Scotland\" selected=\"".$selected."\">Scotland</option>"; echo "<option value=\"Wales\" selected=\"".$selected."\">Wales</option>"; echo "</select>"; However, this shows the 'Wales' one all the? Please help! Many thanks Pete I am currently creating a form and I want to populate a drop down selection menu with data from two fields in a form. For example, I want it to pull the first and last name fields from a database to populate names in a drop down menu in a form. I want the form to submit to the email address of the person selected in the drop down. Is this possible to do? The email is already a field in the record of the person in the database. Can anyone give me some pointers or advice on how I should go about setting up the "Select" box drop down? I am not sure how to code it to do what I am wanting. Any links to relevant help would be appreciated too. Thanks in advance! how do i get a drop down value and store it in a another table as of now i am getting the value from one table showing it on drop down now once that is selected i need to insert it into another table. <html> <?php mysql_connect('localhost','root','') or die('Could not connect to mysql ' . mysql_error()); mysql_select_db("dbtest") or die(mysql_error()); if(isset($_POST['submit'])) { //$name=$_POST['val']; $id = $_POST['select']; $id=$row['name']; ?> <input type="text" name="name2" value="<?php echo $row['name'] ?>"><br /> <?php $query = "INSERT INTO sub (`sub`) values('" . $id . "')"; $result = mysql_query($query); if($query) { echo "query executed"; } } ?> <form id="name" action="<?php $_POST['SERVER_SELF'] ?>" method="POST" > <input type="text" name="name1"><br /> <select name="select" id="select"> <?php $query = "SELECT name,id FROM main"; $result = mysql_query($query); if($query) { while($row = mysql_fetch_array($result)) { $options .= '<option value="' . $row['name'] . '">' . $row['name'] . '</option>'; // $out .= "<option value=\" . $row['name'] . \" >' . $row['name'] . '</option>"; } } echo $options; ?> </select> <input type="submit" name="submit1"> </form> </html> Thanks the above seem to work. Quote i need to store a variable from database like if i have "copies" in one of my column in my database then i have to store a particular value for copies store it to $copies here i want that i can store value of copies into $copies $update_book="update book set copies=copies-1 where bookid='$bookid'"; $result=mysql_query($update_book,$linkID1); if($result) { print "<html><body background=\"header.jpg\"> <p>book successfully subtracted from database</p></body></html>"; } else { print "<html><body background=\"header.jpg\"> <p>problem occured</p></body></html>"; } } It seems that in the following script I am not able to make $page contain some values as it always acts as it contains 0. The script will give you more idea about what I wanna do with it. Code: [Select] <?php include('dbinfo.inc'); session_start(); $connect= mysql_connect($host,$dbuser,$password) or die("connect to database fails"); mysql_select_db($dbname, $connect); //This will set the number of messages we want to display on each page $rows_per_page = 5; function show_msg() { echo "<tr align='center'> <td>$row[message]</td> </tr> <tr><td><strong>Sent By: $_SESSION[username] ON Date: $row[date]</strong></td></tr> <tr><td></td></tr>"; } //This will set the necessay variables required to set the page layout function page_vars() { $sql = "select * from msgs"; $result = mysql_query($sql, $connect); $numrows = mysql_num_rows($result); $total_pages = ceil($numrows/$rows_per_page); //Check whether page is given or not if(isset($_GET["page"])) { $page = $_GET["page"]; } else { $page = 1; } if($page < 1): { $page = 1; } elseif($page > $total_pages): { $page = $total_pages; } endif; } if($_SESSION[loggedin] == TRUE && $_SESSION[actype] == lecturer) { echo "<form action='postmsg.php' method='POST'>"; echo "<table width='80%' border='1'><tr align='center'><td><strong>To send a new message type it in the below box and press SUBMIT</strong> </td></tr>"; echo "<tr align='center'> <td><textarea name='msgarea' cols='40' row='5'></textarea></td> </tr> <tr align='center'> <td><input type='submit' value='Send Message' /></td> </tr> </table>"; $limit .= 'LIMIT '.($page - 1)*$rows_per_page.', '.$rows_per_page; echo $limit; $sql = "select * from 'msgs' $limit"; $result = mysql_query($sql); echo "<table width='80%' border='1'>"; $row = mysql_fetch_assoc($result); while($row) { show_msg(); } } else { page_vars(); $limit .= 'LIMIT '.($page - 1)*$rows_per_page.', '.$rows_per_page; $sql = "select * from msgs $limit"; $result = mysql_query($sql,$connect); echo "<table width='80%' border='1'>"; while($row = mysql_fetch_assoc($result)) { show_msg(); } echo "<tr align='center'> <td> <a href='$_SERVER[php_self]?page=1'>First Page</a> <a href='$_SERVER[php_self]?page=($page - 1)'>$page</a> <a href='$_SERVER[php_self]?page=$page'>$page</a> <a href='$_SERVER[php_self]?page=($page + 1)'>$page</a> <a href='$_SERVER[php_self]?page=($total_pages)'>>>>Last Page</a> </td> </tr>"; echo "</table>"; } ?> now when i echo $limit it always shows "-5, 5" , that's not what i want from this script. Instead I want limit to store "0, 1" with the same concept not direct because it will change according to what $_GET[page] holds. Hope I will get the solution here.... Currently I am adding the concept of "entitlements" to my website. In the past, my "article.php" script would simply look to the URL for which article was being requested and then load it. However now that I am also adding the concept of "premium content" for "paid members", I need a way to control who sees what. What I am wondering is - from a security standpoint - how much information I should load into the $_SESSION variable. For instance, right now when a user logs in, I think I just store the "memberID" and "FirstName" and possibly "Username". It would be more efficient when a Member logs in to also retrieve their "Membership Plan" and store that in the $_SESSION variable, so that as they browse my website, each page can simply grab $_SESSION['MembershipPlan'] and run that through a function that I need to build and then determine if the user gets to access said page. However, maybe it would be more secure to have it so when a user lands on page XYZ, I would look at their "memberID" and query the database to get their "MembershipPlan"? Any thoughts on each approach? Again, my main concern is *security*, but I also suppose this plays into "performance".
hello, um i got a form that stored an array of variables upon users clicks. it submits to another page fine as an array and i can print them using if(!empty($_POST['langSpoken'])) { echo "\n<p><strong>You selected the following LangSpoken:</strong></p>\n<ul>"; foreach($langSpoken as $lang) { // exclude any items with chars we don't want, just in case someone is playing if(!preg_match('/^[-A-Z0-9\., ]+$/iD', $lang)) continue; // print the lang echo "\n\t<li>" . htmlspecialchars($lang) . "</li>"; } echo "\n</ul>"; } else { echo "\n<p>No items selected</p>"; } it prints a list a of array like english spanish but i need to store each one in one variable so i can store in a database is there a way? thanks //multi query $sql= "SELECT option_value FROM settings WHERE option_name = 'email_temp_issue';"; //1 $sql .="SELECT title FROM books WHERE id = '$bookid';"; //2 $sql .="SELECT fullName from students WHERE email = '$username'"; //3 $sql_run = mysqli_multi_query($connection,$sql); if(mysqli_num_rows($sql_run)>0) { foreach ($sql_run as $row) { $email_template = $row['option_value']; //retrieve from query 1 $title = $row['title']; //retrieve from query 2 $name = $row['fullName']; //retrieve from query 3 } } I am trying to store the data from the multi query into each variable, which I am not sure how to do..Anyone can help? :) Hi, I want to be able to let user type in xml text and it will be parsed and uploaded to db, but it doesn't work, it just keeps redirecting me back to this form below! here is html form: Code: [Select] <html> <body> <form method='post' method='form.php'> <p> <textarea name="pastedXML" rows="10" cols="30"> Please paste your xml file here. </textarea> </p> <p> <input type="submit" value="Convert to SQL" name="textXML" /> </p> </form> </body> </html> Here is script (form.php;I just want to retrieve the contents typed in text area to store to variable...how??) Code: [Select] <?php //get the text in textarea and shred it! if(isset($_POST['textXML'])) print $_POST['pastedXML']; ?> Any help much appreciated! Hi, I want to store files to variable array using glob() like $files[0] = xyz.txt $files[1] = pqr.txt . . . $files[n] = nfile.txt I know how to list files from directory using glob() Code: [Select] <?php foreach (glob("*.txt") as $filename) { echo "$filename size " . filesize($filename) . "\n"; } ?> How can I do that ? Hi everyone - this code is doing my head in! You might want to take a look at the page as it will help me explain it better (www.bradleystokejudoclub.co.uk/inttest.php Basically what I have is a database with the results from international competitions, and I am trying to build a kind of search for it. I have 3 drop down boxes, one with player names, one with competiton and one with year. The only one that works is the competiton. I can do competition + year, but not year by itself, and player doesnt work at all .... this is the relevant code: (before head) <?php include("includes/dbconnect120-gem.php"); include("includes/db_auth_bits.php"); include("includes/db_stp.php"); if($_POST) { if($name == 'select') { $sql1 = ""; } else { if(($award == 'select') && ($year == 'select')) { $sql1 = "r.pname_id = '$pname'"; } else { $sql1 = "r.pname_id = '$pname' AND "; } } if($award == 'select') { $sql2 = ""; } else { if($year == 'select') { $sql2 = "r.comp_id = '$comp'"; } else { $sql2 = "r.comp_id = '$comp' AND "; } } if($year == 'select') { $sql3 = ""; } else { $sql3 = "r.year_id = '$year'"; } if(($sql1 == "") && ($sql2 == "") && ($sql3 == "")) { $where = ""; } else { $where = " WHERE "; } $sql = "select p.pname, i.comp, m.place_name, yr.year_full from intcomp_result r left join playername p on r.pname_id=p.name_id left join intcomp i on i.comp_id = r.comp_id left join place m on m.place_id = r.place_id left join yearname yr on r.year_id = yr.year_id $where $sql1 $sql2 $sql3 order by r.year_id desc, r.comp_id, r.place_id"; $search_result = mysql_query($sql); } ?> (body) <form method="post" action="<?php echo $PHP_SELF;?>"> Name:<select name="name"> <option value="select" selected="selected" >-SHOW ALL-</option> <?php $sql="select distinct r.pname_id, p.pname from intcomp_result r left join playername p on r.pname_id=p.name_id order by p.pname"; $result=mysql_query($sql); while($row=mysql_fetch_array($result)) { $nameid=$row['pname_id']; $pname=$row['pname']; ?> <option value="<?php echo $nameid; ?>"><?php echo $pname;?></option> <?php } ?> </select><br /> Competition:<select name="comp"> <option value="select" selected="selected" >-SHOW ALL-</option> <?php $sql="select * from intcomp order by comp_id asc"; $result=mysql_query($sql); while($row=mysql_fetch_array($result)) { $compid=$row['comp_id']; $comp=$row['comp']; ?> <option value="<?php echo $compid; ?>"><?php echo $comp;?></option> <?php } ?> </select><br /> Year:<select name="year"> <option value="select" selected="selected" >-SHOW ALL-</option> <?php $sql="select distinct r.year_id, y.year_full from intcomp_result r left join yearname y on r.year_id=y.year_id order by y.year_id"; $result=mysql_query($sql); while($row=mysql_fetch_array($result)) { $yearid=$row['year_id']; $year=$row['year_full']; ?> <option value="<?php echo $yearid; ?>"><?php echo $year;?></option> <?php } ?> </select><br /> <input name="Submit" type="submit" class="button" tabindex="14" value="Submit" /> </form> <?php if(isset($search_result)) { while($row = mysql_fetch_array($search_result)) { echo $row['pname'].' - '.$row['comp'].' - '.$row['year_full'].' - '.$row['place_name'].'<br />'; } } ?> Hope you can help! Thanks Gem Hey there, i have a cookie which echos ok but would like to know how to automatically select the corresponding option from a drop down list when the page loads. Normal List <form id="switchform"> <select name="switchcontrol" size="1" class="topusernav" onChange="chooseStyle(this.options[this.selectedIndex].value, 60)"> <option value="none">-----</option> <option value="noir">Noir</option> <option value="crimson">Crimson</option> <option value="forrest">Forrest</option> <option value="ocean">Ocean</option> <option value="petal">Petal</option> </select> </form> works fine but <form id="switchform"> <select name="switchcontrol" size="1" class="topusernav" onChange="chooseStyle(this.options[this.selectedIndex].value, 60)"> <option value="none" <?php if (!(strcmp("none", "$_COOKIE[\"mysheet\"];")) {echo "selected=\"selected\"";} ?>>-----</option> <option value="noir" <?php if (!(strcmp("noir", "$_COOKIE[\"mysheet\"];")) {echo "selected=\"selected\"";} ?>>Noir</option> <option value="crimson" <?php if (!(strcmp("crimson", "$_COOKIE[\"mysheet\"];")) {echo "selected=\"selected\"";} ?>>Crimson</option> <option value="forrest" <?php if (!(strcmp("forrest", "$_COOKIE[\"mysheet\"];")) {echo "selected=\"selected\"";} ?>>Forrest</option> <option value="ocean" <?php if (!(strcmp("ocean", "$_COOKIE[\"mysheet\"];")) {echo "selected=\"selected\"";} ?>>Ocean</option> <option value="petal" <?php if (!(strcmp("petal", "$_COOKIE[\"mysheet\"];")) {echo "selected=\"selected\"";} ?>>Petal</option> </select> </form> gives me a whitespace error any thoughts?? thanks Hey guys i am new around here and i need some help with php. So basically i want to make a submit form and i found a problem for myself here's some images to understand what i am talking about. so here's what i've made at first. This is meant for new data. <p><label>Status :</label> <select name="status" > <option value=""></option> <option value="Ongoing">Ongoing</option> <option value="Completed">Completed</option> </select> basically what I want is when I edit the data, that selection returns the value that was stored in the database rather than just null value. See image below to understand what i am trying to do. i dont really get how to do it with select. Please help me and thanks before. Sorry if my question was already asked before. Tried to search but didnt really found what i am looking for. Hey Guys. I am trying to create a time dropdown and increment eat by an an hour until it is lless than or equal to the end time. I am using the DateTime class to accomplish this.
Now when I am trying to use a for loop to accompish this it does not work. Can anyone please help me out with this issue?
Below is the code that I have
$start_hour = new DateTime("now",new DateTimeZone("America/New_York")); $start_hour->setTime(6,00); $formatted_start_time = $start_hour->format("H:i:s"); $end_hour = mktime(11,45); for($start_hour; $start_hour <= $end_hour; $start_hour->modify("+60 minutes"));{ echo "<select name='cat_display_timeslot'>"; echo "<option>{$formatted_start_time}</option>"; echo "</select>"; }Thanks! Edited by eldan88, 24 August 2014 - 07:21 PM. I am trying to update a mysql table called AvItems with the value 'Torso' in the Equip "section?" I have been through the forums and cannot see anything to match. I dont mind if the page looses the onsubmit() and has a button instead. Though I would like to update the database and link back to the same page: There is a display that shows the item that is currently equiped, I have put this in to show it works, or doesn't as the case may be. Hope I got the code /code right this time. many thanks in advance Andy Curtis Code: [Select] create table Items( ItemID integer unsigned auto_increment primary key, ItemName varchar(20) not null, Type varchar(10), UsedOn varchar(10), ); create table AvItems( AvItemID integer unsigned auto_increment primary key, AvID integer unsigned, ItemID integer unsigned, Equip varchar(8)); <?php $username="root"; $password="MyPassword"; $database="MyDataBase"; $AvName = "AndyJCurtis"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $AvAccR = mysql_query( " SELECT AvID FROM AvAcc WHERE AvName = '$AvName' " ); $AvID = mysql_result($AvAccR, 0, 'AvID'); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// $Torso = mysql_query(" select ItemName from AvItems, Items where AvItems.itemID = Items.itemID AND AvItems.AvID = '$AvID' AND UsedON = 'Body' "); $TorsoE = mysql_query(" select ItemName from AvItems, Items where AvItems.itemID = Items.itemID AND AvItems.AvID = '$AvID' And UsedON = 'Body' AND Equip = 'Body' "); if(mysql_num_rows($TorsoE) != 0) { $TorsoItem = mysql_result($TorsoE ,0,"ItemName"); //mysql_close(); ?> <title></title> <head></head> <body> <form action="http://localhost/CI/Equip2.php" method="post"> <table border=1> <tbody> <tr> <td>Torso<BR> <?PHP echo "$TorsoItem <BR>"; ?> <select name="Torso" onchange="submit();" value =" Update"> <?PHP while($TorsoRow = mysql_fetch_array($Torso)) { echo "<option value=\"".$TorsoRow['ItemName']."\">".$TorsoRow['ItemName']."\n </option>"; } ?> </select> </td> </tr> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// <?php if($_POST['Torso'] == 'Update') { mysql_query("update AvItems set Equip = '' where Equip='Torso'") or die("cant update unequip"); mysql_query("update AvItems set Equip = 'Torso' where ItemID='{$_POST['ItemName']}'") or die("cant update equip"); } ?> /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// </tbody> </table> </.form> </body> </html> Hi, I am trying to create a drop down menu that will select a value that is stored in the database - right now the code creats a dropdown (with nothing selected) - hope someone can help. in the database, the values are stored as --null- Option1 Option2 Option3 my code is Code: [Select] $instruction = $_GET['instruction']; <?php <select id="instruction" name="instruction"> <option value="" <?php if (!empty($instruction) && $instruction == '' ) echo 'selected = "selected"'; ?>></option> <option value="Option1" <?php if (!empty($instruction) && $instruction == 'Option1') echo 'selected = "selected"'; ?>>Option1</option> <option value="Option2" <?php if (!empty($instruction) && $instruction == 'Option2') echo 'selected = "selected"'; ?>>Option2</option> <option value="Option3" <?php if (!empty($instruction) && $instruction == 'Option3') echo 'selected = "selected"'; ?>>Option3</option> </select> <? Hello guys I've hit a problem whicle trying to validate my form. I have 3 drop down boxes where the user chooses from three options. But I can't seem to figure out how to set the validation so the user does not select the same option in each drop down. Can anyone help me solve this please. Thank you. <p><b>Course Choice 1</b> <select name="course1"> <option value="0"></option> <option value="Business computer Systems">Business computer Systems</option> <option value="Business computer Science">Business computer Science</option> <option value="Business computer Science (Games)">Business computer Science (Games)</option> <option value="Business Information Systems">Business Information Systems</option> <option value="Digital Media Development">Digital Media Development</option> <option value="Digital Media">Digital Media</option> </select></p> <p><b>Course Choice 2</b> <select name="course2"> <option value="Leave Blank">Leave Blank</option> <option value="Business computer Systems">Business computer Systems</option> <option value="Business computer Science">Business computer Science</option> <option value="Business computer Science (Games)">Business computer Science (Games)</option> <option value="Business Information Systems">Business Information Systems</option> <option value="Digital Media Development">Digital Media Development</option> <option value="Digital Media">Digital Media</option> </select></p> <p><b>Course Choice 3</b> <select name="course3"> <option value="Leave Blank">Leave Blank</option> <option value="Business computer Systems">Business computer Systems</option> <option value="Business computer Science">Business computer Science</option> <option value="Business computer Science (Games)">Business computer Science (Games)</option> <option value="Business Information Systems">Business Information Systems</option> <option value="Digital Media Development">Digital Media Development</option> <option value="Digital Media">Digital Media</option> </select></p> <div align="centre"><input type="submit" name="submit" value="send request" /></div> //Validate course choice 1 if (!empty($_REQUEST['course1'])) { $course1 = $_REQUEST['course1']; } else { $course1 = NULL; echo '<p><font color="red">Please enter your first choice</font></p>'; } //Validate course choice 2 if (!empty($_REQUEST['course2'])) { $course2 = $_REQUEST['course2']; } else { $course2 = NULL; echo '<p><font color="red">Please enter your second choice</font></p>'; } //Validate course choice 3 if (!empty($_REQUEST['course3'])) { $course3 = $_REQUEST['course3']; } else { $course3 = NULL; echo '<p><font color="red">Please enter your third choice</font></p>'; } //If everything is ok, print the message if ($name && $email && $course1 && $course2 && $course3) { echo "<p>Thank you, <b>$name</b>, You have chosen the following courses for information:<br /><br /> <b>$course1</b><br /> <b>$course2</b><br /> <b>$course3</b></p> <p>We will reply to you at <i>$email</i>.</p>\n"; } else { // One form element was not filled out properly echo '<p><font color="red">Please go back and fill out the form again.</font></p>'; } |