www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

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

  1. #1
    Join Date
    Nov 2013
    Posts
    69

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

    I have a for loop looping through data from a mysql query and displaying the results of each. The link is a cropped shot of what each iteration displays on the browser. I wrapped it in img tags but it didn't work for some reason. What I need to do is be able to click the button that is included in each iteration and grab that data from that iteration so that I can have a script send an email to the person that listed the book. I have no clue how to do this with PHP and cannot find anything on the web or any of the books I have.

    http://i25.photobucket.com/albums/c82/wevie/PHPSS.jpg


    below is the code the code that is in the loop.

    PHP Code:
    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]."
                                          </div>
                                          <div class=bookImg>
                                            <img class='thumbnail' src=\""
    .$bookInfo['thumbnail'][$j] ."\">
                                            <input type='submit' name='purchase' value='Purchase' />
                                          </div>
                                          
                                        </div><br />"
    ;
                    }
    //end for loop 

  2. #2
    Join Date
    May 2004
    Location
    chennai, tamil nadu, India
    Posts
    437
    Can you send a specific variable for each click so that using $_GET in php you can trap what was clicked ?
    Chris, Senior Developer,
    www.Chrisranjana.com

  3. #3
    Join Date
    Nov 2013
    Posts
    69
    I'm not sure what you mean.

  4. #4
    Join Date
    Dec 2011
    Posts
    159
    Couldn't you make a "hidden" form?
    HTML Code:
    form.hidden {display:none;}
    That way you can still send POST variables to another page, but the user won't know the difference.

  5. #5
    Join Date
    Nov 2013
    Posts
    69
    Quote Originally Posted by Dragonfire2008 View Post
    Couldn't you make a "hidden" form?
    HTML Code:
    form.hidden {display:none;}
    That way you can still send POST variables to another page, but the user won't know the difference.
    I thought about that but doesn't the button also have to be included in the form, which would also be hidden?

  6. #6
    Join Date
    Dec 2011
    Posts
    159
    Couldn't you put the inputs in a fieldset, with the submit button outside and just hide the fieldset?

  7. #7
    Join Date
    Nov 2013
    Posts
    69
    Quote Originally Posted by Dragonfire2008 View Post
    Couldn't you put the inputs in a fieldset, with the submit button outside and just hide the fieldset?
    I will give this a try tomorrow and let you know how it works. Thanks!

  8. #8
    Join Date
    Sep 2013
    Posts
    221
    One of the approach which i had tried out once is as follows:
    1.You click a link

    2.The js function changes the value of the HIDDEN INPUT field

    3.The js function submit the form

    4.$_POST relates to the hidden input value

    Following is the code for it:

    <input type='hidden' id='hx' name='hx' value=''>
    Then , change your calling function to:

    <a href="javascript: submitform('h1')">h1</a><br>
    Your function:

    <script type="text/javascript">
    function submitform(val)
    {
    $("#hx").val(val);
    document.myform.submit();
    }
    </script>
    in your php:

    echo $_POST['hx'];

    The above code had worked fine for me.
    Hope this helps.
    strad solutionswww.stradsolutions.com

  9. #9
    Join Date
    Nov 2013
    Posts
    33
    is you for loop inside a form?

  10. #10
    Join Date
    Dec 2011
    Posts
    159
    Another way could possibly be to make a php page dedicated to just changing GET variables to POST variables.

    Make a "Processing..." page or something, where you would send GET variables to the processing page, it would then put the variables in a form as values, hide the form, then use a JavaScript timeout to submit the form to the page that wants the POST variables.

    PS. I'm just thinking out loud, but in theory it should work...

  11. #11
    Join Date
    Nov 2013
    Posts
    33
    a faux array for testing
    PHP Code:
    $bookInfo = array();

    $bookInfo['title'][0]     = 'Harry Potter and The Chamber of Secrets';
    $bookInfo['author'][0]    = 'J K Rowling';
    $bookInfo['isbn'][0]       = '9780747560722';
    $bookInfo['condition'][0] = 'New';
    $bookInfo['price'][0]     = '4.50';
    $bookInfo['thumbnail'][0] = 'http://bks0.books.google.co.uk/books?id=5iTebBW-w7QC&printsec=frontcover&img=1&zoom=1&imgtk=AFLRE73kYBfjfL-_8gFnsUaJRLXkihHKmvMdTSDFJz_b9w8strbI969X3kue-mwuEimr5IDYo1bKR9Is1YPNnWcGsHpr5DsK8nnR6djQ0_vtLurAZYowFdPvmIjCzTZ4XOdPInt8qujo';

    $bookInfo['title'][1]     = 'Harry Potter and the Prisoner of Azkaban';
    $bookInfo['author'][1]    = 'J K Rowling';
    $bookInfo['isbn'][1]       = '9781551922461';
    $bookInfo['condition'][1] = 'Used';
    $bookInfo['price'][1]     = '2.50';
    $bookInfo['thumbnail'][1] = 'http://bks2.books.google.co.uk/books?id=Sm5AKLXKxHgC&printsec=frontcover&img=1&zoom=1&imgtk=AFLRE70mylGsTLd1aSUmsXhl53Ff1Bnef3CxhPzX525PBN4HqBXPFIEmS4Va1ko7F5BX55B0Kig9jKGNMdHQrh3XZctKGFwxhJdrbL8j2vwO9v0mjQWRUMrwikmm4q96StRddUxfpMil';

    $bookInfo['title'][2]     = 'Harry Potter and the Sorcerer\'s Stone';
    $bookInfo['author'][2]    = 'J K Rowling';
    $bookInfo['isbn'][2]       = '9781781100271';
    $bookInfo['condition'][2] = 'Super Old';
    $bookInfo['price'][2]     = '900.50';
    $bookInfo['thumbnail'][2] = 'http://bks4.books.google.co.uk/books?id=wrOQLV6xB-wC&printsec=frontcover&img=1&zoom=1&imgtk=AFLRE73fwnZ8mROrZp7GtQbgQf125bRsztAO1qMtmfI1hs9GvRuUTdJga6zaP4ah9n9oXJEWR3Ci97j5tf7qNnohcr87i46vpQzoWj4qtZUz6DrUA9T5PjF6T2lwphoOZbRMl1kUvqz4'
    and the working code
    PHP Code:
    for($j=0$j count($bookInfo['title']); $j++){ 
        echo 
    "<form name=\"{$bookInfo['isbn'][$j]}\"><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].
                   </div> 
                   <div class=bookImg> 
                           <img class='thumbnail' src=\""
    .$bookInfo['thumbnail'][$j] ."\"> 
                           <input type='submit' name=" 
    urlencode($bookInfo['title'][$j]) . " value='Purchase' /> 
                   </div> 
                                           
                 </div><br /></form>"


    hopefully will help you extrapolate something useful.

  12. #12
    Join Date
    Nov 2013
    Posts
    69
    Hey guys. I have something working now. I ended up using something similar to what priyankagound suggested although I also used a but of AJAX as well and by the way, what Dragon Fire suggested did work but I figured there may be a better way so I kept digging.

    If you look at the AJAX, all is really is doing is telling what URL to use, which is what I have the code in that grabs the data and sends an email, how to transfer the data, which is post, and then it passes whatever data I tell it to via post. If anyone wants to see it, I can show the code from the other page as well but all it does is grab the data like such, ( $sTitle = $_POST['title']; )for each variable I passed, and send an email.

    Thanks to all that took a look at this!

    PHP Code:
    "<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>
                                            
                                            <!-- Send Purchase Request to Owner -->
                                          <a class='purchase' href='#' class='post'><img class='purchase' src='images/purchase.png' alt='click' /></a>
                                          <script type='text/javascript'>
                                                jQuery(document).ready(function($){
                                                    $('.post').on('click',function(){
                                                        $.ajax({
                                                            url: 'purchaserequest.php',
                                                            type: 'POST',
                                                            data: { title: '"
    .$bookInfo['title'][$j]."', author: '".$bookInfo['author'][$j]."', isbn: '".$bookInfo['isbn'][$j]."', price: '".$bookInfo['price'][$j]."', email: '".$bookInfo['email'][$j]."' },
                                                            success: function(response){
                                                                  $('.searchResults').hide();
                                                                  $('.successMessage').show();
                                                            },
                                                            error: function(){
                                                                
                                                            }
                                                        });
                                                    });
                                                });
                                            </script> 

  13. #13
    Join Date
    Nov 2013
    Posts
    69
    Well this is not working as well as I thought. It is sending out an email for each time the loop runs and not just the specific button that is clicked. Back to the drawing board I suppose.

  14. #14
    Join Date
    May 2004
    Location
    chennai, tamil nadu, India
    Posts
    437
    You can put

    <script type='text/javascript'>
    jQuery(document).ready(function($){
    $('.post').on('click',function(event){
    $.ajax({
    in the footer just only once and use the event object http://api.jquery.com/event.target/ to find which link was clicked and proceed appropriately
    Chris, Senior Developer,
    www.Chrisranjana.com

  15. #15
    Join Date
    Nov 2013
    Posts
    69
    Quote Originally Posted by chrisranjana View Post
    You can put



    in the footer just only once and use the event object http://api.jquery.com/event.target/ to find which link was clicked and proceed appropriately

    I don't quite follow what you mean. Would you mind explaining this in more detail please?

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