set session variable or local storage in javascript

604 Views Asked by At

I have many php pages which have different forms. I want to submit all the forms to a page called insertproc.php in which I call my stored procedures to insert the data. However I am submitting the form data using jquery.Should I set session variables in jquery script after every form submission ? or can I acheive this with some other functionality. example: if I have 3 pages say page1.php, page2.php, page3.php which has one form each say form1.php, form2.php, form3.php. I have to submit all 3 forms to insertproc.php. In insertproc.php I have check from which form I have submitted. Accordingly I have to run that respective stored procedure. Should I set some session variable in jquery after I submit a form say

$Session['proc_name'] = 'insert_user';

and check in insertproc.php the value of the session variable to call its respective stored procedure. Please guide me how I can achieve this functionality.

1

There are 1 best solutions below

1
On BEST ANSWER

If you submit your form with jQuery you can add another input field to your <form> element which says which form is it, for example, form1.php:

<form ...>
...
<input type="hidden" name="source" value="form1"/>
</form>

form2.php:

<form ...>
...
<input type="hidden" name="source" value="form2"/>
</form>

etc.

Then you can easily check in your PHP code where did the submitted values come from by checking $_POST['source']. No need for sessions. Actually sessions could be source of errors here - what if someone will open form1, not submit, open form2 in another tab, come back to form1 and submit form1? I guess session variable would contain value from form2 instead of form1.