1

I currently have a javascript popup div that contains a select object that is populated from a POST variable that was generated from the main page. Is it efficient to pass data with html via post to pass data to a popup? Or is there a better way to do this?

main page

< ?php 
require('testclass.php);
$obj = new testclass();
foreach ($obj->getlist as $listobj)
{
$output .= "<option>" . $listobj['name'] . "</option>";
?>

main page javascript

<script type=text/javascript">
$(document).ready(function () {
$("#a_popup").click(function () {
$("#div_popup").load("popup.php", {"list" : "<?php echo $output ?>"});
});
});

popup.php

<select> <?php echo $_POST['list'] ?> </select>
2
  • Why wouldn't you pass an id or something and let popup.php handle the processing? Seems more sane than passing a huge html string to a file that only appears to wrap the string in a select tag. Alternatively, write the select tag to the html and hide it. Use the click listener to reference the hidden id and do with it as you please from there Commented Oct 1, 2013 at 17:37
  • you can send a response back to page and display elements with php/html Commented Oct 1, 2013 at 17:38

1 Answer 1

1

You can send data back to page and do something like this

   <select name="what ever you want">
                            <option value=""></option>
                            <?php
                            foreach ($values_from_page_you_sent as $key => $value)
                            {

                                    echo '<option value="' . $key . '">' . $value . '</option>';
                            }
                            ?>
                        </select>

Something like this you can modify it according to your use values_from_page_you_sent are values that you send from some X page to this page.

Edit: if you are looking Jquery/JS based solution then that is another scenario which you can do some thing like this

jQuery.each(data, function(key, value) {
            jQuery('select[name="' + populatedElement + '"]')
                    .append(jQuery("<option></option>")
                    .attr("value", key)
                    .text(value));
        });
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the info. I may just use Json to transfer the data over to the popup page. Sending html tags via the post method is a little over kill.
well if you think it works for you then use my Jquery part using JSON and then ...(if it works :))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.