Forum Moderators: coopster

Message Too Old, No Replies

form and form processing within the same script

how to check if button was pressed?

         

kumarsena

11:19 pm on Oct 12, 2004 (gmt 0)

10+ Year Member



hey again,

need a way to chcek if a form button was pressed. i have a comment form and when sumbit si pressed some proocessing, just that i dont want to call a another script when button is pressed, prefer to have everything in the same file, so that inlcuding it elsewehere is simple

any sugessitons...u still around minkler..? ;)

k

jatar_k

11:39 pm on Oct 12, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



most scripts check for the existense of the submit button name in the $_POST array

if (isset($_POST['submitit']))

when the button would be

<input type="submit" name="submitit" value="Submit">

mincklerstraat

11:53 pm on Oct 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jatar posted this much more succinctly above I see on editing - 'longer' version here, might still be helpful for extra questions.

lol. You can also get input from your 'submit button' as well as from normal input fields, radio / checkboxes etc. I.i.r.c. for a 'special form' you just have to use <input type="submit" name="thisname" value="Go!">. Then (say if you're using post) $_POST['thisname'] == 'Go!' if this was the button clicked for that particular form. Thing is, "name" is usually left out in the input fields of type "submit", but you can still use this if you need this.

You have another 'non-special' form that's not the 'special form' that either doesn't name it, or doesn't use the value 'Go!' and instead says 'submit this'. Then in the form script:
if($_POST['thisname'] == 'Go!') {
/* do the special stuff for that special form / button */
} else {
/* do the other non-special stuff */
}
You can probably also do the same with just one form, but with two different submit buttons named different things. I know I've tried this, though not in production, just can't remember how it all came out. Basic key is: <input type="submit"> can also have a relevant "name" and "value".

That said, if you have two different submit forms that go to the same script, don't forget that you have <input type="hidden" name="this" value="that">. Your user won't see anything of all of this stuff, but $_POST['this'] == 'that' in your action script. Also, you can add as many of these 'hidden' fields as you want, which is brilliant of course.

Hope this answers your question to some degree. You should really sticky me your url so I can admire your work until now ;)

kumarsena

10:11 am on Oct 13, 2004 (gmt 0)

10+ Year Member



hey,

minckler, my site is still in bit ans pieces but i think i will have it ready in a day or two, sure will stikcy u the add, wouldn't mind some comments...

tnx the button advice guys..

k

kumarsena

10:46 am on Oct 13, 2004 (gmt 0)

10+ Year Member



i did it as follows,

echo '<form method="POST" name="metsign" action="">
<strong>Make a comment</strong><br><br>
Name<br><input type="text" name="name" size="20"><br>
<input type="hidden" name="date" size="20"><br>
Comment<br><textarea name="comment" rows="5" cols="20"></textarea><br />

<input type="submit" name="submit" value="OK">

</form>';

if (isset($_POST[submit]))
{
echo 'cool';

}

?>

but following notice occurs...

any ideas?
k

Notice: Use of undefined constant submit - assumed 'submit' in c:\program files\easyphp1-7\www\test\comment.php on line 34

kumarsena

10:47 am on Oct 13, 2004 (gmt 0)

10+ Year Member



the funny thing is, it is echo-ing 'cool'....

SofterLogic UK

11:28 am on Oct 13, 2004 (gmt 0)

10+ Year Member



if (isset($_POST[submit])) --> if (isset($_POST['submit']))

The reason is that $_POST[submit] is not valid, even though it can make a good guess you mean $_POST['submit'], which it does, and that' why it echos 'cool'

coopster

1:05 pm on Oct 13, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Correct. PHP is thinking you are using an undefined constant rather than a string for an array index. See Array do's and don'ts [php.net] for more information.

kumarsena

8:35 pm on Oct 13, 2004 (gmt 0)

10+ Year Member



tnx alot guys,,,should have noticed that, but hey...making that mistake lead to more info so...

k

kumarsena

8:40 pm on Oct 13, 2004 (gmt 0)

10+ Year Member




Notice: Undefined index: submit in c:\program files\easyphp1-7\www\test\comment.php on line 34

i get this notice now before pressing the button.....

helenp

7:54 am on Oct 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



And how can you do this without an submit button?

I mean if something is choosed in an select name <option> box.

kumarsena

3:44 pm on Oct 14, 2004 (gmt 0)

10+ Year Member



anyone?

helenp

5:07 pm on Oct 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I donīt know whats wrong, but
I just checked your code and I think you should have an action in it, if you want it posted to the same page like this:
action="<?=$_SERVER['PHP_SELF']?>"

Maybe that isnīt the problem,

kumarsena

8:56 pm on Oct 14, 2004 (gmt 0)

10+ Year Member



doesnt work...have to play around with it, maybe ill come up with something...then ill post it..

mincklerstraat

7:41 am on Oct 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure you're using isset or empty on your line 36? also:

$arrayname['array_index'] = 'value';
you'll need quotes around your 'index' as above if the index is a string written out (and not a variable or constant, or int or other number).

kumarsena

9:43 am on Oct 15, 2004 (gmt 0)

10+ Year Member



i got it working...somehow...

<?php

//print existing comments here
//print form

if (isset($_POST['submit']))
{

if((!isset($_POST['name'])) ¦¦ empty($_POST['name']))
{
echo 'no name';

}

else
{
echo 'something';
}

}
?>

the above code works....

kumarsena

9:44 am on Oct 15, 2004 (gmt 0)

10+ Year Member



as for doing it without the submitr button....dont think it will work as you will need a way to trigger the check ,but i may be wrong..

k