Jump to content

Archived

This topic is now archived and is closed to further replies.

Xeon-YK

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

Recommended Posts

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?>

Share this post


Link to post
Share on other sites
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:
[code]<?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'];
}
?>[/code]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
You cant do that you will [b]HAVE[/b] 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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.