简体   繁体   中英

URL Updating in Drupal and AJAX

I have a select list on my form as the following

            <form name="source_form">
                <select id="sel_source_zero" name="sel_source_zero">
                <?php
                $sources = _ajax_get_news_sources();
                foreach ($sources as $key => $value) {
                    print ("<option value=\"$key\">$value</option>\n");
                }
                ?>
                </select>
                <input type="submit" value="Submit" />
            </form>

When the user selects a value from the list, I want to take that value and pass it into a PHP function and return a list of IDs. Once I have the source value and the IDs, I want to update my URL with those values. My entire code is as follows

           <?php
                   $source_var = $_GET['sel_source_zero'];
                   $action_zero = "/news/date/"._ajax_condition_ids_final_unique_plus_seperated($source_var)."/$source_var";
            ?>

            <script type="text/javascript">
            $("#sel_source_zero").change(function (){
            $.ajax()
            });
            </script>

            <form name="source_form" method="get">
                <select id="sel_source_zero" name="sel_source_zero">
                <?php
                $sources = _ajax_get_news_sources();
                foreach ($sources as $key => $value) {
                    print ("<option value=\"$key\">$value</option>\n");
                }
                ?>
                </select>
                <input type="submit" value="Submit" />
            </form>

I have approached this problem by first trying to store the selected value ($("sel_source_zero").value()) into a PHP variable using AJAX. This is where I got stuck. I am trying to use $.ajax() function from jQuery appended to Drupal, but I'm not sure how to approach this. If I can some how store the JavaScript variable into PHP variable then I will be able to use my new converted to PHP source variable and pass that into my function to get the list of IDs and then update my URL. But I'm not even sure if this is the right approach. If anyone can point me in the right direction to how I can update my URL with the selected value and returned PHP values returned from a function that uses the selected value as it's parameter, I would greatly appreciate it.

That is impossible to communicate with the php which has been executed and dead. I'm not 100% sure what you want to do. To guess, you want to redirect to a new url when user select "#sel_cource_zero".

If so (or similar), the answer is as following:

form.php:

<script type="text/javascript">
        $("#sel_source_zero").change(function (){
                var sel = $(this).val();

                $.ajax({type:"GET", data: {'sel_source_zero': sel},
                        url: "ajax.php", dataType: "json", success:
                        function(result){
                                location.href = result;
                        }
                });
        });
</script>

<form name="source_form" method="get">
        <select id="sel_source_zero" name="sel_source_zero">
        <?php   
                $sources = _ajax_get_news_sources();
                foreach ($sources as $key => $value) {                                                                      
                        print ("<option value=\"$key\">$value</option>\n");
                }       
        ?>      
        </select>
</form> 

ajax.php:

<?php

$source_var = $_GET['sel_source_zero'];
$action_zero = "/news/date/"._ajax_condition_ids_final_unique_plus_seperated($source_var)."/$source_var";

echo json_encode($action_zero);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM