0

I have come across a situation I am not sure how to hndle. I am new to this, but I do understand the server side realm of php vs user side of the browser.. I just dont know how to accomplish what I want to do..

I have a form on a page where a user can enroll in a school course. They select the course type, location, date, and payment type. On submit it goes to an outsourced shopping cart for payment, which uses PHP vars to populate the item description, price, ect.. along with our store id and other pertinent information. I ALSO need to insert some of the PHP vars into the user database.

I tried having the form action go to another PHP page which process the DB entry then redirects to the cart, but when I get to the cart an error is generated saying the info was not submitted properly.. but the DB entry was successful.

I also tried using an include(dbentry.php) in the form action with the cart link.. this generates a server side error on loading the page.

At one point I successfully had it create a db entry (although it was blank) AND successfully redirect to the cart with all of the vars there, but a blank DB entry does me no good. I am assuming entry happened before the $POST vars were created... I also have changed so much I dont remember how I did it and cannot reproduce that..

My main question is: How can I have a user fill out an HTML form, and when submitted perform the DB entry with the $POST vars while also directly passing the $POST vars to the cart page? Normally I would run the dbentry.php on the next page, but I have no access to scripting on the outsourced cart page...

Chris
  • 133
  • 1
  • 10
  • Here is your answer: Exactly how I would do it - http://stackoverflow.com/questions/5576619/php-redirect-with-post-data –  Feb 29 '12 at 18:06

2 Answers2

0

You can try to use hidden textboxes to hold the values of the form! And this value can be accessed from different php pages

sameer
  • 220
  • 2
  • 6
  • 18
  • 1
    Most of the form is hidden text fields.. There are four user entered fields and 8 hidden fields. – Chris Feb 29 '12 at 18:08
  • sorry.. Most of the form is hidden text fields.. There are four user entered fields and 8 hidden fields. I even took the $_POST vars and cleaned them, then converted to a local var ( example: $name = clean(_$POST['name']); ) then tried converting back after db entry (example: $_POST['name'] = $name; ) but I get the error on the cart page.. The cart only rcognizes values if I post right to it directly. I also tried putting a PHP include into the customizable header code area in my cart with a direct http path to my dbentry.php with no success. I cant even echo a $_POST var from there.. – Chris Feb 29 '12 at 18:14
  • @user1226911 why use 8 when you can use only 4 or even less? – sameer Feb 29 '12 at 18:15
  • bc they are all fields i require to be passed to the cart.. how could i use 4 or less? – Chris Mar 01 '12 at 00:48
0

You will need to pass your variables from page1.php to page2.php to outsourced cart. I would do something like the following:

What the below code is doing:

  1. Send original form data using POST to page2.php

  2. Page2.php will then read the POST data (you can now do what you want with this data, whether it be store it into a database, etc.). A Javascript snippet will then submit the form to your checkout cart page (page3.php) with the necessary POST variables which are being stored as hidden fields within the form.

Page1.php

<form action="page2.php">
<input type="text" name="myfield1"/>
<input type="text" name="myfield2"/>
<input type="text" name="myfield3"/>
<input type="hidden" name="myfield4"/>
<input type="hidden" name="myfield5"/>
<input type="hidden" name="myfield6"/>
<input type="hidden" name="myfield7"/>
<input type="hidden" name="myfield8"/>
</form>

page2.php

        <?php
    if(isset($_POST['myfield1']))
    {
    $myfield1 = $_POST['myfield1'];
    }

// do the above for all of the fields, use the data to query database with.

// Perform database operations here 

?>

<form action='Page3.php' method='post' name='frm'>
<?php
foreach ($_POST as $a => $b) {
echo "<input type='hidden' name='".$a."' value='".$b."'>";
}
?>
</form>

<!-- Script to submit button -->

<script language="JavaScript">
document.frm.submit();
</script>
  • Credit where due: http://www.stackoverflow.com/questions/5576619/php-redirect-with-post-data –  Feb 29 '12 at 18:13
  • THANK YOU SOOO MUCH!!! Ok.. This got me SOOOOO close!! I probably did something wrong. It was only entering the data in the DB and hanging on a white screen in page2.php.. then I realized I had the < script > section placed where you have it rather than in the < head > section.. now the redirect works and the fields are populated on the cart, but nothing entered in the DB.. what am i missing between the first scenario and the second..? – Chris Feb 29 '12 at 18:35
  • thats already basically in there.. without the if-isset.. If that were the issue it would not have put the proper information in the database before moving the < script > up in to the < head > area, would it? that is what kills the db entry.. If I have the < script > info under the form in the < body > the db entry works, but not the form submit (form does not submit for obvious reasons..) – Chris Feb 29 '12 at 18:42
  • I tried adding the If-isset just in case.. same result.. You got me so close!! What am i missing? – Chris Feb 29 '12 at 18:58
  • Ok.. this worked on my test page, but does not work on my actual page.. I cannot figure out why... – Chris Mar 05 '12 at 14:19
  • $b) { echo ""; } ?> – Chris Mar 05 '12 at 14:32
  • send me the URL to your "live" site - I'll take a look at it. –  Mar 05 '12 at 18:37