annoying HTML onclick= "" with PHP

Discussion in 'Programming/Scripts' started by alexnz, Jul 3, 2006.

  1. alexnz

    alexnz New Member

    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;                                     
              
        
    }
    ?>  
    
    
     
    Last edited: Jul 3, 2006
  2. falko

    falko Super Moderator Howtoforge Staff

    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!
     
  3. alexnz

    alexnz New Member

    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....
     
    Last edited: Jul 4, 2006
  4. sjau

    sjau Local Meanie Moderator

    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.
     
  5. alexnz

    alexnz New Member

    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";
    }
    ?>
    
    
     
    Last edited: Jul 5, 2006
  6. sjau

    sjau Local Meanie Moderator

    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";
    }

    ?>
     
  7. alexnz

    alexnz New Member

    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";
    }
    
    ?>
    
     
    Last edited: Jul 5, 2006
  8. sjau

    sjau Local Meanie Moderator

    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"


    ?>
     
  9. smitty350z

    smitty350z New Member

    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";
    }

    ?>
    1. 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.

    2. 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!:)
     
    Last edited: Dec 3, 2006
  10. sjau

    sjau Local Meanie Moderator

    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.
     
  11. smitty350z

    smitty350z New Member

    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.
     
    Last edited: Dec 3, 2006
  12. sjau

    sjau Local Meanie Moderator

    well, I know the last code that I posted works... so you could post all of your code... otherwise I have no clue...
     

Share This Page