Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


php javascript ajax saving varables

Posted on 2010-11-22
Medium Priority
Last Modified: 2012-05-10
have a web page that i am running it is a php backed site with a mysql data base, am runing ajax to update a data base table i have to harvest 10 text boxs with data in them pass them to the ajax call function and up date the field. I can do that now with one long term with the 10 fileds all collected in $msg and i could pull it apart from there and place them in the aprop fields, but was wondering were would be the better place to store this information so that when i  click on the save button it could punch the individual fields into a cookie or varable or something and then access it from the db save area and up date the individual fields from there with the saved data
any ideas would be appreciated  looking for best practice direction
Question by:sydneyguy
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
LVL 31

Accepted Solution

Marco Gasi earned 1000 total points
ID: 34194160
You could not use Ajax, use save button to post form to $_SERVER['PHP_SELF'] and store data in several session variables

if (isset($_POST['submit'])){
  $_SESSION[text01'] = $_POST['text01'];

Then place in any page of your program at the very top


so to can access $_SESSION variables


Expert Comment

ID: 34194163

Session is the best place to store all these 10 values and access it in another page..but remeber to clear session once you utilize it for updating data using ajax.


Author Comment

ID: 34194173
really need to use ajax so i do not have to post off and can update my DB with the saved data.
it really needs to use ajax. at present i have all the var passed in one sting and if needs i will just disect the sting then place them into the db that way individually, then update the display page, but would still like to find a cleaner way of doing this. is posting it the best way that you see of doing it
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 34194196
but to access the session i would still have to post it off or how would i do this via ajax the below code sits on my javascript page, thsi will assemble the code that i want and then ajax it through to the other side and then pull it apart.
its at this spot i would need to save it to the session or what ever to hold it till i get to the server. or may be this is the simplest way of doing it and keep going foward

<td><input name="rv_counter" type="text" class="textinput" size="2" maxlength="3" value="" readonly />                  
<input type="button" class="formbutton" name="Submit" value="{lang mkey='sb_send'}" onclick="javascript: sendSBMsglines(this.form.trip.value,
      ); " /></td>                   

Expert Comment

ID: 34194216
create one array of information having key-value pair of all the values to be required for ajax function.
serialize this arry.
store this ary into one session..assign some name(id) to session
pass thsi session id to ajax function
in ajax function unserialize the session value abnd get the requried valiables.


Author Comment

ID: 34194252
brilliant i believe we have a winner. this  is the answer i was after, elegant and will do the job required
this is new to me. do we have any examples before i run off and troll the net please.am happy to look just thought you might have an example or a good article i could go through

1) serialize this arry.  
2) store this ary into one session..assign some name(id) to session
3) pass thsi session id to ajax function
4) in ajax function unserialize the session value abnd get the requried valiables.
LVL 31

Expert Comment

by:Marco Gasi
ID: 34194260
If you need to use Ajax, then you can proceed with your actual code: it works fine enough, I think. Any way, to save session variables you have to submit them with a form: if realy you want user session variables oyu should post the code for sendSBMsglines(). But I repeat, If it's not to avoid ajax call, I really don't see the need to change your code.


Expert Comment

ID: 34194265
but as he wants to save the length of url of passing multiple variables sesison->serialization is useful..

Assisted Solution

boraganesh earned 1000 total points
ID: 34194292
create array.
$arrayToSerialize = array("key1"=>"value1","key2"=>"value2","key3"=>"value3");

serialize the array
$serializedArray = serialize($arrayToSerialize);

store it into session

$id = "yoursessionid4ajax"

call ajax with url
ajax func url?id=$id

$id = $GET['id']
$serializedArray  = $_SESSION[$id];
$unserializedArray = unserialize(stripslashes($serializedArray));  


Expert Comment

ID: 34194314
is this sufficient.....

Author Closing Comment

ID: 34194592
yes thanks i now have enough information to go foward. so i will look at which method best suits my needs, as marqus sugested, if the code runs and it does i may just run with this code, and then revist the sessions as i get to furthur down the track.
point is as i have it now it runs and is quick and offers me all the rsults that i need.
as for the sessions had not thought of this and may use it later. as i get down to the end of the project.
but thanks for you help always good to get confirmation from the resident gurus in this area

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question