user3806613
user3806613

Reputation: 510

how to echo select option value in the php page without AJAX?

I need to find a way to echo the selected option value in the page.

I do not want to use AJAX or javascript if possible.

is this possible using PHP only?

basically when an option is selected, the value of that option should be echo-ed on the page.

My current code is this:

<select name="userpages" id="upages">
    <?php
    foreach ($postResults as $postResult) {
            echo '<option value="'.$postResult["page_id"].'">'.$postResult["name"].'</option>';
        }

    ?>
</select>

<?php echo $postResult["page_id"]; ?>

this code will only echo the first selected option value and it won't echo the rest of them if they are selected.

could someone please help me out with this?

any help would be appreciated.

Upvotes: 0

Views: 17719

Answers (2)

andrew
andrew

Reputation: 9583

This is about as javascriptless as you can get

<?php if (isset($_POST['userpages'])){
   header ("location:somepage?id=$_POST[userpages]");
   exit;
}

<form method="post">
  <select name="userpages" id="upages">
    <?php
    foreach ($postResults as $postResult) {
            echo '<option value="'.$postResult["page_id"].'">'.$postResult["name"].'</option>';
        }

    ?>
</select>
<input type="submit" value="load page">
</form>

<?php echo $postResult["page_id"]; ?>

you'll have to add an onchange event to the select if you dont want a submit button

Upvotes: 1

Harshal
Harshal

Reputation: 3622

SELECT event is client side activity so we can not get the values of changes using PHP, SO you have to use Jquery for that. My solution is without AJAX but not without jquery

        //show the remaining amount 
        $("#upages").live("change", function(event){
            $("#sel_val").html($(this).val());
        });

and in your HTML code you should have an HTML element with id sel_val.

like <p id="sel_val"></p>

Upvotes: 0

Related Questions