Boby Boby - 6 months ago 10
PHP Question

Passing '&' and space javascript

Good day, sir. I'm trying to pass my JS variable to a PHP function. I don't know how to say it, my English is bad. Please check my script first,

My JS. I only know to change the space.

<script>
function names()
{
var dept=$('#dept').val();
alert(dept);
dept=dept.trim().replace(/ /g, '-');
alert(dept);
$.post(
'<?php echo base_url();?>index.php/penilaian/list_dropdown/'+ dept,
{ dept:dept },
function(data){
$('#listnama').html(data);
}
);
}
</script>


And my php script

function list_dropdown($deptcode)
{
$newdeptcode = str_replace("-"," ",$deptcode);
echo $newdeptcode;
}


When dept value has a space, like "A B", its working fine, but when dept value has an & like "A&C" I can't receive it. So, how i can replace "&" with my JS.



Answer

You should pass &amp; instead of & for the dept.

dept.replace(/&/g, '&amp;');
Comments