Solved

php javascript ajax saving varables

Posted on 2010-11-22
11
361 Views
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
0
Comment
Question by:sydneyguy
  • 5
  • 4
  • 2
11 Comments
 
LVL 30

Accepted Solution

by:
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

<?php
if (isset($_POST['submit'])){
  session_start();
  $_SESSION[text01'] = $_POST['text01'];
  ...
}

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

<?php
session_start();
?>

so to can access $_SESSION variables

Cheers
0
 
LVL 3

Expert Comment

by:boraganesh
ID: 34194163
Hi,

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.

thakx
0
 

Author Comment

by:sydneyguy
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
0
 

Author Comment

by:sydneyguy
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,
            this.form.abc.value,
            this.form.123,
            this.form.xyz.value,
            this.form.job.value,
      ); " /></td>                   
</tr></table>
0
 
LVL 3

Expert Comment

by:boraganesh
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.




 
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:sydneyguy
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.
0
 
LVL 30

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.

Cheers
0
 
LVL 3

Expert Comment

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

Assisted Solution

by:boraganesh
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

session_start();
$id = "yoursessionid4ajax"
$_SESSION[$id]=$serializedArray;

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

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




0
 
LVL 3

Expert Comment

by:boraganesh
ID: 34194314
is this sufficient.....
0
 

Author Closing Comment

by:sydneyguy
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
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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)

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now