• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 380
  • Last Modified:

php javascript ajax saving varables

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
sydneyguy
Asked:
sydneyguy
  • 5
  • 4
  • 2
2 Solutions
 
Marco GasiFreelancerCommented:
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
 
boraganeshCommented:
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
 
sydneyguyAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sydneyguyAuthor Commented:
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
 
boraganeshCommented:
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
 
sydneyguyAuthor Commented:
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
 
Marco GasiFreelancerCommented:
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
 
boraganeshCommented:
but as he wants to save the length of url of passing multiple variables sesison->serialization is useful..
0
 
boraganeshCommented:
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
 
boraganeshCommented:
is this sufficient.....
0
 
sydneyguyAuthor Commented:
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now