Jump to content


Photo

Get and print <select name ="education"> value


  • Please log in to reply
5 replies to this topic

#1 Xeon-YK

Xeon-YK
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 26 March 2006 - 05:32 PM

Hi, I'm Beggining in PHP.
I Want to ask how to get select value before i submit
here is the code i want get and print out the <select name = "education"> value (in line 3) before i click the submit button so when the page load it will print out selected value in this case Jr.High
Please help me.... thank you

01<?php
02$education = $_POST["education"];
03echo $education;
0i4f (!isset($_POST['submit']))
05{
06?>
07
08
09<form method="post" action="<?php echo $PHP_SELF;?>">
10 <select name="education">
11 <option value="Jr.High">Jr.High</option>
12 <option value="HighSchool">HighSchool</option>
13 <option value="College">College</option>
14 </select>:<br />
15 <input type="submit" value="submit" name="submit"><br />
16</form><br />
17
18<?
19
20}
21else {
22
23echo $education;
24}
25?>

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 March 2006 - 05:41 PM

IF you want to show the current selection of the select box before submitting the form it. Then you'll want to look into Javascript to get the current selected value fo the selectbox.

Something like this:
<?php

if (!isset($_POST['submit']))
{
?>

<script type="text/javascript">

function getEdu(eduValue)
{
    //alert('Form has changed: value = ' + eduValue);
    eduText.innerHTML = "You have selected: " + eduValue;
}

</script>
<div id="eduText">Make a selection below:</div><br />
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" onChange="getEdu(document.getElementById('edu').value)">
  <select name="education" id="edu">
    <option value="Jr.High">Jr.High</option>
    <option value="HighSchool">HighSchool</option>
    <option value="College">College</option>
  </select>:<br />
 <input type="submit" value="submit" name="submit"><br />
</form><br />
<?
}
else
{
    echo $_POST['education'];
}
?>


#3 Xeon-YK

Xeon-YK
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 27 March 2006 - 06:52 AM

thank for reply my question...
but i want ask one question again, how i get the select value before submit and i want store to php variabel???

thank you

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 27 March 2006 - 10:24 AM

You cant do that you will HAVE to submit the form in order for PHP to create the $_POST['education'] variable this will then hold value fo the option that was selected upon submit. PHP will create this variable automatically.

Why do want PHP to create the variable before you submit the form?, as this is impossible to do

#5 Xeon-YK

Xeon-YK
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 27 March 2006 - 03:37 PM

i want to get the select value because from the selected value i want to use the value in mysql_query and view the result before i submit the form

example :

$edu = $_POST["education"];
select * from master_education where level=$edu

can't you help me to solve this problem or you can tell me another solution, thank you...

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 27 March 2006 - 03:59 PM

Well in that case you will want to look in to AJAX and PHP for what you want to do. As you can't do what you want to do with just PHP on its own. Have a look at the [a href=\"http://www.ajaxfreaks.com/tutorials/1/0.php\" target=\"_blank\"]AJAX Tutorial[/a] over at ajaxfreaks.com.

NOTE: You can get help over at ajaxfreaks.com on the AJAX forum no need to signup again to that forum over at ajaxfreaks.com as your account which you use here should work over there as both forums are tied to the same database.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users