Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > Linux Forums > Programming/Scripts

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #1  
Old 3rd July 2006, 10:33
alexnz alexnz is offline
Member
 
Join Date: Jun 2006
Posts: 66
Thanks: 3
Thanked 1 Time in 1 Post
Default annoying HTML onclick= "" with PHP

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 by alexnz; 3rd July 2006 at 10:45.
Reply With Quote
Sponsored Links
  #2  
Old 3rd July 2006, 23:56
falko falko is offline
Super Moderator
 
Join Date: Apr 2005
Location: Lneburg, Germany
Posts: 41,701
Thanks: 1,900
Thanked 2,747 Times in 2,578 Posts
Default

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!
__________________
Falko
--
Download the ISPConfig 3 Manual! | Check out the ISPConfig 3 Billing Module!

FB: http://www.facebook.com/howtoforge

nginx-Webhosting: Timme Hosting | Follow me on:
Reply With Quote
  #3  
Old 4th July 2006, 02:51
alexnz alexnz is offline
Member
 
Join Date: Jun 2006
Posts: 66
Thanks: 3
Thanked 1 Time in 1 Post
Default

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 by alexnz; 4th July 2006 at 02:54.
Reply With Quote
  #4  
Old 4th July 2006, 15:47
sjau sjau is offline
Local Meanie
 
Join Date: Apr 2006
Location: Switzerland
Posts: 1,149
Thanks: 4
Thanked 56 Times in 52 Posts
Default

Try something like this:

PHP Code:
<?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.
Reply With Quote
  #5  
Old 5th July 2006, 04:13
alexnz alexnz is offline
Member
 
Join Date: Jun 2006
Posts: 66
Thanks: 3
Thanked 1 Time in 1 Post
Default

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 by alexnz; 5th July 2006 at 04:23.
Reply With Quote
  #6  
Old 5th July 2006, 09:24
sjau sjau is offline
Local Meanie
 
Join Date: Apr 2006
Location: Switzerland
Posts: 1,149
Thanks: 4
Thanked 56 Times in 52 Posts
Default

I would use something like this:

PHP Code:
<?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";
}

?>
Reply With Quote
  #7  
Old 5th July 2006, 15:45
alexnz alexnz is offline
Member
 
Join Date: Jun 2006
Posts: 66
Thanks: 3
Thanked 1 Time in 1 Post
Default

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 by alexnz; 5th July 2006 at 15:48.
Reply With Quote
  #8  
Old 5th July 2006, 15:57
sjau sjau is offline
Local Meanie
 
Join Date: Apr 2006
Location: Switzerland
Posts: 1,149
Thanks: 4
Thanked 56 Times in 52 Posts
Default

This one now works fine...


PHP Code:
<?

$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"


?>
Reply With Quote
  #9  
Old 3rd December 2006, 13:47
smitty350z smitty350z is offline
Junior Member
 
Join Date: Dec 2006
Location: Aberdeen, Scotland
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 Code:
<?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 Code:
    $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 by smitty350z; 3rd December 2006 at 14:07.
Reply With Quote
  #10  
Old 3rd December 2006, 13:53
sjau sjau is offline
Local Meanie
 
Join Date: Apr 2006
Location: Switzerland
Posts: 1,149
Thanks: 4
Thanked 56 Times in 52 Posts
 
Default

Quote:
Originally Posted by smitty350z
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.
Well, it's not $_POST;['task']; but $_POST['task'];
You have in there an additional ";"

Quote:
Originally Posted by smitty350z
[*]For the "do" function's is it possible to run MySQL Queries. For example:
PHP Code:
$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.
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.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
php as cgi thedoc Installation/Configuration 16 28th December 2006 19:14
"unknown filter" growing apache log. wwparrish Installation/Configuration 6 1st September 2006 19:40
2 domains, 1 site wadims Installation/Configuration 13 31st May 2006 01:21
Downgrade php5 to php4.4.2 llizards Installation/Configuration 4 14th March 2006 00:58
all my site go to /var/www/ Absolusteph Installation/Configuration 14 11th March 2006 22:27


All times are GMT +2. The time now is 16:36.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.