www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19

Thread: How do I use a button without a form with PHP?

  1. #16
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    I may be missing some crucial detail here, but is there any reason why you can't use a form tag, using get or post method as desired, and putting any data needed for each form submission in type="hidden" input tags -- no need for JavaScript, etc., just basic HTML generated from the PHP that displays the page?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  2. #17
    Join Date
    Nov 2013
    Posts
    72
    Quote Originally Posted by NogDog View Post
    I may be missing some crucial detail here, but is there any reason why you can't use a form tag, using get or post method as desired, and putting any data needed for each form submission in type="hidden" input tags -- no need for JavaScript, etc., just basic HTML generated from the PHP that displays the page?
    That was my first thought and suggestion of someone here was to try it like that but then I tried the AJAX and JavaScript on the suggestion of someone else. I will give the hidden forms a try. It should work since that button click is specific to that particular form right?

  3. #18
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    Right: only the input elements within that form tag will be submitted.
    PHP Code:
    <!DOCTYPE html>
    <html>
        <head><title>Test</title></head>
        <body>
            <form action="" method="post">
                <p>
                    <input type="submit" name="submit" value="Submit 1" />
                    <input type="hidden" name="foo" value="one" />
                </p>
            </form>
            <form action="" method="post">
                <p>
                    <input type="submit" name="submit" value="Submit 2" />
                    <input type="hidden" name="foo" value="two" />
                </p>
            </form>
    <?php
    if(!empty($_POST['foo'])) {
        echo 
    "<p>You clicked '".$_POST['foo']."'.</p>";
    }
    ?>
        </body>
    </html>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #19
    Join Date
    Nov 2013
    Posts
    72
    That worked great!

    PHP Code:
    if($numRows != && isset($_POST['searchSubmit'])){
                    
                    for(
    $j=0$j $numRows$j++){
                        
                       echo 
    "<div class='searchResults'>
                                          <div class='infoHeading'>
                                            Title:<br />
                                            Author:<br />
                                            ISBN:<br />
                                            Condition:<br />
                                            Price:
                                          </div>
                                          <div class='bookInfo'>
                                            "
    .$bookInfo['title'][$j]."<br />
                                            "
    .$bookInfo['author'][$j]."<br />
                                            "
    .$bookInfo['isbn'][$j]."<br />
                                            "
    .$bookInfo['condition'][$j]."<br />
                                            $"
    .$bookInfo['price'][$j]."<br />
                                            <span id='contactInfo'>"
    .$bookInfo['email'][$j]."</span>    
                                          </div>
                                          <div class=bookImg>
                                            <img class='thumbnail' src=\""
    .$bookInfo['thumbnail'][$j] ."\">
                                          </div>
                                            <form action='' method='post'>
                                                <input type='text' name='title' value='"
    .$bookInfo['title'][$j]."' hidden>
                                                <input type='text' name='author' value='"
    .$bookInfo['author'][$j]."' hidden>    
                                                <input type='text' name='isbn' value='"
    .$bookInfo['isbn'][$j]."' hidden>
                                                <input type='text' name='condition' value='"
    .$bookInfo['condition'][$j]."' hidden>    
                                                <input type='text' name='price' value='"
    .$bookInfo['price'][$j]."' hidden>
                                                <input type='text' name='email' value='"
    .$bookInfo['email'][$j]."' hidden>
                                                <button onclick='successMsg()' id='purButton' type='submit' name='purchase'><img src='images/purchase.png' alt='Click' /></button>
                                            </form>
                                           
                                                            
                                                            
                                        </div><br />"
    ;//end echo div 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles