Send sql data from jQuery - php

I'm trying to retrieve database information based on what row a user clicks in a table, then later use that data in another page to display other database information. I don't know the best way to achieve it. Should I use Ajax and $.post() or are there other better/simpler ways?
I can retrieve the data from the table by
echo "<tr data-href=". $row["id"] . " class=\"tableclass\"><td>"
and then in jQuery
$(".tableclass").click(function () {
data = $(this).data("href");
alert(data);
The alert shows that I do get the database information (column ID). Now, I would like to post that information, preferably in a secure manner, to another PHP page where I can retrieve it and use it to get other information from the database.
How do I post it and then how should I retrieve it in the next php page?

Both works in same manner.
1. $.post is just a call with $.ajax(), just with the type set.
2. $.ajax() defaults to a GET
$.post( "/ajax", {"data" : json }) //is nothing but
$.ajax({
type: "POST",
url: "/ajax",
data: {"data": json}
});
If you want the data to be passed securely, use json or don't do thing if the request is not properly authenticated.
Typically, when you make an AJAX request, cookies are also sent: along with the request so you should just be able to use the same authentication method that you use for your regular requests with your AJAX requests.
Nothing much to do that, you need to use $_POST to access all the values in php in file save.php

Related

send data from page to page using ajax without server interaction html

I want to send json object from page 1 to page 2 without server interaction what i have tried so far is like this
from page 1 i have this code
url = '../reports/page2.php?basicinfo=' + encodeURIComponent(JSON.stringify(basicinfo));
window.open(url + "&month=" + month, '_self');
in page two i acces the data by getting the object from the url.
But i had a problem. I exceeded the The requested URL's length exceeds the capacity limit for this server. So i wanted to try if it is possible using ajax what i have tried is
var url = '../reports/page2.php;
var basicinfo = JSON.stringify(basicinfo)
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: {
action: "page2",basicinfo:'basicinfo '
},
complete: function() {
window.location = url;
}
});
I was directed to the correct page my problem is i cant get the data now.
For this purpose You can use localstorage save your json in localstorage variable and fetch it at any moment
localStorage.setItem('favoriteflavor','vanilla'); /*how to save data in localstorage*/
var taste = localStorage.getItem('favoriteflavor');/*how to fetch data from localstorage*/
alert(taste);
localStorage.removeItem('favoriteflavor');/*how to delete data from localstorage*/
For more details click here
So save all your json in localstorage variable before
window.location = url;
in your ajax complete section and fetch localstorage data after redirection (i.e. on this page url )
If you don't want to use jquery variable for data security. Then the only possible way is by using cookies.
Here is the link on stack from where you can see how to create cookies using jquery how to save data in a cookie using jquery
Try to write this to a file and read from the popup.
http://php.net/manual/en/function.file-put-contents.php
http://php.net/manual/en/function.file-get-contents.php
HTTP is stateless protocol. When you redirected to another page - it is new request and data from previous page will be lost.
To share data between pages need to store data (from first ajax request), e.g in session, and restore data from session on second page.

How do I access external html elements (not defined in the view) from a cakePHP controller function?

I have a set of HTML checkboxes inside of a form that are connected to a MySQL database and updated using a CakePHP controller edit function. They are in a modal within my site and not in the view.
When I use the form to make the API call, I'm able to access whether or not the checkboxes are checked with isset($this->request->data['checkbox_name']) however, I'm not able to pass through the auth token and therefore can't authenticate.
When I use an AJAX call in my JavaScript, I'm able to pass through the auth token, but can't view the checkbox data.
Is there a way to access the checkbox information while using the JavaScript method rather than the form submit method? I think I could send the set of boolean values through as a message but I'm not sure how.
$.ajax({
type: "POST",
url: "<?php echo $this->base; ?>/controllers/action",
dataType: 'json',
data: { student_id: '5' },
success: function(data)
{
if(data){
}
}
});
When receiving a passed data from ajax I used $_POST['student_id'] or $_GET['student_id'], depends on the ajax dataType. You may want to inspect the request first with $this->request->is('ajax'); This may help : http://book.cakephp.org/2.0/en/controllers/request-response.html#inspecting-the-request http://book.cakephp.org/2.0/en/controllers/request-response.html#accessing-xml-or-json-data

how to pass android data from ajax through php and then to mysql

need to pass data to android app through ajax by using php.
the data is pulled from mysql database through a select query
please provide me code example how to perform the task
i had search the internet and i find the below code for reference
http://www.grobmeier.de/android-does-not-fire-ajax-reqests-because-they-are-caches-ajax-requests-at-least-on-jquery-mobile-10072011.html#.UGvLbU3A-RI
how to perform the select query to fetch the data array.
please correct me if the example is not proper.
$.ajax({
url: "yoururl.html",
context: document.body,
cache : false,
data: {
username : $('#username').val(),
password : $('#password').val(),
},
success: function ( data ) {
// do something
}
});
What you need is a regular HTTP request (made from AJAX, of course): you send variables to the server by the method you choose. Something like: http://www.example.com/script.php?var1=foo&var2=bar Those variables can be accessed within script.php this way:
<?php
echo $_GET['var1'], PHP_EOL; // shows foo
echo $_GET['var2'], PHP_EOL; // shows bar
?>
If you use POST method instead of GET you must use $_POST[<whatever>] to access you variables.
Once you got the values within PHP you can execute the query you need to persist that data.
There is a ton of this arround Internet, if you make a minimal search you will find exactly what you want.

How to perform GET requests on a JSON file in php?

I have a json file in the form of:
[{"label":"apple","desc":"fruit"},
{"label":"banana","desc":"fruit"},
{"label":"celery","desc":"vegetable"},
{"label":"plum","desc":"fruit"},
{"label":"","desc":"fruit"}]
How would I go about performing GET requests on this data? For example
http://www.mysite.com/data?desc=vegetable would display [{"label":"celery","desc":"vegetable"}] to the browser and
http://www.mysite.com/data?label=apple&desc=fruit would display
[{"label":"apple","desc":"fruit"}] to the browser
Here is a working example that I would like to emulate:
http://ws.geonames.org/searchJSON (returns nothing)
http://ws.geonames.org/searchJSON?featureClass=P&style=full&maxRows=12&name_startsWith=paris (returns matching cities.)
Jquery and php are the tools I'm using. I'm trying to set this up so I can use jQuery UI autocomplete with a remote JSONP datasource.
http://jqueryui.com/demos/autocomplete/#remote-jsonp
You'll probably have better results storing it in a database (e.g. MySQL). Then, you can query based on label and desc columns. Then, access the resulting row(s) with fetchObject and convert the object directly to JSON with json_encode.
You might need a few different versions of the query depending on what GET parameters they pass in.
$.ajax({
type: 'GET',
url: '/getFood/',
dataType: 'json',
success: function(data) {
// do whatever you want with the data here (like show in a div)
},
// replace with whatever you wanna send in the querystring
data: {'desc':'vegetable',},
});
On the server side check the 'desc' parameter in your GET request and send appropriate response.

Update form fields after form submitted

I have no problem with submitting form data through AJAX, and display all that data in a separate div.
But how do I update the form itself with some new info based on server response?
In my case I have:
a form with many inputs, including a group of checkboxes
data from form is collected (with JQuery serialize) and sent to php script through Ajax.
I need to set a specific color for text near a selected checkbox.
I need to set that color not on client side, but based on server side script.
Please explain the correct logical process when need to update a form based on initial form data.
Server side, you can send back a json encoded variable like so.
echo json_encode( array('text_color', 'green') );
Then, on the client side, you can access this variable in the callback function.
$.ajax({
url: 'ajax/test.html',
dataType: 'json',
data: $('yourform').serialize(),
success: function(data) {
var color = data.text_color;
$('yourElement').css('color', color);
}
});
Alternatively you could send back a class and add that class to the element.
If you are using jQuery, you would send the data via AJAX (post/get) and then you would get the response from server script in plain text / json / xml format (your choice). In this case, if you need to return only color code, you can use plain text format. When you get the response, you can manipulate the data.
$.post("test.php", $('#form').serialize(),
function(returned_data_from_server_script) {
$('some dom for color').css('color', returned_data_from_server_script);
});

Categories