Solved

php javascript ajax saving varables

Posted on 2010-11-22
11
362 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 31

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 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.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 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

27 Experts available now in Live!

Get 1:1 Help Now