SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Two submit-button in one form

    Hello,

    in the code below I use radion buttons to EDIT the entries in a mysql table.

    My question:
    When I have a second submit-Button, DELETE: How can I use the same radio-button for both, EDIT and DELETE?


    <form action="editjoke.php" method="post">
    <table>
    <tr><th>Joke Text</th><th>Options</th></tr>

    <?php
    $jokes = @mysql_query($select . $from . $where);
    if (!$jokes) {
    echo '</table>';
    exit('<p>Error retrieving jokes from database!<br />'.
    'Error: ' . mysql_error() . '</p>');
    }

    while ($joke = mysql_fetch_array($jokes)) {
    //echo "<tr valign='top'>\n";
    $id = $joke['id'];
    $joketext = htmlspecialchars($joke['joketext']);
    echo "<input type='radio' name='id' value='$id' />$joketext<br />\n";
    echo "</tr>\n";
    }
    ?>

    </table>

    <input type="hidden" name="id" value="<?php echo $id; ?>" />
    <input type="submit" value="EDIT" />

  2. #2
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A form can have 2 submits of you want it. Just give them different names, so you can't differentiate them in your php code

    e.g:
    Code:
    <form action="" method="POST">
      <input type="text" name="blah"/><br/>
      <input type="submit" value="submit1" name="submit1"/> <input type="submit" value="submit2" name="submit2"/>
    </form>
    Then in your php code you do this
    PHP Code:
    if(is_set($_POST['submit1'])
    {
      
    // do one thing with the form data
    }
    else if (
    is_set($_POST['submit2'])
    {
      
    // do something different with the form data
    }
    else
    {
      
    // uhm, print form?

    chrome is a wrapper that combines a browser with spyware

  3. #3
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have two submit buttons one with a value of edit and the other with a value of delete, then a simple branching statement can be used. You can give both buttons the same name and the when you check the post variable just check to see if $_PosT['submit'] is equal to edit or delete.

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by -T- View Post
    Then in your php code you do this
    PHP Code:
    if(is_set($_POST['submit1'])
    {
      
    // do one thing with the form data
    }
    else if (
    is_set($_POST['submit2'])
    {
      
    // do something different with the form data
    }
    else
    {
      
    // uhm, print form?

    Is there a function in PHP is_set()? It should be isset() i think.

    Quote Originally Posted by imaginethis View Post
    If you have two submit buttons one with a value of edit and the other with a value of delete, then a simple branching statement can be used. You can give both buttons the same name and the when you check the post variable just check to see if $_PosT['submit'] is equal to edit or delete.
    Yes this is the proper way to achieve the goal as far as i know too.
    HTML Code:
    <input type="submit" name="btnSubmit" id="btnSubmit" value="Save Changes" />
    <input type="submit" name="btnSubmit" id="btnSubmit" value="Delete Post" />
    And in your PHP:
    PHP Code:
    if($_POST['btnSubmit'] == 'Save Changes'){
        
    # do something here to save
    }
    if(
    $_POST['btnSubmit'] == 'Delete Post'){
        
    # do something here to delete

    Hope this makes you more clear.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the answers!

    I have two files: editjoke.php and deletejoke.php.

    The variabe 'id' shoud be send to editjoke.php with the first submit-button and with the second to deletejoke.php.

    How do I have to start the code?
    Where do I write the second 'action'?

    Could it be something like this:
    <form action="editjoke.php" method="post">
    <form action="deletejoke.php" method="post">

  6. #6
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow

    you can also think of following way:


    for submitting form you need to use DOM + javascript
    i.e
    To submit first form:

    document.form[0].submit(); It will post the variable to set action file in <form>.

  7. #7
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh.. No!

    You should handle this in different way. Like you can have a simple button for Delete instead of Submit. And use javascript document.location to redirect to the delete page to delete along with the ID (deleting row ID).
    HTML Code:
    <input type="submit" name="btnSubmit" id="btnSubmit" value="Save Changes" />
    <input type="button" name="btnDelete" id="btnDelete" value="Delete Post" onClick="document.location='deletejoke.php?id=<?php echo $row_id;?>';" />
    But be careful if the JS is disabled in the browser, it wont work.

    Why not follow the same above method having two submit buttons and having only one script file which does both functions Edit and Delete??
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  8. #8
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry,
    but I have to go back at the beginning of my question. The script I posted on the 10th of November no more works. I was so happy but now I am very frustrated. I am a newbie and it is difficult for me but I would like to go on but I need some more help:

    Now, please, at first talk once again about only one 'edit' button and one script file 'editjoke.php' I want to send the variabe 'id'. The code I posted was:


    <form action="editjoke.php" method="post">
    <table>
    <tr><th>Joke Text</th><th>Options</th></tr>

    <?php
    $jokes = @mysql_query($select . $from . $where);
    if (!$jokes) {
    echo '</table>';
    exit('<p>Error retrieving jokes from database!<br />'.
    'Error: ' . mysql_error() . '</p>');
    }

    while ($joke = mysql_fetch_array($jokes)) {
    //echo "<tr valign='top'>\n";
    $id = $joke['id'];
    $joketext = htmlspecialchars($joke['joketext']);
    echo "<input type='radio' name='id' value='$id' />$joketext<br />\n";
    echo "</tr>\n";
    }
    ?>

    </table>

    <input type="hidden" name="id" value="<?php echo $id; ?>" />
    <input type="submit" value="EDIT" />


    What's wrong with it?

    It should work like: echo "<td><a href='editjoke.php?id=$id'>Edit</a>

    Thank you.


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
  •