Getting List Using Foreach In Javascript Inside Php Codeigniter
I have a JS function inside my PHP view file. What I want to do is enable a button when the function verifies that the input does not exist in the database. So here's my function:
Solution 1:
Look at your HTML-Code that is generated than you see your mistake.
If you have for example a Array ["name" => "test"], ["name" => "test2"] than you view generates the following HTML:
if ($('#edit-name').val() == 'test') {
name = 0;
$('#warning').html('Name already exists');
}
else {
$('#warning').html('');
name = 1;
}
if ($('#edit-name').val() == 'test1') {
name = 0;
$('#warning').html('Name already exists');
}
else {
$('#warning').html('');
name = 1;
}
You see "else" is always true.
You need to write:
<?$stringarray = "";
foreach($name_listas$list){
$stringarray .= '"'.$list->name.'",';
}
// Del last ,$stringarray = substr($stringarray,0,-1);
?>var names = [<?phpecho$stringarray;?>];
if (names.indexOf($('#edit-name').val()) != -1) {
name = 0;
$('#warning').html('Name already exists');
}else {
$('#warning').html('');
name = 1;
}
Post a Comment for "Getting List Using Foreach In Javascript Inside Php Codeigniter"