for some reason when i run this it comes up with $submitcont = 1 and $sumbitcancel = 1 , even before i click the buttons? i think HTML is just running the php stuff without thinking, how can i set this up so when i click on a button itll change one varible too true? am i going the wrong way with this? how can i get it so when i click on a button its either going too continue with the code form_continue() or else you click cancel and itll go back too my show_form() function; help please?? Code: <?php function process_form () { $submitcancel = FALSE; $submitcont = FALSE; ?> <input type="submit" name="submitcont" value="CONTINUE" onclick="<?php $submitcont = TRUE; ?>"> <input type="submit" name="sumbitcancel" value="CANCEL" onclick="<?php $submitcancel = TRUE; ?>"> <?php echo $submitcont; echo $submitcancel; } ?>
Code: <input type="submit" name="submitcont" value="CONTINUE" onclick="<?php $submitcont = TRUE; ?>"> <input type="submit" name="sumbitcancel" value="CANCEL" onclick="<?php $submitcancel = TRUE; ?>"> You're trying to mix up Javascript and PHP. Javascript is processed on the client, PHP on the server. So this cannot work!
hmmmm so how do create two submit boxes that will allow me too set varibles too true or false? or alternativly, when they click on a submit button itll goto a function, or itll go too another part of the script OR ANYTHING....
Try something like this: PHP: <?php $task = $_POST['task']; switch( $task ) { case 'CONTINUE': doContinue(); break; case 'CANCEL': doCancel(); break; default: doForm(); } function doForm() { ?> <form method="post" action=""> <input type="submit" name="task" value="CONTINUE"> <input type="submit" name="task" value="CANCEL"> </form> <?php } function doContinue() { echo "CONTINUE"; } function doCancel() { echo "CANCEL"; } ?> I haven't checked the code so there is a high chance that something is not working 100% but that should give you an idea.
thanks for the code, works perfectly, now im running into a problem where my POST varibles arnt carrying over from function too function, form too form i have multiple post forms, as youll see from the example below of some code, you can see that when it goes too cont_form() function it doesnt bring over the varibles of $_POST['textbox'] - and it just leaves it as blank; how can i fix this up so i can enter something into the textbox itll save as a varible so that it may be called up at a later stage i think it breaks because i have two forms, as ive asked this above, how can i set a post varible too a global varible so that i can then use that global varible in another function after another form post???? for example i have this code, youll see that when you go into cont_form() it doesnt bring over the $_POST['text'] varible, how can i fix? Code: <?php ; if(isset($_POST['task'])) { process_form(); } elseif (isset($_POST['cont'])) { cont_form(); } else { show_form(); } function show_form () { ?> <FORM method="post" action="<?php $_SERVER[PHP_SELF]?>"> <INPUT type="text" name="textbox" size="20"><BR> <input type="submit" name="task" value="continue"> </form> <?php } function process_form () { echo "<BR>You typed <b>" . $_POST['textbox'] . "</b> in the textbox; press continue too continue or cancel too reset\n"; ?> <FORM method="post" action=""> <input type="submit" name="cont" value="continue"> <input type="submit" name="cancel" value="cancel"> </form> <?php } function cont_form () { echo "<BR>You typed <b>" . $_POST['textbox'] . "</b>\n"; } ?>
I would use something like this: PHP: <?php $task = $_POST['task']; $textbox = $_POST['textbox'] switch( $task ) { case 'continue': doContinue($textbox); break; case 'process': doProcess($textbox); break; case 'cancel': doCancel(); break; default: doForm(); } function doForm() { ?> <form method="post" action=""> <INPUT type="text" name="textbox" size="20"><BR> <input type="submit" name="task" value="continue"> </form> <?php } function doProcess($textbox) { echo "<BR>You typed <b>$textbox</b> in the textbox; press continue too continue or cancel too reset\n"; ?> <FORM method="post" action=""> <input type="submit" name="task" value="continue"> <input type="submit" name="task" value="cancel"> </form> <?php } function doContinue($textbox) { echo "<BR>You typed <b>$textbox</b>\n"; } function doCancel() { echo "CANCEL"; } ?>
thanks for the code agian ive cleaned up some of the mistakes in your orginal code (left out a ; and had the switch statements around the wrong way) - but its still not transferring the varible $textbox too the final function doContinue(); i think its got something too do with the second <form> clears all previous post varibles? and thus $textbox gets cleared when you click the process button? how do we get around this problem? any help much appriacted, and hopefully this will help others who are learning, thanks agian Code: <?php $task = $_POST['task']; $textbox = $_POST['textbox']; switch( $task ) { case 'continue': doProcess($textbox); break; case 'process': doContinue($textbox); break; case 'cancel': doCancel(); break; default: doForm(); } function doForm() { ?> <form method="post" action=""> <INPUT type="text" name="textbox" size="20"><BR> <input type="submit" name="task" value="continue"> </form> <?php } function doProcess($textbox) { echo "<BR>You typed <b>$textbox</b> in the textbox; press continue too continue or cancel too reset\n"; ?> <FORM method="post" action=""> <input type="submit" name="task" value="process"> <input type="submit" name="task" value="cancel"> </form> <?php } function doContinue($textbox) { echo "<BR>You typed <b>$textbox</b>\n"; } function doCancel() { echo "CANCEL"; } ?>
This one now works fine... PHP: <? $task = $_POST['task']; $textbox = $_POST['textbox']; switch( $task ) { case 'continue': doContinue($textbox); break; case 'process': doProcess($textbox); break; case 'cancel': doCancel(); break; default: doForm(); } function doForm() { ?> <form method="post" action=""> <INPUT type="text" name="textbox" size="20"><BR> <input type="submit" name="task" value="continue"> </form> <?php } function doContinue($textbox) { echo "<BR>You typed <b>$textbox</b> in the textbox; press \"continue\" to continue or press \"cancel\" to reset\n"; ?> <FORM method="post" action=""> <input type="hidden" name="textbox" value="<? echo $textbox; ?>"> <input type="submit" name="task" value="process"> <input type="submit" name="task" value="cancel"> </form> <?php } function doProcess($textbox) { echo "<BR>You typed <b>$textbox</b>\n"; } function doCancel() { echo "CANCEL"; } ?>
Hi all! Been using this as refrence for a project I am currently working on and this code seems to be what i need but need to clear up a few queries first: PHP: <?php $task = $_POST['task']; switch( $task ) { case 'CONTINUE': doContinue(); break; case 'CANCEL': doCancel(); break; default: doForm(); } function doForm() { ?> <form method="post" action=""> <input type="submit" name="task" value="CONTINUE"> <input type="submit" name="task" value="CANCEL"> </form> <?php } function doContinue() { echo "CONTINUE"; } function doCancel() { echo "CANCEL"; } ?> When i run this piece of code, as it is or implement it with my code it is giving me an undefined index for "$task = $_POST['task'];". What i am doing wrong? Or is the code wrong because 'task' is not declared until later in the code, within the creation of the form. For the "do" function's is it possible to run MySQL Queries. For example: PHP: $db -> query("DELETE from period where (period.period like '$periodID' && period.course_id like '$courseID');"); This query has been tested and works perfectlly, i just can't tie it in with the onClick on the buttons, due to the PHP/Javascript problem. Any help would be greatly appreciated!
Well, it's not $_POST;['task']; but $_POST['task']; You have in there an additional ";" Well, I see now reasons why the db query should not work. However in that piece of code that I have given there is no db-connector defined.
Sorry sjau, that was just a typo when entering it onto the forum. Therefore i am still having an undefined index problem. As for the MySQL query, could you please explain what you mean. The query works perfectly, and can be run in PHP with that exact code, i just cannot tie it in with a button from a form.
well, I know the last code that I posted works... so you could post all of your code... otherwise I have no clue...