Jump to content

Two Forms, One $_SESSION


StevenOliver

Recommended Posts

Info: When Customer clicks the "Add More Stuff" button on "/merchandise.php" to add more stuff, modify quantities, etc., the page submits to itself.

Desired: I want a "FINISHED"  button on the bottom of the page. When clicked on, Customer lands on a Thank You, "/FINISHED.php" page.

Problem: The <form action="FINISHED.php"....>  does not have access to the variables in the first form <form action="$_SERVER['PHP_SELF']">.

Question: Using Sessions, javascript, or fancy PHP, is there a way the 2nd form can capture all the data from the first form, so when Customer clicks the FINISHED button, all the variables are sent to the FINISHED.php page?

Like:
function populate_second_form(){
on-Clicking-FINISHED-button {

populate Finished-Form with all data from First-Form;
Then-submit-all-this-data-to-Finished.php page;
}
}

Reasons: There are too many PHP and Javascript variables created in the first form,  many of which I don't understand, it would take me the rest of year 2019 to go line by line and rewrite all of the code.

Requisite whining: Some code is beyond my skill level. For example, when the customer increases a quantity from "1" to "2" javascript will instantly change the displayed dollar-amount from $1.00 to $2.00 without actually "submitting/refreshing" the entire page. Though the displayed amount changes visually, the PHP $variable remains what it was until the page submits to itself. That means I cannot use $_SESSION to capture the newly-displayed amount until the page resubmits to itself.

I've tried everything I can think of.... the closest solution I've found is the new HTML5 "formaction" tag, but I'm looking for a more universal solution (doesn't work in all  browsers).

Thank you!!

 

 

Edited by StevenOliver
Link to comment
Share on other sites

While I have not yet used it, and while W3 (I know, I know) specifically excludes IE and Edge as adopters of it, there is a "form" attribute that you can specify in an input element that allows you to specify multiple form ids to which that input element belongs.  Sounds like what you need, if it actually works.  As for IE and Edge - perhaps W3 is incorrect, so maybe a little research is in order or perhaps a short test form/script...

Link to comment
Share on other sites

ginerjm, hello, thank you for your reply! I think while you were replying, I was editing my post to add that I had already tried the new HTML5 "formaction" tag. Is that what you mean? It is really really nifty, I was like OMG it works.... but then when I read the documentation and did further testing, I found I would lose some of my customers with older browsers. So I'm still looking for a more universal solution (other than spending year 2019 rewriting my code haha)

Link to comment
Share on other sites

Barand, thank you. When you say Redirect, do you mean like this:
header('Location: http://www.example.com/FINISHED.php');

I had it that way for a while, and it did work, but what bothered me was even though I was actually on the desired "FINISHED.php" page,  my browser still showed the URL as the original "merchandise.php." Is there a way to fix this? Or is that pretty much my only choice (other than "hello year 2019 rewriting my code").

Edited by StevenOliver
Link to comment
Share on other sites

1 hour ago, StevenOliver said:

Question: Using Sessions, javascript, or fancy PHP, is there a way the 2nd form can capture all the data from the first form, so when Customer clicks the FINISHED button, all the variables are sent to the FINISHED.php page?

store them in a session array variable as stated in your previous thread.

1 hour ago, StevenOliver said:

Reasons: There are too many PHP and Javascript variables created in the first form,  many of which I don't understand, it would take me the rest of year 2019 to go line by line and rewrite all of the code.

all what code? the submitted form data will be in $_POST. the only thing submitted when someone adds an item to the cart is the item id (sku) and the quantity (which could be an implied 1.) the form processing code needs to add new items to the session cart array variable with the submitted quantity or implied quantity of 1 or add the submitted quantity to the existing quantity if the item is already in the cart. you can have code to handle other operations, such as removing an item from the cart, clearing the entire cart, or updating the entire cart. if there's a lot of code/variables to do this, somethings wrong with the existing code.  the server-side code to perform the 'add' to cart operation is probably about 20 lines of code. anything occurring in the client-side javascript are for display purposes and don't affect what's in the server-side cart. when you process the contents of the server-side cart (item ids and quantities), you calculate any amounts on the server (accepting prices/amounts from the client-side code allows someone to purchase things for the price they want, not the price you have set.)

if you want specific help with any existing code you will need to post it.

 

 

Edited by mac_gyver
Link to comment
Share on other sites

mac_gyver, here is a simplified version of what I am working with.

Customer gets to alter quantities and see the updated total instantly without actually submitting (client-side Javascript). For a $5.00 item, changing the quantity from 1 to 3 and instantly seeing "$15.00" looks nifty. But since that $15.00 value is only displayed client-side, PHP sessions or hidden inputs won't recognize that new $15.00 value until posted.

Up until  now, the way my page works is when Customer clicks the "FINISHED" button, that same merchandise.php page posts to itself and displays "FInished! Thank you for your order!"

But I'm now trying to have the "Finished! Thank you for your order" appear on its own "FINISHED.php" page. And, I can't get this to work because somehow I need to get all the form data that submits to itself, into the 2nd form which submits to the FINISHED.php page.

<script>
function update(){
var quantity = document.getElementById("quantity");
var total = document.getElementById("total");
total.value = (quantity.value * 5);
}
</script></head><body>

<!-- THE ADD MORE STUFF / CHANGE QUANTITY FORM -->
<!-- NOTE: Although Customer sees the inputs populated with numbers, PHP will not
recognize these as variables until form is posted to itself -->

<form id="form_1" method="post" action="merchandise.php">
<input id="quantity" name="quantity" value="<?=$_POST["quantity"]?>" type="text" onKeyUp="update();"> at $5.00 each =
<input id="total" name="total" value="<?=$_POST["total"]?>" type="text"> dollars.
<input type="submit" value="Add More Stuff"> <!-- submits to itself -->
</form>

<!-- NEW FORM: but the hidden inputs (and Sessions, if I had sessions) will be empty  -->
<form action="FINISHED.php" method="post">
<input type="submit" value="FINISHED">
<input type="hidden" name="q" value="<?=$_POST["quantity"]?>">
<input type="hidden" name="t" value="<?=$_POST["total"]?>">
</form>

Thank you.

Edited by StevenOliver
Link to comment
Share on other sites

11 hours ago, Barand said:

Add the finished button to the first form.

When you process the form, first check if the FINISH button was submitted. If it was , redirect to finish.php.

Hmm...interesting. Before when I tried this, my browser kept showing the original URL.
But your answer has been bothering me all day today -- I've been thinking, "How could Barand be wrong?"
So I tried it again just now! Clean code, clean script, clean browser, WORKS PERFECT!
- Session captures the Post variables
- Second submit button triggers header:location
- FINISHED.php page displayes all the variables
- Browser correctly shows the new "FINISHED" URL!

Thank you!! I hereby transfer all 5 of my reputation points to you! ?

 

Link to comment
Share on other sites

Since you are okay with your application relying on JavaScript, there is no need for PHP for this portion.  Customer alters quantities on form_1 and when transitioning to form_2, just use JavaScript to copy the values you want to the hidden inputs.

<script>
    function update(){
        var quantity = document.getElementById("quantity");
        var total = document.getElementById("total");
        total.value = (quantity.value * 5);
        document.getElementById("q").value=quantity;
        document.getElementById("t").value=total;
    }
</script>
<form action="FINISHED.php" method="post">
    <input type="submit" value="FINISHED">
    <input type="hidden" name="q" id="q" value="">
    <input type="hidden" name="t" id="t" value="">
</form> 

You need to get your head around what the client (i.e. your web browser) and server (i.e. PHP) is.  The client always makes the request and the server only responds.  The server cannot directly change anything on the client.  If the client receives the code <?=$_POST["quantity"]?>, it will just be interpreted as this exact literal text.  Only the server knows that it should replace this code with the value of $_POST['quantity'].  Similarly,  the server doesn't have access to the JavaScript variable "quantity" or "total".  The only way data is shared is when the client sends some data to the server or the server responds to the client with some data.  The client sends data to the server either by submitting the page or by submitting via an Ajax request, and for both cases the server responds with whatever it was instructed to do provide.  You "could" make the client submit form_ and have the server respond with form_2 with certain inputs filled out using an echo statement.  Or you can make the client under form_1 update form_2 with the correct values and submit form_2.  And really "kind of" the same thing, you can make the client perform an Ajax request to send (and receive) data without submitting the form.  Make extra, extra, extra sure you understand this.

Link to comment
Share on other sites

5 hours ago, NotionCommotion said:

Since you are okay with your application relying on JavaScript... use JavaScript to copy the values you want to the hidden inputs.

Thank you! After trying to solve this for so long, I realize my real question all along is: "How can I share Javascript variables with PHP?"

Your answer is perfect, hits the nail on the head.
(And yes, the basic concept is kinda hard to wrap my head around... I "get it" for a few seconds, then sort of "accidentally forget"......  like paradoxes, schrodinger's cat, etc.  :-)

I did take your suggestion one step farther.... Because my code generates so many Javascript variables on the fly (up to 100 different quantities, prices, totals, and item numbers), I can't know ahead of time how many preset hidden inputs I would need. Therefore, I wrote Javascript code to create and write entire hidden input tags into a <DIV> using a "document.createElement" function triggered by "onKeyUp" (when Customer inputs quantities). That indeed worked!

However, with all those hidden inputs being generated (sometimes 100's), I'm thinking it might be more safe & secure to use Sessions, with the "Redirect" suggestion made earlier in this thread. Then it would be one less Javascript function, and potentially 100's less hidden inputs :-)

What do you think?

 

 

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.