php javascript ajax saving varables

Posted on 2010-11-22
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 250 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.


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 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 250 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
[form-control] Retain values after a POST action 21 37
Put javascript to get dropdown value 3 22
Find RGB colors from a screen. 2 19
PHP substring 3 15
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

726 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