0

I am passing vlues to javasript function from PHP in following manner:

<select onChange="display_databody(<?=$manufacturers_id?>,<?=$modelID?>,this.value,<?=**$month_year**?>);">

Here $month_year is a string obtained from MySQL.

When javascript function is getting this value it is getting something like -8 or -13.

I have tried json_encode() but it is also not working.

4

3 回答 3

1

You could use addslashes to escape the variables:

<select onChange="display_databody('<?= addslashes($manufacturers_id) ?>',
    '<?= addslashes($modelID); ?>', this.value, '<?= addslashes($month_year); ?>');">

Or use jQuery and htmlspecialchars:

<select onChange="display_databody(this)" 
    data-manufacturer="<?= htmlspecialchars($manufacturers_id); ?>" 
    data-monthyear="<?= htmlspecialchars($month_year); ?>">

Then inside your function:

function display_databody(dropdown) {
    var $dropdown = $(dropdown),
    dropdown_value = $dropdown.val(),
    manufacturer_id = $dropdown.data('manufacturer'),
    month_year = $dropdown.data('monthyear');

    // your code
}
于 2012-05-27T08:08:04.440 回答
1

I had a brainwave and guessed that $month_year is a string like 1-12 for January 2012. This would result in your HTML becoming something like

 <select onChange="display_databody(2,4,this.value,1-12);">

and Javascript will evaluate that value (as -11 here) when it executes the function.

Wrap your string values in single quotes. You may need to do that with the first two as well if they aren't entirely numeric. Line split here for clarity: you will probably join it back together.

 <select 
     onChange="display_databody(<?=$manufacturers_id?>,
     <?=$modelID?>,
     this.value,
     '<?=$month_year?>');">
于 2012-05-27T08:18:18.597 回答
-1

You have to use echo to write the variable

<select onChange="display_databody(<?php echo $manufacturers_id ?>,<?php echo $modelID ?>,this.value,<?php echo $month_year ?>);">
于 2012-05-27T07:57:33.503 回答