Need a button
Need a button
(OP)
I need to create a button within a PHP script that, when clicked, takes some information from the script and runs another PHP script to perform a MySQL query and display the result in a popup window. I believe AJAX is the way to do it but I don't know where to start. I need guidance on the AJAX part.
RE: Need a button
Seriously, let me make sure I have this straight: You want to put a button on the Client page that passes data to and executes a script on the server?
RE: Need a button
CODE --> formpage.php
CODE --> ajaxServer.php
RE: Need a button
Prattaratt: Yes. The initial script does a mysql query and presents some information. One bit of the information is excluded (actually not part of the query) and a button is put in its place. On clicking that button, another script runs, validates that the user has access to the information, runs another query, then displays the output in a popup.
RE: Need a button
e.g.
CODE --> onclick
CODE --> jQuery
I prefer the jQuery approach for readability.
you could also add a native event listener.
RE: Need a button
RE: Need a button
CODE
to this:
CODE
for it to work. Is that normal? A good idea?
RE: Need a button
CODE
If you are using mysqli or PDO then something like this would be more normal
CODE --> dbConnect.php
CODE
if you have to put the include in to the functional scope it is because the mysql resource handle is being stored in a variable rather than 'floating' as part of the library. so you have to make the variable ($conn, sometimes) available to the function. using the global keyword does this. Or, as you have done, you can put the include into the function. In which case do not use include_once, but include. Note that this is less good overall from a performance and memory handling perspective.
RE: Need a button
RE: Need a button
CODE
RE: Need a button
CODE
RE: Need a button
so try this
CODE
the value of $test is passed to doList and is available within the function as $id. note that this creates a _copy_ of the variable and is not a pointer to the variable. This can sometimes have material benefits, and equally sometimes is not what you want. In either case if you are coming from a C or C# or C++ etc background, knowing the difference is important.
RE: Need a button
Here is the example trying to use sessions. $user is set by the session creation script. The $user is validated correctly, but it dies at the json_encode line.
CODE
If I define $user specifically in the script as below and omit the session script, it works ok.
CODE
Here is the session script for reference.
CODE
RE: Need a button
CODE
and please also try an alternative to the main script
CODE
RE: Need a button
RE: Need a button
RE: Need a button
on the first script, make _sure_ that the HTML; is absolutely flush to the left hand side and that your code editor does not enforce any silly indenting (several code editors do). if you cannot guarantee this then change the heredoc for single quotes.
CODE
RE: Need a button
RE: Need a button
RE: Need a button
make sure that error_reporting is set to E_ALL and error display turned in, in php. if you don't have access to php.ini (restart the server after each change) you can add this to the beginning of each script
CODE
if by any chance you don't have json_encode as a function on your server then you may need to install that. assuming you are on linux and have an apt based package manager you would run this command
CODE
this is particularly relevant on recent versions of ubuntu.
RE: Need a button
RE: Need a button
RE: Need a button
so there is an error somewhere that is interrupting the flow. this should be displayed in the firebug response, providing that you have properly turned on error display and output.
try navigating directly to the ajaxserver and see what output you get.
RE: Need a button
RE: Need a button
RE: Need a button
CODE
RE: Need a button
RE: Need a button
RE: Need a button
Can you then post the scripts verbatim (delete the passwords).
Another thing to try is to force output with some echo footprinting. It will break the json but help the debug.
RE: Need a button
formpage.php
CODE
ajaxserver.php
CODE
user_header.php
CODE
login.php
CODE
RE: Need a button
the ajaxserver now has a debug function. this will break the json but will provide feedback that will be visible either by browsing direct to the ajaxserver.php file or by the console in firebug.
CODE --> user_header.php
CODE --> ajaxserver.php
CODE --> formpage.php
CODE --> login.php
RE: Need a button
Here are the other scripts.
dbconnect.php
CODE
authenticate.php
CODE
RE: Need a button
now try adding an id. ajaxserver.php?id=someid
thanks for posting those two scripts. if you are comfortable that they are functional (and their includes, likewise) then all i would suggest is to comment out this line in my version of login.php (as it is already set within authenticate())
CODE
RE: Need a button
When I hit formpage with or without commenting that line, I get nothing if the user_header is included. Without it, I get the button but it doesn't work.
RE: Need a button
so as I understand it, the script is working at the moment if you point your browser directly at the ajax server. but not if BOTH these two things are true:
1. you load up the form and rely on ajax for server interaction
2. the form includes user_header
however the ajaxserver direct route works fine when user_header is included.
i'm not certain what this indicates as you have not posted any site where we can test this and examine the raw headers and responses. But i suspect that the most likely culprit is somehow related to the session management and/or timeout.
let's try to fix the button before attempting the ajax functionality.
please use this version of user_header.php to test with.
CODE
and then post back the verbatim output please.
RE: Need a button
RE: Need a button
but please also comment out the header redirect line. the point is to force the page to remain in place and give you feedback.
RE: Need a button
CODE
The second time I hit it, I get
CODE
RE: Need a button
browse to the login page manually and log in.
at the end of login.php before the last endif; insert a line and add this
CODE
once you are logged in, browse back to the form page manually and post back the response.
RE: Need a button
CODE
Here is the ldap.php that I forgot to post earlier.
CODE
RE: Need a button
RE: Need a button
After login, I get:
CODE
When I hit formpage I get the same. When I add the ?id=, I get the same but the button shows up. When I click the button I get nothing.
RE: Need a button
on formpage the buttons will show up when the id is populated as a query string. so that is working as you wanted.
so the issue now is what happens when you click the button.
_something_ must happen. firebug (on firefox) should report that there is an XHR request and give details of what that request looked like and what the results of that request was. it should also provide the response.
at the moment the response will be in html form (not ajax) because the debug switch is set. but there will still be some output if the script is being addressed at all.
if there is NO output, then there will also be NO successful XHR request (firebug will inform you) or NO XHR request at all. In which case the issue is in the javascript.
if there is an XHR request but not one that is successful, then usually this is because the address is not accurate. replace the current url with an absolutely qualified url.
if the issue is in the javascript please post the source code of the html rendered page (i.e not the php but the resulting output from the php page)
RE: Need a button
CODE
Here is the source for the HTML.
CODE
RE: Need a button
then you are not loading jQuery. which means that the jQuery script (jquery-1.10.1.min.js) is not available on your site.
most people reference jQuery on a CDN. replace the src attribute as follows
CODE
or if you want to host locally, save that file to your web root and reference it properly.
RE: Need a button
I get:
CODE
RE: Need a button
Turn off the debug switch in the JavaScript. And try again.
RE: Need a button
RE: Need a button
CODE
$.ajax( { async: false, url: 'ajaxserver.php?
debug=1', type: 'POST', dataType: 'json', data: {id:id}, success: function(data){ if(data.result == 'ok'){ alert(data.data); }else{ alert(data.errorMessage); } }, complete: function(data){ console.log(data); } });RE: Need a button
RE: Need a button
RE: Need a button
RE: Need a button
xhr is clearly marked as a button to the right of javascript in the Net tab.
RE: Need a button
This is on the response tab with XHR selected:
CODE
RE: Need a button
CODE
RE: Need a button
if:
1. A TITLE tag is placed in the user_header.php and
2. Sessions are used to get the user for ajaxserver.php
The alert box doesn't work.
if:
1. A TITLE tag is placed in the user_header.php and
2. $user is defined directly without enabling the session in ajaxserver.php
The alert box works.
RE: Need a button
we seem to have gone back a number of steps.
before, everything was working fine. you were just getting the session data displayed which was breaking the json encoding. stopping the output of session data would make everything work.
you are now saying (and I assume here that you have done NOTHING other than change the code as I posted) that a whole bunch of new symptoms are being exhibited simply by stopping the session data from being output.
If this is the case then either (i) you have introduced errors in following my suggestions; or (ii) you have the most oddly corrupted version of php that I have ever come across.
user_header.php, as posted by you, contains nothing that should be output in an interaction with the server by a user that is already logged in. The only time that there is _ever_ output of any sort is when either the no user is set in the session store (no login) or there is a timeout. In the first instance, output is sent to the user with a meta redirect. In the second instance, the browser is redirected at the http response level back to the login. both of those are scenarios where login must be redone and thus not part of the current process.
there is nothing in the above script (as posted) that can possibly give any output other than as stated. and certainly nothing that contains a <title> tag.
so we will have to wait to hear from you which of (i) and (ii) above you think it is.
Alternatively, provide a link to your site and a login and we can test from here.
RE: Need a button
CODE
I think the problem is that the occurrence of this <TITLE> tag breaks the <head> tag for the script. (you've now seen every script that this touches).
RE: Need a button
the user_header script deals with authentication. I would say it is not a great idea to use it for outputting content.
two solutions:
1. move the authentication layer to another include, and in ajaxserver point to the new include rather than user_header
2. change the ajaxserver script to discard the extraneous output
CODE
the better is the first solution.
RE: Need a button
RE: Need a button
Glad you have it working.