Jump to content

Buttons not seperating the output of 2 seperated functions


MaranV

Recommended Posts

Hi, I`m having a problem with seperating the output of 2 functions, I have specified a unique button to each function (I think) but when I click either one of the buttons the output of the 2 functions is displayed so my question is: how can i get the first button to only display the first output ( the for function without table) and the second button to display the 2nd output (the for function with tables) <?phpif (isset ($_POST["awwyeaa"])) ;for ( $i = 1 ; $i <= 10 ; $i++ ) { print $i." ";}print "<BR /><BR /><BR />";for ( $i ; $i <= 20 ; $i++ ) { print $i." ";}print "<BR /><BR /><BR />";for ( $i ; $i <= 30 ; $i++ ) { print $i." ";}print "<BR /><BR /><BR />";for ( $i ; $i <= 40 ; $i++ ) { print $i." ";}print "<BR /><BR /><BR />";if (isset ($_POST["juppp"])) ;echo "<table border=\"10\" align=\"left\">"; print "<tr>"; for ($i = 1 ; $i <= 10 ; $i++) { print "<td>$i</td>"; } print "</tr>";print "<tr>"; for ($i ; $i <= 20 ; $i++) { print "<td>$i</td>"; } print "</tr>";print "<tr>"; for ($i ; $i <= 30 ; $i++) { print "<td>$i</td>"; } print "</tr>";print "<tr>"; for ($i ; $i <= 40 ; $i++) { print "<td>$i</td>"; } print "</tr>"; ?> _____________________________________________________________________________________and on a sidenote, in a php i wrote earlier i could define the following like this : if (isset ($_POST["testplus"])) { $testplus = $a + $b;echo " Als " . $a . " bij " . $b . " opgeteld wordt is de totale hoeveelheid " . $testplus ;exit; so NO ; after the isset but with a { below it while in the top page code this returns an error and I had to place a ; while removing { an answer to this would be appreciated as to me it seems that php just randomly decided it to require different characters in the same situation :S

Edited by MaranV
Link to comment
Share on other sites

You only need one of the if's to be ran when clicking the button for no table. So you should use a if and an elseif. In your case, when the button for no table is clicked, the other if gets ran because in the POST array, this is considered set: isset ($_POST["juppp"]) as well and when it reaches the if statement for that, it will execute as well. Instead try this:

if(isset($_POST["awwyeaa"])) {	 //code}else if(isset($_POST["juppp"])){    //code}

Edit( I was under the impression of checking a submit button using isset would be like checking an input text field with nothing entered into it. It is still considered 'set' thus why I use empty() to check for such things and so why I thought it was the same for checking if a submit button is clicked(isset). Regardless, I will still use if, else if's for that anyway )

Edited by Don E
Link to comment
Share on other sites

Hmm lets see, you dont have any function, you have POST method that sends (I dont know how you did the form so you can paste here the code of your form) post variables. And it will still run the code after the if (isset ($_POST["juppp"])) ; if its inside the form of the submitted page. Because it will still submit the post and the post variable will be set so the condition is true.Next is you use the single line if condition. It will run only the things on the line itself knowing the condition of the line. example: if (isset ($_POST["juppp"])) echo $_POST["juppp"];it will run the code after the if inside the line itself and run all the codes after the line. So you have to use braces after the if statements.So I revised your code, take a look. I assume that your post inputs are both inside a form. And also checks is if the inputs have value.

if (isset ($_POST["awwyeaa"]) && $_POST["awwyeaa"]!="") {for ( $i = 1 ; $i <= 10 ; $i++ ) {print $i." ";}print "<BR /><BR /><BR />";for ( $i ; $i <= 20 ; $i++ ) {print $i." ";}print "<BR /><BR /><BR />";for ( $i ; $i <= 30 ; $i++ ) {print $i." ";}print "<BR /><BR /><BR />";for ( $i ; $i <= 40 ; $i++ ) {print $i." ";}print "<BR /><BR /><BR />";}if (isset ($_POST["juppp"]) && $_POST["juppp"]!="") {echo "<table border=\"10\" align=\"left\">";print "<tr>";for ($i = 1 ; $i <= 10 ; $i++) {print "<td>$i</td>";}print "</tr>";print "<tr>";for ($i ; $i <= 20 ; $i++) {print "<td>$i</td>";}print "</tr>";print "<tr>";for ($i ; $i <= 30 ; $i++) {print "<td>$i</td>";}print "</tr>";print "<tr>";for ($i ; $i <= 40 ; $i++) {print "<td>$i</td>";}print "</tr>";}

___________________________________________________ On your second question, you dont have closing braces. use this:

if (isset ($_POST["testplus"])){$testplus = $a + $b;echo " Als " . $a . " bij " . $b . " opgeteld wordt is de totale hoeveelheid " . $testplus ;exit;}

Edited by roy0702
Link to comment
Share on other sites

You only need one of the if's to be ran when clicking the button for no table. So you should use a if and an elseif. In your case, when the button for no table is clicked, the other if gets ran because in the POST array, this is considered set: isset ($_POST["juppp"]) as well and when it reaches the if statement for that, it will execute as well. Instead try this:
if(isset($_POST["awwyeaa"])) {	 //code}else if(isset($_POST["juppp"])){    //code}

Hmm lets see, you dont have any function, you have POST method that sends (I dont know how you did the form so you can paste here the code of your form) post variables. And it will still run the code after the if (isset ($_POST["juppp"])) ; if its inside the form of the submitted page. Because it will still submit the post and the post variable will be set so the condition is true.Next is you use the single line if condition. It will run only the things on the line itself knowing the condition of the line. example: if (isset ($_POST["juppp"])) echo $_POST["juppp"];it will run the code after the if inside the line itself and run all the codes after the line. So you have to use braces after the if statements.So I revised your code, take a look. I assume that your post inputs are both inside a form. And also checks is if the inputs have value.
if (isset ($_POST["awwyeaa"]) && $_POST["awwyeaa"]!="") {for ( $i = 1 ; $i <= 10 ; $i++ ) {print $i." ";}print "<BR /><BR /><BR />";for ( $i ; $i <= 20 ; $i++ ) {print $i." ";}print "<BR /><BR /><BR />";for ( $i ; $i <= 30 ; $i++ ) {print $i." ";}print "<BR /><BR /><BR />";for ( $i ; $i <= 40 ; $i++ ) {print $i." ";}print "<BR /><BR /><BR />";}if (isset ($_POST["juppp"]) && $_POST["juppp"]!="") {echo "<table border=\"10\" align=\"left\">";print "<tr>";for ($i = 1 ; $i <= 10 ; $i++) {print "<td>$i</td>";}print "</tr>";print "<tr>";for ($i ; $i <= 20 ; $i++) {print "<td>$i</td>";}print "</tr>";print "<tr>";for ($i ; $i <= 30 ; $i++) {print "<td>$i</td>";}print "</tr>";print "<tr>";for ($i ; $i <= 40 ; $i++) {print "<td>$i</td>";}print "</tr>";}

___________________________________________________ On your second question, you dont have closing braces. use this:

if (isset ($_POST["testplus"])){$testplus = $a + $b;echo " Als " . $a . " bij " . $b . " opgeteld wordt is de totale hoeveelheid " . $testplus ;exit;}

thanks don E, this was not the problem, but thanks for the effort roy, I copied your code, as expected, works like a charm thanks :D however I have a question, in css (html & css being my meager former experience with Webdesign) I seem to always have to place opposite accalades ( { vs } } vs { ) or the code will not work, but in the php you wrote im seeing (or maybe it really is THIS late lol) you using equal accalades following eachother could you explain what this is exactly? infact, im pretty clueless what accalades do besides that they make or break code
Link to comment
Share on other sites

if you meant this

if (isset ($_POST["testplus"]))
if (isset ($_POST["testplus"]))first open paren is for if statement and last one for closing it (black braces) and the red one are forisset() function. the rule of thumb is. as many braces you will open you must have to close it. braces may benested inside but all have to be closed.
  • Like 1
Link to comment
Share on other sites

Yes Birbal is right, use them to separate codes, inside or outside of a function or condition. If you are confused using too much braces you can learn the standard coding or you can use your own, just dont change your format. Or you can use some applications which will help you see the closing and opening braces and parenthesis. I used notepad++ months ago. ^_^

  • Like 1
Link to comment
Share on other sites

ah yes excellent, also something I was curious about a follow up question then: if ((is_numeric ( $a & $b )) && ( $a == $b )) why are there 2 ( characters at the start and 2 ) at the end ? because its 2 conditions being set? and then another unrelated one what are the { and } doing? I understand that if not properly placed your code will fail, but i dont see their function or a logic on where to place them

Link to comment
Share on other sites

why are there 2 ( characters at the start and 2 ) at the end ? because its 2 conditions being set?
braces are used to set precedence of expressions explicitly here. you can see the precedence of operators here. http://www.php.net/m....precedence.phpthis
if ((is_numeric ( $a & $b )) && ( $a == $b ))
could be written as
  if (is_numeric ( $a & $b ) && $a == $b )

as "==" precedence is greater than "&&" $a==$b will be executed first. when you use parens around it you explicitly tell it to execute it. which is in this case is same either way. is_numeric() also will return boolean value so you can ommit the braces around it too.

what are the { and } doing?
they are used to separate code blocks like in control structure,function declaration, class declaration,namespace declration.http://www.php.net/m...tures.intro.php Edited by birbal
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...