/    Sign up×
Community /Pin to ProfileBookmark

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.

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

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

[code=php]
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
[/code]

to post a comment
PHP

18 Comments(s)

Copy linkTweet thisAlerts:
@chrisranjanaDec 01.2013 — Can you send a specific variable for each click so that using $_GET in php you can trap what was clicked ?
Copy linkTweet thisAlerts:
@wevieauthorDec 01.2013 — I'm not sure what you mean.
Copy linkTweet thisAlerts:
@Dragonfire2008Dec 01.2013 — Couldn't you make a "hidden" form?
[code=html]form.hidden {display:none;}[/code]
That way you can still send POST variables to another page, but the user won't know the difference.
Copy linkTweet thisAlerts:
@wevieauthorDec 01.2013 — Couldn't you make a "hidden" form?
[code=html]form.hidden {display:none;}[/code]
That way you can still send POST variables to another page, but the user won't know the difference.[/QUOTE]


I thought about that but doesn't the button also have to be included in the form, which would also be hidden?
Copy linkTweet thisAlerts:
@Dragonfire2008Dec 02.2013 — Couldn't you put the inputs in a fieldset, with the submit button outside and just hide the fieldset?
Copy linkTweet thisAlerts:
@wevieauthorDec 02.2013 — Couldn't you put the inputs in a fieldset, with the submit button outside and just hide the fieldset?[/QUOTE]

I will give this a try tomorrow and let you know how it works. Thanks!
Copy linkTweet thisAlerts:
@priyankagoundDec 02.2013 — [U]One of the approach which i had tried out once is as follows:[/U]

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.
Copy linkTweet thisAlerts:
@unasAquilaDec 04.2013 — is you for loop inside a form?
Copy linkTweet thisAlerts:
@Dragonfire2008Dec 04.2013 — 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...
Copy linkTweet thisAlerts:
@unasAquilaDec 04.2013 — a faux array for testing
[code=php]
$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';
[/code]


and the working code
[code=php]
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>";
}
[/code]


hopefully will help you extrapolate something useful.
Copy linkTweet thisAlerts:
@wevieauthorDec 05.2013 — 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!

[code=php]
"<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>
[/code]
Copy linkTweet thisAlerts:
@wevieauthorDec 07.2013 — 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.
Copy linkTweet thisAlerts:
@chrisranjanaDec 07.2013 — You can put

<script type='text/javascript'>

jQuery(document).ready(function($){

$('.post').on('click',function([COLOR="#FF0000"][B]event[/B][/COLOR]){

$.ajax({[/QUOTE]


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
Copy linkTweet thisAlerts:
@wevieauthorDec 07.2013 — 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[/QUOTE]



I don't quite follow what you mean. Would you mind explaining this in more detail please?
Copy linkTweet thisAlerts:
@NogDogDec 07.2013 — 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?
Copy linkTweet thisAlerts:
@wevieauthorDec 07.2013 — 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?[/QUOTE]

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?
Copy linkTweet thisAlerts:
@NogDogDec 08.2013 — Right: only the input elements within that form tag will be submitted.
[code=php]
<!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>
[/code]
Copy linkTweet thisAlerts:
@wevieauthorDec 08.2013 — That worked great!

[code=php]
if($numRows != 0 && 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
[/code]
×

Success!

Help @wevie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...