-1

I want to send a value from php page that will select the value and html page show that selected value.

Here is my code:

Blood Group: 
<select name="bloodgp" <?php  echo($data['user_bloodgroup']==$bloodgp)?'checked':'' ?>>
    <option value="A+">A+</option
    <option value="B+">B+</option>
    <option value="AB+">AB+</option>
    <option value="O+">O+</option>
    <option value="A-">A-</option>
    <option value="B-">B-</option>
    <option value="AB-">AB-</option>
    <option value="O-">O-</option>
</select>
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
oli
  • 1
  • 2

3 Answers3

0

If you just want to send data on HTML page then it will be possible through jquery. You can use below code for your requirement.

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"
    type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $(document).on('click', '#bloodgp', function() {
            var get_selected_option_value = $(this)val();
            window.location = 'your_html_page_name/bloodgp=' + get_selected_option_value;
        });
    });
</script>
</head>
<body>
    Blood Group: 
    <select name="bloodgp" id="bloodgp" <?php echo($data['user_bloodgroup']==$bloodgp)?'checked':'' ?>>
    <option value="A+">A+</option
    <option value="B+">B+</option>
    <option value="AB+">AB+</option>
    <option value="O+">O+</option>
    <option value="A-">A-</option>
    <option value="B-">B-</option>
    <option value="AB-">AB-</option>
    <option value="O-">O-</option>
</select>
</body>
</html>

You can get the value from URL using above code.

Above code is for your requirement. But i am suggesting you to send the value to php file instead of HTML file. it will be more feasible for you.

If you want send data to php file then you can use ajax for that. You can find the below code with ajax.

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"
    type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $(document).on('click', '#bloodgp', function() {
            var get_selected_option_value = $(this)val();
            window.location = 
            $.ajax({
                url: 'your_page_page_name.php',
                type: 'POST',
                dataType: 'json',
                data: {bloodgp: get_selected_option_value},
            })
            .success(function(response) {
                console.log("you have successfully sended the data");
            })
        });
    });
</script>
</head>
<body>
Blood Group: 
<select name="bloodgp" id="bloodgp" <?php echo($data['user_bloodgroup']==$bloodgp)?'checked':'' ?>>
    <option value="A+">A+</option
    <option value="B+">B+</option>
    <option value="AB+">AB+</option>
    <option value="O+">O+</option>
    <option value="A-">A-</option>
    <option value="B-">B-</option>
    <option value="AB-">AB-</option>
    <option value="O-">O-</option>
</select>
</body>
</html>

You can get value by using <?php print_r($_POST); ?> on your recieving php page.

Harsh Barach
  • 947
  • 9
  • 18
0

Considering your <select> is in a <form>, depending on the method (get or post), when submitting, the value will be store in $_GET or $_POST.

// Example with post
<?php
//Verifying the form has been submitted
if (isset($_POST) && isset($_POST['submit'])) {
    // here is the value you want
    $select_value = $_POST['bloodgp'];
}
?>

If you are looking to do something when the user choose an <option> and not on submit of a form, this will be done with JavaScript.
You can find answers here as I did myself sometime ago (read the 2 firsts answers).

Next time, please search for already answered questions, post your code, and explain more what you are trying to do and how.

Community
  • 1
  • 1
AymDev
  • 6,626
  • 4
  • 29
  • 52
0

Please do not bother to put your code here, which is easy to understand.

Well you want to send a blood group which is selected by the user and now want to show this in html, the common format of select box is

<select name="bloodgp">
<option value="A+">A+</option
<option value="B+" selected >B+</option>
</select>

this selected attribute will show this on html not checked, so you can use javascript to check all the options value which on match then add this attribute or you can easily print it as first option like

<select name="bloodgp">
<option value = "<?php echo $data['user_bloodgroup'];?>" selected><?php echo $data['user_bloodgroup'];?></option>
<option value="A+">A+</option
<option value="B+">B+</option>
</select>

which will make that option double. In my case i will like to use js to filter that option.

M. K Hossain
  • 807
  • 1
  • 12
  • 28