SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    SitePoint Addict smittenbite's Avatar
    Join Date
    Jul 2005
    Location
    Friendswood
    Posts
    397
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    checking which submit button was pressed

    i have a form and there are two submit buttons, one enters data and then goes back to the main page, one enters data and then returns to the same page to let the user continue to enter more data. i tried to follow some code from here http://builder.com.com/5102-6371-5242116.html but i cant get it to work.

    the buttons :

    Code HTML4Strict:
    <p><input type="submit" name="addcont" value="Add More"></p>
    <p><input type="submit" name="addfin" value="Add and Finish "></p>

    the php:

    PHP Code:
     if ($_POST['addcont']) // if (isset($_POST['addcont']))
         
    {
            echo 
    "hello!";

        } 


    what am i doing wrong? my page never echos hello when i click on the add more button. any help appreciated.
    nothing.

  2. #2
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure you have those inputs enclosed by a form tag?

  3. #3
    SitePoint Addict smittenbite's Avatar
    Join Date
    Jul 2005
    Location
    Friendswood
    Posts
    397
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, yes they are in between <form> tags in the html
    nothing.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    <form method="post" action="">
        <input type="hidden" name="run" value="true">
     
        <input type="submit" name="runthis"><br>
        <input type="submit" name="runthat">
    </form>
     
    <?php
     
    if (isset($_POST['run'])) {
     
        if (isset($_POST['runthat'])) {
            print 'Run That';
        } else if (isset($_POST['runthis'])) {
            print 'Run This';
        }
     
        print '<hr><pre>' . print_r($_POST, 1) . '</pre>';
     
    }
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Addict smittenbite's Avatar
    Join Date
    Jul 2005
    Location
    Friendswood
    Posts
    397
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks logic_earth, i put in the extra 'run' field and if statement but it still did not work. one thing i just noticed though is that after i submit , all the variables show up in the url including 'run=true' so i dont understand why my simple 'echo' did not show up still! does anyone know why my variables are showing up in the url though?
    nothing.

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Can you post your full code?

    I added <input type="hidden" name="run" value="true"> because it will always been sent with the form is submitted. IE tends to forget passing the main submit button if it is not clicked.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it's in the URL, then your form is using GET, not POST. Either change your form's method to "post" (preferred) or use $_GET to access your form data.
    PHP questions? RTFM
    MySQL questions? RTFM

  8. #8
    SitePoint Addict smittenbite's Avatar
    Join Date
    Jul 2005
    Location
    Friendswood
    Posts
    397
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes here is the html

    Code HTML4Strict:
    <body>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <fieldset>
    <p>Name : <input type="text" name="setname" size="50"></p>
     
    <input type="hidden" name="run" value="true">
    <p>Term:<textarea rows="3" name="1a" cols="40"></textarea>
       Notes:<textarea rows="3" name="1b" cols="40"></textarea></p>
    <p>Term:<textarea rows="3" name="2a" cols="40"></textarea>
       Notes:<textarea rows="3" name="2b" cols="40"></textarea></p>
    <p><input type="submit" name="addcont" value="Add More"></p>
    <p><input type="submit" name="addfin" value="Add and Finish"></p>
    </fieldset>
    </form>


    this is the php now :

    PHP Code:
    <?php
    if (isset($_POST['run']))
         {
            echo 
    "hello!";
            
            if (isset(
    $_POST['addcont']))
                {
                    echo 
    'add continue submitted!';

                }
            }
    ?>

    edit*
    thank you kromey, i will add the post method to the form now and run it.
    nothing.

  9. #9
    SitePoint Addict smittenbite's Avatar
    Join Date
    Jul 2005
    Location
    Friendswood
    Posts
    397
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks kromey that was the problem.
    nothing.

  10. #10
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Interesting... I would have assumed a browser would default to the (very marginally) more secure POST when no method is explicitly stated. Guess I would have been wrong...
    PHP questions? RTFM
    MySQL questions? RTFM


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •