Jump to content

Using two $_POST Function / Switch () statements, second does not work.


reo_pro

Recommended Posts

Hi all. I’m new to php and am having a problem getting $_POST Function / switch () to work. I am coding a registration form. I ask the user a Yes/No question. Depending on the answer I have an Include () bring in the coding for the next step of the registration process, which also has a Yes/No question. I am using submit buttons for the user to give me the answers.

 

The first $_POST Function / switch () works and will include () the proper coding. The second $_POST Function / switch () does not work.

 

Both are essentially the same coding with just the variable name changed.

 

This is the coding used:

 

<form action="register.php" method="post" name="regForm" id="regForm">

//

HTML in between, input user first name / last name

//

(This works)

 

// Asking if user is a real estate agent

 

<?php

 

$agent = " ";

 

if (!isset($_POST['agent']))

{

//If not isset -> set with dummy value

$_POST['agent'] = " ";

}

 

?>

 

<table align = "center" border="5" cellpadding="3" cellspacing="0">

 

<col width="430" >

 

<tr align = "center">

 

<td>

 

Are you a licensed Florida real estate agent?<br><br>

 

                        // This is the submit buttons

 

<input type="submit" name="agent" value="No">  

 

<input type="submit" name="agent" value="Yes">

 

</td>

 

</tr>

 

</table>

 

// Yes / No switch

 

<?php

 

$agent = $_POST['agent'];

 

switch( $agent ) {

case 'No':

include ("register_3.php");

break;

case 'Yes':

include ("register_2.php");

break;

}

 

?>

 

Once the above Yes/No is executed it brings in:

 

(This does not work)

 

// Asking if user want to sign up for foreclosure list

 

<?php

 

$forlst = " ";

 

if (!isset($_POST['forlst']))

{

//If not isset -> set with dummy value

$_POST['forlst'] = " ";

}

 

?>

<table align = "center" border="5" cellpadding="3" cellspacing="0">

 

<col width="430" >

 

<tr align = "center">

 

<td>

 

Would you like to receive our weekly foreclosure list (by email)<br><br>

 

                        //This is the submit buttons

 

<input type="submit" name="forlst" value="No">  

 

<input type="submit" name="forlst" value="Yes">

 

</td>

 

</tr>

 

</table>

 

// Yes / No switch

 

<?php

 

$forlst = $_POST['forlst'];

 

switch( $forlst ) {

case 'No':

include ("register_4.php");

break;

case 'Yes':

include ("register_4.php");

break;

}

 

?>

 

What am I doing wrong? Do I need to some how clear the POST before I use the function again? If so, how do I do it?

 

 

When I do the same for the code that is not working (echo 'list test'.$forlst;) it echoes the 'list test' and stops. That code (script and buttons) disappears from the browser (like it has been doing) and I end up with only the first "agent" yes/no (script and buttons) showing.

you can try both they both work.

 

example.

<?php

//$forlst="No";

$forlst="Yes";

switch( $forlst ) {
   case 'No':
      echo "We say no";
      break;
   case 'Yes':
      echo "we say yes";
      break;
}

?>

 

 

and if and else need to be used as switch function for large complications.

 

<?php

$forlst="Yes";


if($forlst=="No" ) {


   	echo "We say no";
      
}else{

      echo "we say yes";
   
}

?>

Thank you Alt_F-4 and redarrow for your assistance.

 

I tried both the codes that redarrow gave (the switch and the “if”). Neither works.

 

I believe the test that Alt_F-4 had me run, “echo ‘list test’.$forlst;’, showed that I am not getting a value from “$forlst = $_POST['forlst'];” (or that value is ""). Either way it appears that the input button(s) are not giving a value or the “$forlst = $_POST['forlst'];” is not giving the correct value to $forlst.

 

As soon as I click on either the Yes or No button all the code that was brouht in with the include () dissapears. Nothing is sent to the browser from “echo ‘list test’.$forlst;’ once a button is clicked.

 

What did I do right with the first code that I did wrong with the second code? As I stated before the code is the same except for the variables (agent and forlst). I have been working on this for many hours. This one has me stumped!

 

 

i ran your code without the includes (cause i dont have them) and it worked fine for me.

 

try replacing the includes with an echo statement and see if you get the expected output

 

eg replace

 include ("register_3.php");

 

with

 

 echo 'register_3'; 

 

do that for each of your includes and see what happens. if this works, as it did for me, them i would say that the issues lies in one of you include files.

Thanks for the reply Alt_F4.

 

I replaced “include ("register_3.php");” with “echo 'register_3';”

 

When I ran the code “register_3” printed in the browser (did not run the coding).

 

I tried  “echo 'register_3.php';” Again  “'register_3.php' printed in the browser.

 

I have been playing with this for a while now.

 

I tried replacing the include ( ) in register_3.php with just echo “No” or echo “Yes” just to see if it would run. It did not.

 

I removed the whole switch. It still did not run.

 

I removed the whole php statement back to the </table>. It still acted the same.

 

This tells me something is wrong with the submit buttons.

 

I decided to see what happened if I pulled register_3.php in first and have it include ( ) register_1.php.

 

When I ran it register_3.php worked fine and register_1.php started acting like register_3.php did.

 

Register_1.php is pulled in with “include ( )” and runs fine. Something has to be happening to the submit buttons in the second file I pull it in with the include ( ) (know matter which file I pull in).

 

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.