简体   繁体   English

在Drupal和AJAX中更新URL

[英]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. 当用户从列表中选择一个值时,我想获取该值并将其传递给PHP函数并返回ID列表。 Once I have the source value and the IDs, I want to update my URL with those values. 有了源值和ID后,我想用这些值更新URL。 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. 我通过首先尝试使用AJAX将选定的值($(“ sel_source_zero”)。value())存储到PHP变量中来解决此问题。 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. 我试图使用附加到Drupal的jQuery中的$ .ajax()函数,但是我不确定如何处理此问题。 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. 如果可以将JavaScript变量存储到PHP变量中,那么我将能够使用新的转换为PHP的源变量并将其传递给函数以获取ID列表,然后更新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. 如果有人能正确地指导我如何使用所选值更新URL,以及如何从使用所选值作为参数的函数返回的PHP值中返回PHP值,我将不胜感激。

That is impossible to communicate with the php which has been executed and dead. 与已经执行并已失效的php通信是不可能的。 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". 猜测一下,您想在用户选择“ #sel_cource_zero”时重定向到新的URL。

If so (or similar), the answer is as following: 如果是这样(或类似的结果),答案如下:

form.php: 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: 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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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