I'm using Laravel and I have two php variable in my blade view like this:
$dropDown = $formDataValue['name']
$emptyDropDown = $formDataValue['name'];
And I create a Javascript function with two parameters in same blade view:
function autoFill (dropDown, emptyDropDown) {
$("#" + dropDown ).change(function() {
$.ajax({
type: "GET",
url: "",
success: function(response){
var dataLength = response.length;
$("#" + dropDown).empty();
$("#" + dropDown).append("<option value=''>Select</option>");
for( var i = 0; i< dataLength; i++){
var id = response[i].id;
var name = response[i].name;
$("#" + dropDown).append("<option value='"+id+"'>" + name + " </option>");
}
}
});
});
}
Then I call this:
<?php echo "<script> autoFill(".$dropDown.", ".$emptyDropDown."); </script>"; ?>
Problem is they can pass the parameters to function! I check element in browser and I see when I call this function, it's have parameters. But in the script tag, there nothing!
How I can fix this?
Thank you very much!
I'm using Laravel and I have two php variable in my blade view like this:
$dropDown = $formDataValue['name']
$emptyDropDown = $formDataValue['name'];
And I create a Javascript function with two parameters in same blade view:
function autoFill (dropDown, emptyDropDown) {
$("#" + dropDown ).change(function() {
$.ajax({
type: "GET",
url: "https://api.myjson./bins/bcnss",
success: function(response){
var dataLength = response.length;
$("#" + dropDown).empty();
$("#" + dropDown).append("<option value=''>Select</option>");
for( var i = 0; i< dataLength; i++){
var id = response[i].id;
var name = response[i].name;
$("#" + dropDown).append("<option value='"+id+"'>" + name + " </option>");
}
}
});
});
}
Then I call this:
<?php echo "<script> autoFill(".$dropDown.", ".$emptyDropDown."); </script>"; ?>
Problem is they can pass the parameters to function! I check element in browser and I see when I call this function, it's have parameters. But in the script tag, there nothing!
How I can fix this?
Thank you very much!
Share Improve this question edited Jun 6, 2019 at 7:49 Lakhwinder Singh 5,5825 gold badges29 silver badges52 bronze badges asked Jun 6, 2019 at 7:21 TomatoTomato 7793 gold badges16 silver badges39 bronze badges 3-
1
autofill(city, ward)
would be passing the two variablescity
andward
into the function. The rest of your code however seems to indicate that you probably rather want to pass text strings to your function here. – 04FS Commented Jun 6, 2019 at 7:25 -
1
Put your
city
andward
to quotes, as there is no such variables:<?php echo "<script> autoFill('{$dropDown}', '{$emptyDropDown}'); </script>"; ?>
– Justinas Commented Jun 6, 2019 at 7:27 - Thanks for your replies. I understood! – Tomato Commented Jun 6, 2019 at 7:31
4 Answers
Reset to default 3Seems like you forgot about quotes, when you are passing strings into function.
<script>
$(document).ready(function () {
autoFill('{{ $dropDown }}', '{{ $emptyDropDown }}');
});
</script>
You can do it this way, but you shouldn't, because Laravel Blade do it for you itself.
<?php echo "<script> autoFill('".$dropDown."', '".$emptyDropDown."'); </script>"; ?>
You can assign the php value in js variable in blade file like below:
<script>
var dropDown = '{{$formDataValue['name']}}';
var emptyDropDown = '{{$formDataValue['name']}}';
</script>
and then use js file and move the autoFill() function in js file and call that js file in blade using . and call autoFill(dropDown, emptyDropDown); in js file on document.ready Like:
$(document).ready(function() {
autoFill(dropDown, emptyDropDown);
});
if you are using laravel try this
<script type="text/javascript">
autoFill("{{$dropDown}}","{{$emptyDropDown}}");
function autoFill(dropDown,emptyDropDown) {
console.log("hi");
}
Hope its helpfull
not using core php
<script type="text/javascript">
autoFill("<?=$dropDown?>","<?=$emptyDropDown?>");
function autoFill(dropDown,emptyDropDown) {
console.log(dropDown);
console.log(emptyDropDown);
}
You did not render the values as strings, so javascript interprets them as variables, which are not defined.
so instead of autofill(city,ward)
you want autofill('city','ward')
1000 ways to rome, this would be one:
<?php echo "<script> autoFill('$dropDown', '$emptyDropDown'); </script>"; ?>