Passing Array Data between PHP and Javascript is Undefined [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have an associative PHP array I need to access in Javascript to check the value of manufacturer. I am currently outputting this inline on the page, then reading in a separate JS file.
<?php
$carsData = array();
foreach ($cars as $car) {
$carsData[] = ['id' => $car->id, 'title' => $car->title, 'manufacturer' => $car->manufacturer];
}
$carsDataString = json_encode($carsData);
?>
<script>
carsData = <?php echo $carsDataString . ';';?>
</script>
This works so far - the source code shows exactly as I'd expect:
carsData = [{"id":2,"title":"Astra","manufacturer":"2"},{"id":3,"title":"Tepee","manufacturer":"3"},{"id":4,"title":"C4 Grand","manufacturer":"4"},{"id":5,"title":"Civic","manufacturer":"5"},{"id":6,"title":"Jazz","manufacturer":"5"}];
In my so far feeble attempts to read this in JS, I have the following:
$.each(carsData, function(key, value) {
console.log(key['manufacturer']);
});
In my console, this is outputting undefined, undefined, undefined, undefined. If I execute carsData in the console, I see the objects exactly as I would expect.
So what am I doing wrong? I need to be able to take this and do the following:
$.each(carsData, function(key, value) {
if (key['manufacturer'] = 5 {
$('#manufacturer' + key['manufacturer'].show();
}
});
Please note I have simplified it as the value of '5', because this value actually comes from another script and is printing 5 there as I expect.

To make the js valid you need to add a ; to the end of this line
carsData = <?php echo $carsDataString;?>;

try this code
$.each(carsData, function(key, value) {
console.log(value['manufacturer']);
});

<script type="javascript">
carsData = new Array(<?php echo implode(",",$carsDataString);?>);
</script>

Related

Sending data from javascript to php isn't working: Result null [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
Improve this question
So my javascript file looks like this:
$(document).ready(function() {
var map = {
url: window.location.pathname //get URL without domain
};
$.post("/php/SongOutput.php", {map}).done(function(data){
console.log(map); //Check that the map is correct
console.log(data); //Show the value of $_POST["url"];
})
});
And the bit of php is the following:
echo $_POST["url"];
Now, as you can see what I'm trying to do is sending the current url of the site to php. But the only output I get from console.log(data); is null. How can I fix this?
you need to remove {} from second argument
$.post("/php/SongOutput.php", map).done(function(data){
console.log(map); //Check that the map is correct
console.log(data); //Show the value of $_POST["url"];
})

Why do I get an error when trying to console log my $.each [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Sorry im new to jquery I usually use php/html. Can you tell why my code wont console log the VAR values. I am building an array from my vars when looping through them. I get the error Uncaught ReferenceError: $ is not defined.
Fiddle
var string1 = "tesĀ£$%t";
var string2 = "test";
var string3 = "test";
var string4 = "test";
var check_fields = [string1, string2, string3, string4];
$.each(check_fields, function(index, value) {
if (value.replace(/^[a-z\d\-_\s]+$/i, "") != string) {
console.log(value);
}
});
Your code is fine but jquery file is missing here.
Fiddle link:- In the left side under the external resource just put the jquery cdn path and click on run then check.
https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js
This will resolve your problem.
include the jquery header file in the head your html code
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
Your html file is missing the external jQuery library.
Try adding this to the start of your html file (preferably in the head, of in the case of JSFiddle just paste it into the HTML box):
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
Let me know if this works

Undefined variable with string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Inside profuser variable i have string "nick".
echo a href="#" onclick="prof('.$profuser.')">
#UP I DELETED THE SIGN BEFORE a BECOUSE IT WAS DESTROYING WHOLE CODE IN STACK OVERFLOW
It should be sent here:
<script>
function prof(profuser){
var xmlhttp=new
window.XMLHttpRequest(); xmlhttp.open("GET", "user.php?user=" +
profuser, true); xmlhttp.send(); }
</script>
And then to this file called user.php:
$thisuser = $_GET['user']; echo $thisuser;
But this code shows me that string "nick" is not defined.
Could anyone tell me what is wrong about it please?
And if there are more errors in this code tell me please.
As error say nick is treated as undefined variable. It should be seen as string so you need to add quotes:
onclick="prof(\'' . addslashes($profuser) . '\')"
According to #p.s.w.g, you should use addslashes() to escape quotes from php variable in case there were any;)
try this.
onclick="prof('<?php echo $profuser; ?>')"
or
onclick="prof('<?= $profuser; ?>')"
I think you are mixing PHP and javascript? Hard to tell from the limited code that was posted.
Your issue is most likely a quoting one, if you are printing the whole onclick bit try this
echo 'onclick="prof(\''.$profuser.'\')"';
This is because you are probably getting something like this in your source.
onclick="prof(nick)";
And nick is a string not a javascript variable, which will be undefined.

Importing PHP variables into Javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
This question may have already been answered, but how can I import variables from PHP into a segment of Javascript in HTML? I have the following PHP variables (these are parameters passed into a PHP script):
$user = $GET[user];
$pass = $GET[pass];
And I am trying to access these variables with the following references in my Javascript code:
var user = "<?=$user?>";
var pass = "<?=$pass?>";
However, when I check the values of these variables with console.log I get the following result:
user =
pass =
Where am I slipping up? It seems like I just can't read these variables from PHP.
Multiple bugs:
$user = $_GET['user'];
^----^----^--- missing
var user = "<?=$user?>";
^^^^^^^^^^---nasty and can break JS.
Never dump arbitrary text from PHP into a JS code block. One single JS metacharacter and you've introduced a syntax error and the entire JS block is killed. Always use json_encode():
var user = <?= json_encode($user) ?>;
Change:
$user = $GET[user];
$pass = $GET[pass];
To:
$user = $_GET['user'];
$pass = $_GET['password'];

JSON parsing not working in jQuery [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I have a text field with id search_json on a form that contains:
{"standard_id":"2","attribute1":["","Stan"],"attribute2":[""],"attribute3":["","Air Force"],"attribute4":["","Bob"],"selected_index":3}
Upon some event, call it a button click, I want to:
1) Read in that JSON and parse it into an object:
search_json = $.parseJSON($("#search_json").val())
2) Remove "attribute1" from the object
delete search_json["attribute1"]
3) Write the JSON back out to the text field:
$("#search_json").val(JSON.stringify(search_json))
I'm kind of surprised it's not working, though. The delete call does nothing. As a matter of fact,
search_json.hasOwnProperty("attribute1")
returns false. And yet I can log the object to console and it is indeed an object with those values. What is going wrong here?
UPDATE: Actually,
search_json.hasOwnProperty("attribute1")
DOES work. But, if I get the attribute name from another text field, like so:
attribute_name = $("#attribute_name").attr("id")
and:
console.log attribute_name
shows "attribute1", then this does NOT work:
search_json.hasOwnProperty(attribute_name)
returns FALSE. Mystifying.
I don't get it. I'm using your fiddle code and everything is correct
http://jsfiddle.net/ddQbe/1/
The final object is:
attribute2: Array[1]
attribute3: Array[2]
attribute4: Array[2]
selected_index: 3
standard_id: "2"
attribute1 was delete correclty
I would try creating a new result_json object.
var result_json = {};
for (var prop in search_json) {
if (prop !== 'attribute1') {
result_json[prop] = search_json[prop];
}
}
$("#search_json").val(JSON.stringify(result_json));

Categories

Resources