Jump to content

Recommended Posts

If I was to create something like this I would use CURL to post the data that was posted to the first form.

 

http://php.net/manual/en/book.curl.php

Doing some reading up on CURL, if I understand it right.

The first page would just do a standard SUBMIT on the form & POST to the second page.

Then I would use CURL on the second page so when it loaded, it would open the first page & post the data back into the first page.

Does that sound right? 

What I'm trying to do is this:

 

I've written a calculation program that the user chooses some dropdown menu items & a few other inputs in a form from the page. When the form is submitted it takes them to another page where all the data from the first page is posted into variables on the second page & used for further calculations with other inputs. I'm using session variables on both pages. When they leave the first page the only way I know to get the session variables to the first page is to use a form on the second page that posts back to the first page. If they leave the second page without submitting the form then all the data on the first page will be lost. The reason I'm using separate pages is to keep the pages from getting too cluttered since there is so much data that the user inputs. So what I'm trying to do is figure a way that when the user submits the form on the first page the variables get posted onto that page & then takes the user to the second page where the same variables will be posted too.      

Thanks everyone for the input,

It seems like everything i tried didn't work & I'm sure it was because I couldn't understand how to use Curl correctly or the database thing so I figured out a simple way that works for my situation. Since I'm using session variables all I had to do was use hidden inputs on the second page for the variables from the first page. I got some help here earlier with a session function that I use on both pages. It's not very pretty but here it is:

Page1.php

<?php 
session_start();
function get_value($var)
{
if ($_POST[$var]!="" || (!empty($_SESSION[$var]) && $_POST[$var] === '')) { 
    $_SESSION[$var]=$_POST[$var];
}
if (isset($_SESSION[$var])){ return $_SESSION[$var];}else{ return $_POST[$var];} 
} 
?>
<br>
<form method="POST" action="page2.php">
 Wall type : <select name="wall"  >
  <option>----</option> <option value="green" <?php if(get_value('wall') == "green") { echo 'selected="selected"'; } ?> >green  </option>
 <option value="blue" <?php if(get_value('wall') == "blue") { echo 'selected="selected"'; } ?> >blue  </option>
 </select>
<input type="submit" value="Submit">
 </form>

Page2.php

<?php 
session_start();
function get_value($var)
{
if ($_POST[$var]!="" || (!empty($_SESSION[$var]) && $_POST[$var] === '')) { 
    $_SESSION[$var]=$_POST[$var];
}
if (isset($_SESSION[$var])){ return $_SESSION[$var];}else{ return $_POST[$var];} 
} 
?>
<br>
<form method="POST" action="">
 Door : <select name="door"  >
  <option>----</option> <option value="white" <?php if(get_value('door') == "white") { echo 'selected="selected"'; } ?> >white  </option>
  <option value="red" <?php if(get_value('door') == "red") { echo 'selected="selected"'; } ?> >red  </option>
 </select>
<input type="hidden" value="<?php get_value('wall'); ?>" >
<input type="submit" value="Submit">
 </form>

Thanks again for all the help

 

I also see now that I didn't need to use hidden inputs. I could have just called the functions for each variable i wanted to keep when I opened page2.php.

 

Does that mean the forms work as you expect now?

 

Side note: I would recommend using the <label> tag for your form labels. For more information, visit http://www.cyberscorpion.com/2012-02/making-html-forms-more-accessible-and-improving-usability-with-the-label-tag/

Instead of calling the function each time:

 Door : <select name="door"  >
  <option>----</option> <option value="white" <?php if(get_value('door') == "white") { echo 'selected="selected"'; } ?> >white  </option>
  <option value="red" <?php if(get_value('door') == "red") { echo 'selected="selected"'; } ?> >red  </option>
 </select>

you can call it once and use the return value multiple times which will be more efficient:

 Door : <select name="door"  >
<?php $door_val=get_value('door')?>
  <option>----</option> <option value="white" <?php if($door_value == "white") { echo 'selected="selected"'; } ?> >white  </option>
  <option value="red" <?php if($door_value == "red") { echo 'selected="selected"'; } ?> >red  </option>
 </select>

another strategy is to store the values of the colors in an array and then loop through it to create the select. That way, if you want to add 20 more colors you just add them once and don;t have a ton of code:

 Door : <select name="door"  >
<?php $door_val=get_value('door');
$door_colors=Array ('blue','green','red');
?>
  <option>----</option> 
<?php
foreach ($door_colors as $color){
echo '<option value="'.$color.'" ';
if ($color == $door_val) echo "selected=\"selected\"";
echo ">$color </option>";
}

Does that mean the forms work as you expect now?

 

Side note: I would recommend using the <label> tag for your form labels. For more information, visit http://www.cyberscorpion.com/2012-02/making-html-forms-more-accessible-and-improving-usability-with-the-label-tag/

Yes it does.

I'm assume it's not the correct way to do it but it works perfectly now.

 

Instead of calling the function each time:

 Door : <select name="door"  >
  <option>----</option> <option value="white" <?php if(get_value('door') == "white") { echo 'selected="selected"'; } ?> >white  </option>
  <option value="red" <?php if(get_value('door') == "red") { echo 'selected="selected"'; } ?> >red  </option>
 </select>

you can call it once and use the return value multiple times which will be more efficient:

 Door : <select name="door"  >
<?php $door_val=get_value('door')?>
  <option>----</option> <option value="white" <?php if($door_value == "white") { echo 'selected="selected"'; } ?> >white  </option>
  <option value="red" <?php if($door_value == "red") { echo 'selected="selected"'; } ?> >red  </option>
 </select>

another strategy is to store the values of the colors in an array and then loop through it to create the select. That way, if you want to add 20 more colors you just add them once and don;t have a ton of code:

 Door : <select name="door"  >
<?php $door_val=get_value('door');
$door_colors=Array ('blue','green','red');
?>
  <option>----</option> 
<?php
foreach ($door_colors as $color){
echo '<option value="'.$color.'" ';
if ($color == $door_val) echo "selected=\"selected\"";
echo ">$color </option>";
}

That's an interesting way to do it.

I used the code that I posted to test before I changed the code on the actual site. It's got multiple dropdowns with tons of choices that are all populated from a mysql database.

Thanks again to everyone for all the help. 

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.