• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

QUESTION Attribute with js window.location won't work

bencouti

New member
Messages
1
Likes
0
Points
1
#1
I, I have a Php script I made to generate a drop down list of dates.

I use Javascript to open a new url with php variables in it when an element is selected in this list.

I use <?php echo $variable ;?> in a window.location function.

When the new URL is generated, the echo won't add the attribute value in it.

My code is :

PHP:
<?php
    $hostname="XXX";
    $db="XXX";
    $Username="XXX";
    $Password="XXX";

    $conn=new PDO("mysql:host=$hostname;dbname=$db",$Username,$Password);
    $sql= "SELECT CONCAT(display_header,' - ',orig_header) AS date_list FROM wp_wpdatatables_columns WHERE display_header NOT LIKE 'Date%' AND orig_header LIKE 'date%' AND table_id = $id_table";
    
    try{
        $stmt=$conn->prepare($sql);
        $stmt->execute();
        $results=$stmt->fetchAll();
    }
    catch(Exception $ex){
        echo($ex -> getMessage());
    }

?>

<html>
<center>
<label>
    <select name="select1" id="select1">
        <option>-- Sélectionner la date --</option>
        <?php foreach ($results as $output) {?>
        <option><?php echo $output["date_list"];?></option>
        <?php }?
    </select>
</label>
</center>
<script type="text/javascript">

var urlmenu = document.getElementById( 'select1' );
 urlmenu.onchange = function() {
      window.location = ( "/listes-XXX/?table=<?php echo $variable ;?>&date=" + this.options[ this.selectedIndex ].value );
 };
</script>
</html>
I use this snippet in my page : [wbcr_php_snippet id="929" variable="wp_wpdatatable_7" id_table="21"]

You can see in the attached images the generated URL and the correct URL if it worked as I want.


I tried to put an echo $variable in the script and it returned the correct value "wp_wpdatatable_7".


I tried replacing the attribute "variable" by an other variable in the Javascript, I defined it at the start of the script : $var_table = "wp_wpdatatable_7";

It generated the correct URL.


I don't understand why the echo in the javascript won't work with the attribute but work with a standard php variable.


Any help would by wonderfull.

Best Regards,

Benoit COUTINEAU
 

Attachments

alexkovalev

Program developer
Staff member
Messages
267
Likes
19
Points
18
#2
Hi,

Please try to rename variable and attribute names with a prefix. For example: prefix_variable

[wbcr_php_snippet id="929" prefix_variable="wp_wpdatatable_7" id_table="21"]

Code:
<?php echo $prefix_variable; ?>
You need to understand that there are global variables that the plugin will not overwrite. The variable name can often be used by plugins, themes for example.

Best regards, Alex
 

Attachments