I'm currently designing a system working with steps (like an airline booking, where you pass on various steps to buy a ticket) and these steps are being done via AJAX. So, the index.php file is the sole file loaded and the pages' are being loaded via AJAX on the main div.
The system is working as it should be, but I need to ensure the variables' passing on the subsequent loaded pages, based on the data entered before. E. g.:
I have a form on page 1 where the user enter some data, like his name and country. When clicking submit, the button triggers an AJAX request where the result is the content of page 2, being loaded on the div. But I need that the page 2 have the variables filled with the values on page 1. Page 2 have another form with new data and another submit button. When clicked, it requests via AJAX the page 3 which have to contain the variables at pages 1 and 2. And so on.
Which is the smartest way to do that? Using PHP session? Passing via $.get or $.post?
I request an opinion of you.
Thank you very much.
If you are using a library like jQuery - the most easy option is to pass the variables as POST or GET parameters, see: http://api.jquery.com/jQuery.ajax/
For example:
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
You can change the type to be GET or POST depending on your needs and the data key should hold the data you wish to send. (in JSON format)
On how to pass the php data as JSON to the js, see passing php variable
I would go for the
$.post
option of jQuery.
http://api.jquery.com/jQuery.post/
Pass all the variables to your PHP script, and let that PHP script generate the new content of the div (next step) with all the variables (new ones + the ones from the post) filled in. If there are variables that should be transferred but not shown to the visitor, use hidden fields to transfer them.
BTW: to get the contents of all the inputs in your form via jQuery, use
serialize()
http://api.jquery.com/serialize/
They work together like a charm!
Related
I reviewed other post and some helped me but as I am not proficient in Jquery or AJAX, I want to make sure I am doing what I need correctly.
On my main page (base url), I have 2 different outputs based on the view that a user wants to see. I have a toggle button on the screen and a link on either side of the button. Currently when a link is clicked, my PHP is called, sets a session variable with the selected view and returns the specified view.
This changes my URL, i.e. www.mysite.com/view/A and clicking the other toggle link will change the URL to ww.mysite.com/view/B.
My issue is that I am using pagination and I need to specify the correct URL segment for proper page navigation but to normal user navigation, if they leave the main page and then come back, the correct view will be displayed (as the selected view is in the session variable) but the URL could be www.mysite.com, etc. I do not want to write a bunch of logic to determine if the page is the base URL or ww.mysite.com/view/A, etc.
The below function currently changes my toggle button (from left to right) based on which view was selected/clicked. I want to add logic to this function to call my PHP code which would set the session variable without changing the URL, then refresh the page to show the selected view. I know this is simple and I have a similar example which I found online but I want to make sure I am doing this right.
<script>
$('#toggle-control a').click(function(){
$(this).next('ul').slideToggle('500');
$(this).find('i').toggleClass('fa-bars fa-arrow-left');
});
</script>
Ok, so I'm not sure I follow and I apologize about that though you could be a little more clear. From what I grasp you want to accomplish two tasks: (1) you want to figure how to post a session variable to your PHP using jQuery AJAX and (2) you want to make sure your toggle changes position based on the view.
K, so on (1), the following enables you to pass in your script a session variable.
var sessionVar = 1;
var data = {};
data.sessionVariable = sessionVar;
jQuery.ajax({
type: "POST",
url: YOUR PHP FILE HERE
data: data,
success: function(data) {
},
});
In your PHP:
if ($_POST) {
$sessionVar = $_POST['sessionVariable'];
I would recommend using jQuery's .css() to control how the toggle button appears and where using the logic you have in place.
I want to use jquery ajax to submit a form and update it. It works fine with a small amount of data but chokes when I have more data. Is there an inherent limit to this method, or am I on a wild goose chase here?
Here is my save function
function customSave(){
var page = document.getElementById("page").value;
var companyid = document.getElementById("companyid").value;
var formdata = $('#mainform').serialize();
$.ajax({
url: 'transactionsbin-update.php',
method: 'POST',
data: formdata,
success: function() {
reloadList(companyid,page);
},
error: function(){
alert("Save failed");
}
});
}
The page I'm updating is a list of financial transactions that are selected out of the database based on user specified criteria. So depending on the search criteria the user enters, the list of transactions could be long or short. Each transaction has between 7 and 10 inputs, depending on the transaction type, plus there are a number of other inputs in the form that I'm submitting.
Basically what I've noticed is that if I have a few transactions (3 for example) there doesn't seem to be any problem, but if I have a page full of transactions (26 for example) it falls over.
I know this because I'm looking in Chrome's developer tools, network tab and if I click on the update page url it displays a 404 error. It also, by the way, displays a method of GET, even though I clearly specified POST in my ajax call.
The following url fails. It is 11,234 characters long.
https://timesavr.net/dev/transactionsbin-update.php?searchinput=&fromdate=2014-03-01&todate=2014-03-11&activefamiliescbx=on&inactivefamiliescbx=on&chargescbx=on&paymentscbx=on&debitcbx=on&creditcbx=on&chequecbx=on&cashcbx=on&moneyordercbx=on&bankdraftcbx=on&eftcbx=on&paypalcbx=on&emailcbx=on&tdcbx=on&nontdcbx=on&familyid%5B%5D=265&transactiontype%5B%5D=C&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=10&transactionid%5B%5D=133033&transactionreference%5B%5D=0527&listchargeitems%5B%5D=34&listtransactionamount%5B%5D=25&transactionpaymenttype%5B%5D=&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=&transactionpayer%5B%5D=M&familyid%5B%5D=9740731&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=08&transactionid%5B%5D=133031&transactionreference%5B%5D=106&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=-2.00&transactionpaymenttype%5B%5D=MNY&transactiontaxdeductible%5B%5D=N&transactionhistorycomment%5B%5D=Reimbursement+stuff&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=55&transactiontype%5B%5D=C&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=07&transactionid%5B%5D=133032&transactionreference%5B%5D=0526&listchargeitems%5B%5D=34&listtransactionamount%5B%5D=25.00&transactionpaymenttype%5B%5D=&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=&transactionpayer%5B%5D=M&familyid%5B%5D=265&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=133016&transactionreference%5B%5D=21&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=545.00&transactionpaymenttype%5B%5D=BNK&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=Stuff+in+IE&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=265&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=133001&transactionreference%5B%5D=125&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=777.00&transactionpaymenttype%5B%5D=CHQ&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=9732415&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=133002&transactionreference%5B%5D=102&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=888.00&transactionpaymenttype%5B%5D=CHQ&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=265&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=133003&transactionreference%5B%5D=444&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=444.00&transactionpaymenttype%5B%5D=CHQ&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=9732415&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=133004&transactionreference%5B%5D=555&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=555.00&transactionpaymenttype%5B%5D=CHQ&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=&transactionpayerlabel%5B%5D=Father&transactionpayer%5B%5D=F&familyid%5B%5D=265&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=133007&transactionreference%5B%5D=&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=345.00&transactionpaymenttype%5B%5D=BNK&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=9730051&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=133000&transactionreference%5B%5D=&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=650.00&transactionpaymenttype%5B%5D=CHQ&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=Testing&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=265&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=133006&transactionreference%5B%5D=&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=345.00&transactionpaymenttype%5B%5D=CSH&transactiontaxdeductible%5B%5D=N&transactionhistorycomment%5B%5D=&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=265&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=133005&transactionreference%5B%5D=&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=654.00&transactionpaymenttype%5B%5D=DBT&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=9732415&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=132999&transactionreference%5B%5D=&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=550.00&transactionpaymenttype%5B%5D=CHQ&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=Testing&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=9730051&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=133009&transactionreference%5B%5D=&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=345.00&transactionpaymenttype%5B%5D=BNK&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=265&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=132998&transactionreference%5B%5D=&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=450.00&transactionpaymenttype%5B%5D=CHQ&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=Testing&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=9730051&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=132994&transactionreference%5B%5D=&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=88.00&transactionpaymenttype%5B%5D=MNY&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=asdfdddd&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=9732415&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=132996&transactionreference%5B%5D=&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=55.00&transactionpaymenttype%5B%5D=CHQ&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=ddreee&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=9732415&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=132993&transactionreference%5B%5D=&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=855.00&transactionpaymenttype%5B%5D=MNY&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=asdfdddd&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=265&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=132992&transactionreference%5B%5D=&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=350.00&transactionpaymenttype%5B%5D=MNY&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=asdfdddd&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=9732415&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=133008&transactionreference%5B%5D=&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=345.00&transactionpaymenttype%5B%5D=BNK&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=&transactionpayerlabel%5B%5D=Father&transactionpayer%5B%5D=F&familyid%5B%5D=9730051&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=132997&transactionreference%5B%5D=&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=66.00&transactionpaymenttype%5B%5D=CHQ&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=ddreee&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=265&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=132995&transactionreference%5B%5D=&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=22.00&transactionpaymenttype%5B%5D=CHQ&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=ddreee&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=9730051&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=133018&transactionreference%5B%5D=15&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=887.00&transactionpaymenttype%5B%5D=BNK&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=Stuff+in+IE&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=9732415&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=06&transactionid%5B%5D=133017&transactionreference%5B%5D=2&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=877.00&transactionpaymenttype%5B%5D=BNK&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=Stuff+in+IE&transactionpayerlabel%5B%5D=Father&transactionpayer%5B%5D=F&familyid%5B%5D=265&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=04&transactionid%5B%5D=133019&transactionreference%5B%5D=654425&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=12.00&transactionpaymenttype%5B%5D=EFT&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=Testing+FireFox+Now&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=9732415&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=04&transactionid%5B%5D=133020&transactionreference%5B%5D=54&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=45.00&transactionpaymenttype%5B%5D=EFT&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=Testing+FireFox+Now&transactionpayerlabel%5B%5D=Father&transactionpayer%5B%5D=F&familyid%5B%5D=9730051&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=04&transactionid%5B%5D=133021&transactionreference%5B%5D=556&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=12%2C211.00&transactionpaymenttype%5B%5D=EFT&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=Testing+FireFox+Now&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&submittoken=&submitaction=¤ttime24=10%3A46&mm_noteentitytype=&mm_noteentityid=&mm_noteidtodelete=&_=1394556415701
Where as this one submits without a problem. It is 1,566 characters long.
https://timesavr.net/dev/transactionsbin-update.php?searchinput=&fromdate=2014-03-07&todate=2014-03-11&activefamiliescbx=on&inactivefamiliescbx=on&chargescbx=on&paymentscbx=on&debitcbx=on&creditcbx=on&chequecbx=on&cashcbx=on&moneyordercbx=on&bankdraftcbx=on&eftcbx=on&paypalcbx=on&emailcbx=on&tdcbx=on&nontdcbx=on&familyid%5B%5D=265&transactiontype%5B%5D=C&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=10&transactionid%5B%5D=133033&transactionreference%5B%5D=0527&listchargeitems%5B%5D=34&listtransactionamount%5B%5D=25&transactionpaymenttype%5B%5D=&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=&transactionpayer%5B%5D=M&familyid%5B%5D=9740731&transactiontype%5B%5D=P&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=08&transactionid%5B%5D=133031&transactionreference%5B%5D=106&listchargeitems%5B%5D=0&listtransactionamount%5B%5D=-2.00&transactionpaymenttype%5B%5D=MNY&transactiontaxdeductible%5B%5D=N&transactionhistorycomment%5B%5D=Reimbursement+stuff&transactionpayerlabel%5B%5D=Mother&transactionpayer%5B%5D=M&familyid%5B%5D=55&transactiontype%5B%5D=C&transactionyear%5B%5D=2014&transactionmonth%5B%5D=03&transactionday%5B%5D=07&transactionid%5B%5D=133032&transactionreference%5B%5D=0526&listchargeitems%5B%5D=34&listtransactionamount%5B%5D=25.00&transactionpaymenttype%5B%5D=&transactiontaxdeductible%5B%5D=Y&transactionhistorycomment%5B%5D=&transactionpayer%5B%5D=M&submittoken=&submitaction=¤ttime24=10%3A54&mm_noteentitytype=&mm_noteentityid=&mm_noteidtodelete=&_=1394556855187
I know that most browsers have limits on querystring lengths (I'm using Chrome for my testing) but I also read that using method: POST in jquery ajax should behave like a POST not a GET and a real POST does not submit anything on the querystring, although jquery appears to. So I guess the second part of my question is, what is the difference really between using POST and GET in a jquery ajax call?
my process is to call a web service before loading a page that contain a form with some fields ,the fileds organisation,location,building in the form are obtained from the response from the web service call.the fields organistion,building and location are drop downs ,the organisation is selected ,then location is selected after that building is selected,now there is a filed called expiry date ,inorder to get the expiry date i must call a web service by giving the location name as the json input to the web service ,i would like to stick on the same page and get the response for the expiry date,i thought that i will use ajax for that ,i really dont know how to do it can any one help me ? I got some code here can any one redirect me to do some thing done with this code
$.ajax( {
type:'Get',
url:'http://mysite.com/mywebservice',
success:function(data) {
alert(data);
}
})
I assume you want to call a php script to get your results.
Write your php script exactly like you would for generating a normal web page except that instead of outputting (echo) HTML, you output XML or JSON.
I am new to Jquery. I have a doubt:
For example, in a web page as voting, if you click a button, a counter is incremented by +1. Now, how to draw the url of the button on a website? Therefore, if we provide the url to others, and just click on the URL, the counter should increase by 1 on the website.
Best example of this is FaceBook LIKE.
I prefer to use jQuery, PHP and MySQL
It's a little bit difficult to understand what you're trying to ask but here's my take on it.
Scenario
You have a page at http://mywebsite.com/rating which contains 5 items you can rate on.
Solution
There are two events here that point to the same server side code.
What you need to do is assign an identifier to your button/product/whatever you're trying to rate. So you might have something like this <button ratingname="button1">Rate me!</button>
Now you will have a jQuery function that will use AJAX to communicate with your server and store the increment in the database. This jQuery function will be invoked via an event handler for the button and by going to this url: http://mywebsite.com/rating#button1.
Once your page loads you should check the hash for a value and if one is found then invoke the original jQuery function. You may want to additionally check if the value for the hash is a valid rating button value. (Note you could also use a query string).
I would do it using ajax, and a server side program to record votes.
You could design the page with any look-and-feel. Then add the ajax code to talk to your server-side program and maybe show the user the current votes.
?id=642&point=1
It’s a POST, not a GET, so it could only be done from a form or AJAX, not a simple URL.
//Vote update
$.post(
"http://example.com/folder/vote.php", // url
{ id: 642, point : 1 }, // post-data
function(data){ // response
$("#resultBox").addClass("done"); // show msg
}
);
More reading at http://api.jquery.com/jQuery.ajax/
Goal: I want to get the text from the selected element and then create a PHP variable from it.
I know how to do this using jQuery but can't wrap my mind around the idea of how to do it in PHP. Basically lets say I have a table with some data. When the user clicks on the data I would like to retrieve that text and then place it in a PHP variable. I could use .text(); from jQuery to create the variable but I could not pass that variable into PHP for further use.
I am very new to PHP (a front-end developer trying to learn back-end). So any explanation would be helpful also.
thanks!
***OK I now understand how to do it. Thanks for the input. I was not thinking. It makes perfect sense to create the variable using a _post and then returning the data via AJAX. Sorry for wasting your time. Thanks for replying.
There is no onclick event in the backend, at least not that works like in javascript
if you want to pass a variable to the server you can use a form and send the data through a GET or POST
Example:
<form action="index.php" method="get">
<input type="button" value="somevalue" name="action" />
</form>
when the user clicks you will see this in the url
www.example.com/index.php?action=somevalue
now you can retrieve this value in php using the $_GET global variable
$variable = $_GET['action'];
The same can be done using POST, this way the variable is not shown in the url
In order to have PHP gather any client side data you would have to pass that data back to the server.
To assign data from client action to a PHP variable, you would first have to capture that data using a front end solution (like your jQuery one above) then pass that information to your server application for processing using AJAX or form submission.
Your PHP data handler could then parse that information out from the data submission and push that info into a variable.
Your front end would look something like
$.ajax({
type: "POST",
url: 'your_file.php',
data: 'selected='+ captured_data,
});
and on your back end...
<?php
...some php code...
$var_to_hold_selected_thing = $_POST["selected"];
...more php code
?>
Specifics of how to do more than that would depend on your application...
You can do it with ajax:
$.ajax({
type: "POST",
url: 'your_file.php',
data: 'text='+ text,
success: function(data) {
alert(data);
}
});
After the .click() event you could have a dialog box appear, with Is this your text?. Then post it.
You could grab that text with jQuery using $(elem).text() as you've described and then pass the captured value to PHP using an AJAX request with jQuery (see $.ajax). The PHP script you pinged with AJAX (either via GET or POST) can then take the value you passed and push it into a $_SESSION variable.
session_start();
$_SESSION["text_clicked"] = $_GET["text_string"];
exit;
After you do this, any subsequent ajax calls made with jQuery (to any PHP script), can recall this variable, provide that you've properly initiated the session.
Stack 101's answer is the correct execution that you're looking for, however it sounds like you could use a little more background on the context first.
Basically, PHP is a server-side scripting language, which means that it gets executed before the page loads into your browser. Here it can look at a database, connect to other sites, or do a whole host of other stuff. Once it's in the browser, your HTML + CSS + jQuery is in charge of displaying + styling + and manipulating that information respectively.
AJAX is usually used to describe the act of posting data to a PHP script, and then re-loading it back into your page without the page refreshing. In other words, jQuery passes data to a PHP script that runs on the server, and then loads in whatever the PHP script outputted. Sometimes PHP can output an entire site (html tags, css, js, whatever). Other times it can simply access a few variables that it got from the POST array, and output a result. This is much more common with AJAX requests.
A quick search turned up this tutorial which might help (I haven't followed it)
http://www.php4every1.com/tutorials/jquery-ajax-tutorial/
Good luck!
I believe it could be done by sending all the required content through GET or POST, and then make PHP stuff it to a variable, however I'm not exactly sure.
PHP runs on the server. Variables come and go each request. Everytime you refresh a page (or send a request otherwise), a PHP script starts running, spits out a response, and stops again. So setting just a loose variable is not really an option.
The options you got are
- Use a cookie. You can set it from Javascript and it will automatically be sent with the next request. Your PHP script can read this cookie.
- Send the variable by doing an AJAX (asynchronous) request. You can send the value in the url or as posted data.
In either case, you can decide to use the variable only once, or store it in a session. I think the cookie will remain unless you explicitly clear it. So you could keep using that.