Hello everyone!
I’m going crazy with a " Undefined index" error in my PHP
I have table with an id column and button ‘EDIT’ in a html file ( " results.html" )
When the button is clicked, I retrieve the id value and I send it ,with jquery ‘$.post’ ,to a php ( "prova.php " ) and at the same time I open the page prova.php in a popup.
$('.edit').click(function () {
var button = $(this),
tr = button.closest('tr');
// find the ID stored in the .groupId cell
var id = tr.find('td.id').text();
console.log('clicked button with id', id);
var data = { ID: id };
$.post('prova.php', data, function (res) {
console.log('received response', res);
});
$('.edit').click(function () {
var newwindow = window.open("prova.php",'ordini',
'height=800,width=1024');
if (window.focus) {
newwindow.focus();
}
return false;
});
here PHP:
<?php
echo 'Hello ' . ($_POST['ID']) . '!';
?>
I know the value is passed , I can see it in the console . Moreover I used this code in another situation, without displaying the page, to query the database and copy some files.
The web console is ok:
" clicked button with id 1
received response Hello 1! "
but still the popup says:
"
Notice: Undefined index: ID in C:\Program Files (x86)\EasyPHP-Devserver-17\eds-www\prova.php on line 3
Hello ! "
I’m sure it’s simple to fix, but after 10 hours I’m not able to figure it out.
I tried different ways. I tried also a simple href=“prova.php” directly in the HTML, but nothing changes.Same error.
Why do you think the error comes from there?
I don’t know much about JavaScript / JQuery - how does the code know which of the two “click()” functions to run first? Is it just the physical order in the source code? Or does the still-open curly-brace from the first click() function mean that it just runs it all in one sequence? I wasn’t sure if that was a typo or not.
I think what @dormilich is referring to is that in that second bit of code, you open a new window which in turn opens “prova.php”, but you don’t pass any POST variables into the PHP code. So the new window will show a PHP error because it’s running the code with nothing passed in. Your console log shows the first time that prova.php is called, and the data passed to it. In the second function, it throws an error.
The curly-brace is closed in the code, it’ s a typo, Those are two separeted function. I see what you and @dormilich say , but I’m not sure. [quote=“droopsnoot, post:4, topic:283177”]
Your console log shows the first time that prova.php is called, and the data passed to it. In the second function, it throws an error.
[/quote]
Am i wrong if I say that the second console log is shown only in case of success?[quote=“diegosaggiorato, post:1, topic:283177”]
var id = tr.find(‘td.id’).text();
console.log(‘clicked button with id’, id);
var data = { ID: id };
$.post('prova.php', data, function (res) {
console.log('received response', res);
[/quote]
By the way , is there an alternative to open prova.php passing the data?
I post in php because I thought the problem was there.
I wouldn’t like to say, that’s more of a jquery question. But “res” in this case would be whatever the PHP echoes out, which might include that, or any other, error message. Success only means that it was able to call the page and get a response, not that the server code did what you wanted it to do.
But it’s this line
var newwindow = window.open("prova.php",'ordini', etc etc.
that causes the error to display, because it calls prova.php without including the post information, and your PHP code does not cope with the possibility of the id being missing, references it, and gets an error message in return.
One quick and dirty way to code it would be to call
var newwindow = window.open("prova.php?id=1",'ordini',
and modify your PHP code so that, if $_POST['id'] is not found, it looks for $_GET['id'] instead.
I still don’t understand having two click() handlers for the same page element. Can’t you put the code into one?
I don’t like the GET method. As you suggested,I changed the code in order to have the code on the same handler.
Now I open the popup cart.html and import via Ajax from prova.php.
$('.edit').click(function () {
var button = $(this),
tr = button.closest('tr');
// find the ID stored in the .groupId cell
var id = tr.find('td.id').text();
console.log('clicked button with id', id);
// your PHP script expects GROUP_ID so we need to pass it one
var data = { ID: id };
$.post("prova.php", data, function (res) {
console.log('received response', res);
});
var newwindow = window.open("cart.html",'ordini',
'height=800,width=1024');
if (window.focus) {
newwindow.focus();
}
return false;
}
);
In prova.php now I coded a query, filtering the results by $ID, and a looping echo of the results.
In the console now I’m able to see the CORRECT result of the echo , but still in the popup it keeps saying “Notice: Undefined index: ID”.
It looks like you are trying to send a value to a php script then have the php script remember that value exists. If that is the case you will need to store the posted value in a persistent storage space like the session so that the pop-up can discover it again. Also if you need to make two requests – one to POST the value and the other to GET the pop-up contents the pop-up will need to be created after the first request comes back successfully. JavaScript is an asyncronous language by nature. What that means is that in the last snippet you posted the AJAX request is happening at the exact same time as the window opening. You will want to move the window open into the callback function of the AJAX request. You still need to make sure that the value being posted is stored in a session or something so that the script that generate the pop-up content can fetch that value after it has been saved to storage that persists through each request the user makes. Just remember that AJAX requests will happen at the same time when they follow one another. The only way to make one a prerequisite of another will be to have the second in the callback of the first. window.open isn’t an “AJAX” request but either way its javaScript that is going to execute essentially at the same time as the AJAX request in the code you posted. I’m not really sure what you are doing but it seems odd to me to make two requests. It seems like it might be better to make a single AJAX request and have the response be the contents of the pop-up.
What is in cart.html? This is the page that gets opened when you call window.open, and if it relies on values being passed in, then you must pass them in. If you don’t, you will get your “undefined index” or any other error messages unless you check variables exist before using them.
As @zookeeper said above, if you’re hoping that the second time you call the page it will remember the variables from the last time, remember it’s an entirely new instance and if you want it to do that, you need to code it.
Thank you everyone! Thank you @ZooKeeper , you helped me to understand the conceptual error.
I made an Ajax call to php,and on the success I opened a popup , passing the html response to the child window. It works like charm!!
Here the code, in case it would be usefull for others.