Object expect in in java-script - javascript

I am getting the constant error that it is expecting an object.
I call a function onchange:
<select name =<?php echo $dagen[$i]."1"; ?> id=<?php echo $dagen[$i]."1"; ?> onchange="weekchange(this, <?php echo $i; ?>)" />
Here is my javascript function:
function weekchange(selected, weekkeuze)
{
var samen;
var id = weekkeuze;
var week = selected.value;
samen = "id="+id+"&week="+week;
alert(samen);
submit_javascript("GET","capaciteitberekening.php?"+samen,"","true"); //row 13
}
In this function I receive the data from the onchange correctly since it shows the correct values in the alert(samen), with the last sentence I should be sending this data to capaciteitberekening.php. here I am not sure what is going on. On capaciteitberekening.php I use this code to catch the value:
$filt['week'] = $_GET['week'];
$filt['id'] = $_GET['id'];
echo $id, $week;
But as you might expect I do not receive anything, instead i get an error on my browser saying that it is expecting a object in row 13.
I hope you can help me, since I have no clue how to solve my issue.
Thanks in advance.

There could be some issue with your php code at line 13.
To check if the values are posted correctly,
try this at the beginning of capaciteitberekening.php.
<?php
var_dump($_GET);
exit;
?>
If It works, then its sure that there could be some other issue with your php code in capaciteitberekening.php, but request is working fine.

thanks for reminding me about the function, I am working om someone else his code so I didn't look properly at that function. and I had to add a ID and not a class, sorry for making such a basic mistake, and thanks

Related

PHP Variable from String with Javascript popup

In /var/www/html/msg.txt is just one word "Test".
Can someone please tell me why this doesn't work?
echo "<script>alert('$tes');</script>";
Complet php code:
<?php
$ma="Test";
$tes = file_get_contents("/var/www/html/msg.txt");
echo "$tes"; //works
echo "<script>alert('$ma'); //works
</script>";
//but if this
echo "<script>alert('$tes'); // doesn't work!!!! Why?
</script>";
?>
how can I do it?
Most likely you have a line break in that file, so the resulting code is:
<script>alert('Test
');
</script>;
Which is wont work, you can confirm this by looking at the source, and/or it will be erroring out in the browser console.
The problem is with your file_get_contents. Probably you are setting an incorerect path, or the file that you are trying to access doesn't have the right permissions

PHP variable does not parse correctly using accepted answers for passing into javascript

It would seem this is not a new question, but after reading all the answer I can find, I'm still stuck.
I have a bit of php into which a snippet of javascript has been inserted:
<?php
function post_iframe_message( $msg ) {
?>
<script>
window.onload = function msgchild() {
console.log("called msgchild");
// obj = JSON.parse(JSON.stringify("Hello from parent"));
var val = "<?php echo $msg ?>";
obj = JSON.parse(var);
var e = document.getElementById('iframepm');
console.log(e);
e.contentWindow.postMessage(obj,'*');
}
</script>
<?php
echo "<script>iframepm.contentWindow.postMessage($msg,'*');</script>";
}
?>
I am calling this function with:
$current_user = wp_get_current_user()
post_iframe_message(json_encode($current_user));
I don't think the Wordpress bit is relevant other than to provide context for the variable object $current_user.
When this loads, I receive an 'Uncaught SyntaxError: Unexpected identifier' error in the DevTools (Chrome) console.
If I comment out the (attempted) parsing of the $msg object and simply pass the string literal "Hello from parent" everything works great i.e. postMessage succeeds without error.
This is so simple that it kills me to ask the question here, but at this point I'm just tucking my pride up in a little ball and putting it in the corner. What obvious bit am I missing?
My line in question is line 29 of my code. The syntax error is being reported on line 346 which hints at something really stupid like a missed quote. Again, swapping comments to:
obj = JSON.parse(JSON.stringify("Hello from parent"));
// var val = "<?php echo $msg ?>";
// obj = JSON.parse(var);
and everything works. What am I missing?
Thanks in advance and I hope this has provided you with a good newbie chuckle. :-)

Jquery takes last output as value

I have some code and it is for sending comments to database.
And I have jquery and ajax code:
<script type="text/javascript">
$(document).ready(function() {
$('#comm1').keypress(function(event) {
var key = (event.keyCode ? event.keyCode : event.which);
if (key == 13) {
var comment = $('#comm1').val();
var fromstatid = '<?php echo $status->fromid; ?>';
var status = '<?php echo $status->id;?>';
var fromid = '<?php echo $frid; ?>';
$.ajax({
method: "POST",
url: "d/includes/counts.php",
data: {u: fromid, status: status, comment: comment, fus: fromstatid},
success: function(status) {
$('#comm1').val('');
}
});
};
});
});
</script>
#comm1 is textarea for comment ..
For php I have like basic output from database and the problem is that my jquery use only last output from database as value. In other words where ever I type a comment it send to database for the last output. It works if I have button for Reply and then it sends me to new page or something where I can limit output from database to 1 or to only that where I want to comment. But can I do it here on the page where are all comments... Also I was trying to set id as some word + php id from database for that output but it seem like when I add this to jquery code noting going to work....
The code I tried is : $("#something<?php echo $status->id; ?>").keypress.....
It stops the script it looks like the whole script is not working...
I asked similar question but no one answered me the right question... Also I was thinking of can I like make foreach in jquery here on this code.. And say for each output do something. I really don't know why it use only last output on page as value but it will help a lot if someone have some explanation?
EDITED:
Or do I need to echo for each output the jquery code?
UPDATE:
Tried by echoing the whole jquery code and it is not working....
NEW:
As you asked I have d/includes/counts.php file and the index.php
Inside counts.php i just input in database values send via ajax...
Inside index.php I said I have basic foreach database output to echo something like $status->text and for comment to echo textarea where I can type comment for text... Code is too long and I dont have problems with that code I have problems with connecting jquery code to all outputs not only last one....
Why does my var status = '<?php echo $status->id; ?>'; AND var fromstatid = '<?php echo $status->fromid; ?>'; use only last output values? Can I make some var inside echo for each output and use that var here in code to navigate which one is commented on?
I founded that also the script work only on one output like if I try to comment on the other one and press ENTER it wont work....
You must know that only the last output of the PHP file will be retrieved. So I'm wondering what did you output in the PHP file ? Can you post the PHP code please ? (Sorry, for answering here but not enough points to comment).

Push PHP Variable to Javascript Array

I want to push a php variable to javascript. The code is below but does not seem to work. Can anyone show me how to do this?
ticks.push(<?php echo json_encode($new); ?>);
Is this what you're looking for?
ticks.push(JSON.parse('<?= json_encode($new); ?>'));
Or broken down:
var json = '<?= json_encode($new); ?>';
var obj = JSON.parse(json);
ticks.push(obj)
also addressed in this issue:
Accessing an array in PHP from Javascript/jQuery
can you try this
var tricks = [
<?php
foreach ($new as $n) {
echo '"'.$n'", ';
}
?>
];
you can use websocket to do such like functionality; in it you can send the result back to the client from the php code when it is ready one good example on that is how stackoverflow notify you when there is new question, or when they notify you if the post is edited.

Ajax and jquery not sending data correctly to php

I created a basic form that uses jquery (ajax) to send data to php. PHP should insert a new record based on the data to a mysql database. The reason for this is because I want to make insertions to the database without having to submit the whole form and then use the submit action for something else later. It seems that the jquery works fine since the alert() shows the correct output for the variables, but the PHP does not insert the data and I don't get an error. I can't figure out why this isn't working? I think it is a problem with my $post() because the function underneath does not execute but I can't pinpoint the error. Any help debugging this would be really appreciated. Or if anyone knows another way to get the same functionality that would be great too? Thanks. (The code below works fine now. I figured out it was a type cast error, and I fixed it. Hopefully someone can find this useful!)
<script type="text/javascript">
function submitgrade(){
alert("In it");
var classID = $("#classSelect").val();
var student = $("#studentSelect").val();
var exam = $("#Exam").val();
var grade = $("#grade").val();
alert(classID+" - "+student+" - "+exam+" - "+grade);
$.post('submitgrade.php',{postclassSelect:classID,poststudentSelect:student,postExam:exam,postgrade:grade}, /*1*/
function(data){
$("#grade").html("");
});
};
</script>
<?php /*submitgrade.php*/
$con=mysqli_connect("localhost","root","","studentbase");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$classID = $_POST['postclassSelect'];
$studentID = $_POST['poststudentSelect'];
$examID = $_POST['postExam'];
$grade = $_POST['postgrade'];
echo $studentID[0]." examID: ". $examID[0];
$gradequery = "INSERT INTO grade VALUES(".intval($studentID).", '".$classID."', ".intval($examID).", ".intval($grade).");";
$result = $con->query($gradequery);
while($row = $result->fetch_assoc())
{
echo "<br /><p>Grade of ". $grade." submitted for exam ". $row['exam_id'] ." in ". $row['class_ID'] ."</p>";
}
?>
Have you include this line in your html page ??
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
An example is here again, may help you
<script>
$(document).ready(function(){
$("input").keyup(function(){
txt=$("input").val();
$.post("my_page.asp",{suggest:txt},function(result){
$("span").html(result);
});
});
});
but your code seems correct too buddy !!
I suggest to continue debugging by attaching an error handler to your $.post call, your code could look this:
$.post('submitgrade.php', {postclassSelect:classID,poststudentSelect:student,postExam:exam,postgrade:grade})
.done(function(response) {
// success
}).fail(function(response) {
// failure
});
Further more you should check:
Is the script running on a server? ajax might not work on a file:/// address
Is the path from javascript location to php file correct?
what do the browser developer tools say about the request that is initiated?
I fixed it. It was actually just a syntax error in my SQL and a type difference error with one of my database columns. The $grade variable is passed into PHP as a string. Once I wrapped all of my variables in intval() it worked as intended. Stare at the code to long, sometimes you go blind. Haha.
Thank you omnidan for the tip about sanitization. Here is a good guide that I used to apply it to my app:
http://codex.wordpress.org/Validating_Sanitizing_and_Escaping_User_Data

Categories