Jump to content

Howto separate code into different sections to execute at different times


rh-penguin

Recommended Posts

hi,

 

I've got 3 pieces of code in one page(including the forms). Every single piece of code has its own submit button.

What i want to do is once the 1st code/form has bin filled in and the submit button has bin pressed the 2nd piece of code/form appears.

But how can i tell php to go to a specific code because its not in a variable or an array or anything like that. Theres multiple if and else statments and so on.

So my question is: how can i assign the code into different parts/sections?

 

Thanks!

set a hidden field on each form and that will tell u which code to process, and have a unique value in the hidden field like code 1 is gonna have the value 1 ens.

 

eg:

 

$type = $_POST[hidden];

 

then use a case statement or an if/else to determine which code to process

Sorry i dont really understand.

Say i have a code like this

<?php

            $bandwith = $_POST['bandwith'];
            $a = $_POST['a'];

            if( is_numeric( $bandwith ) && is_numeric( $a ) )

{

            if     ( $a <= 3000)
            {
                        $a = 24;
                        $result = $bandwith / $a;
            }
            
		elseif ( $a <= 4000)
            {
                        $a = 32;
                        $result = $bandwith / $a;
            }

		else
            {
		$result = $bandwith / $a;
		$a = floor($a);
		}


}

echo("The result: <u><b>$result</b></u>");

?>

How would i apply the hidden field?

by $type what do you mean?

ok, if I understand correctly, you have 3 forms in one php form.

 

To do this you would have to execute it like this

 

<?php
//check to see which form(s) have been completed, if it has, move on to the next form
if (!isset($_POST['submit1']) && !isset($_POST['submit2']) && !isset($_POST['submit3'])) { //if none of the forms have been submitted, echo the first form
echo 'form1';

} else { // one of the forms has just been submitted, lets see which one it was, and echo the next form
       if (isset($_POST['submit1'])) { // first form complete, echo the second form
           echo 'form2';
       }
       if (isset($_POST['submit2'])) { //second form complete, echo the third
          echo 'form3'
       }
}
?>

 

 

ok, if I understand correctly, you have 3 forms in one php form.

 

To do this you would have to execute it like this

 

<?php
//check to see which form(s) have been completed, if it has, move on to the next form
if (!isset($_POST['submit1']) && !isset($_POST['submit2']) && !isset($_POST['submit3'])) { //if none of the forms have been submitted, echo the first form
echo 'form1';

} else { // one of the forms has just been submitted, lets see which one it was, and echo the next form
       if (isset($_POST['submit1'])) { // first form complete, echo the second form
           echo 'form2';
       }
       if (isset($_POST['submit2'])) { //second form complete, echo the third
          echo 'form3'
       }
}
?>

 

 

But how would i make/assign the form1, form2 and form3?

How do i take the above code that i posted and assign it to say form1?

Wrap each section of code in a function then execute each function as required. A quick example.

 

<?php

  function a() {
    echo "this is a<br />";
  }

  function b() {
    echo "this is b<br />";
  }

  function c() {
    echo "this is c<br />";
  }

  if (isset($_GET['function'])) {
    switch ($_GET['function']) {
      case "a" :
        a();
        break;
      case "b" :
        b();
        break;
      case "c" :
        c();
        break;
    }
  }

  echo "<a href='?function=a'>get a</a><br />";
  echo "<a href='?function=b'>get b</a><br />";
  echo "<a href='?function=c'>get c</a><br />";

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.