Jump to content

Getting value from form and saving it into a variable, later reading it


Sharkadder

Recommended Posts

Hi, i have a question but not sure if i can do it with javascript or if i'd need php for it.

 

Ok so i have a form which has been created with html, the form itself contains many radio buttons which all have their own value name.  Currently when the form is submitted it goes through a javascript procedure, what i would like to do is store the value from the current selected item in the form so that i can later call upon that variable on a seperate page.  The form method cannot be changed to $_POST and would like to know how it can be done.

 

To explain a bit more clearly about what i am after, i have 6 radio buttons, each radio button has it's own value e.g. value="value1" etc. how do i get a variable in javascript/php to equal that value and then change if the focus goes onto a seperate radio button?

 

e.g. one that has value="value2".  That value will be stored in a variable, then i can later call upon that variable on a seperate page and find out it's value.

 

It might even be better if i had a way to detect what radio button your on when you press the submit button, if that is at all possible.

 

All my radio buttons have the same name "rad" as this is needed as part of my code and i have this code when you click on a radio button:

 

onclick  = "ReadForm (this.form, false);" (this being no relevance to you what it does but basically as you can gather i'm telling the readform to switch to false once radio button is clicked)

 

I just basically want to know how to find out what form element you are on once a submit button is pressed, hold the data in a variable, then be able to call upon it on a later page.

 

Would i need to use php, could i just store the value in a cookie or what, if so do i get the value?

 

thanks

Link to comment
Share on other sites

If you are wanting to store the values of the form without actually submitting the form, it cannot be done with only PHP. PHP can only receive a form submission and process the values. If you are wanting to retrieve the values and store them, you will either need to do so with a cookie or via an AJAX request. With the later, you would scrape the values you need and post them to a "save" script that would set your session variables for you.

Link to comment
Share on other sites

Well i got told i can use php sessions to pass values from one page to another, i already knew this but for what i needed it wouldn't work.  I could create a cookie but not sure if it would work, here is what i tried with the php sessions for my index.php page:

 

<form action="javascript: void 0;" method="post" onsubmit="<?php session_start(); $_SESSION['value']=$_POST['rad'] ?>;SetDesc ('');

SetWt ();

SetCode ('a10001');

if (ReadForm (this, true))

                    CallView ();

ClearAll ();

return false;">

 

As you can see i am telling the php session to equal the name rad from the $_POST, however when i go onto my other php page it doesn't seem to work and not sure why, on the next page which it is linked to i have this statement:

 

<?php echo $_GET['value']; ?>

 

 

Nothing shows when i put ?value after the url, so how would i go about getting the forms value in a cookie and then retreving the value from that cookie on another page using javascript or php as both handle cookies.

 

thanks

Link to comment
Share on other sites

Like I said, the PHP gets processed before the markup hits your screen, so you can only process the data with PHP when the form is submitted to the receiving page. If you are wanting to assign the data to a PHP session variable onsubmit, you need to have a JavaScript call that retrieves the data and submits it via an AJAX request to the receiving script that will then assign it to your session.

 

You cannot access PHP after your page has rendered.

Link to comment
Share on other sites

I have never used ajax before, this means i am going to have to look into even more languages which i don't really want to do right now.

 

I understand your trying to suggest things, but i'd really like an example on what i should be doing, once i know how i can get the form value into a cookie or saved into a variable which i can then use on another page, i can do the rest.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.