Welcome to WebmasterWorld Guest from 18.206.194.83

Forum Moderators: open

Message Too Old, No Replies

Increment INT in mySQL database

Help needed to develop a 'like' button.

     
9:11 am on Sep 6, 2011 (gmt 0)

New User

5+ Year Member

joined:Sept 6, 2011
posts: 1
votes: 0


Hi guys. I have no experience with AJAX at all; however, I understand that I need to use this as I want to be able to let a user click on a 'thumbs up' button to add to a like counter without reloading the page.

I would really appreciate some help on this issue. I am developing a business directory and everything so far works how it should apart from this issue. I have done some reading up and have coded the following:

HTML

<script type="text/javascript" src="prototype.js"></script>
<script>
function sendRequest() {
new Ajax.Request("test.php",
{
method: 'post',
postBody: '<?php $Company;?>'+$F('<?php $Company;?>')+'&amp;<?php $Code;?>='+$F('<?php $Code;?>')
onComplete: showResponse
});
}

function showResponse(req){
$('pr_likes').innerHTML= req.responseText;
}
</script>
<body>
<div class="pr_thumb">
<?php $Code = $sr[$ii]['postcode'];
$Company = $sr[$ii]['company_name']; ?>

<form id="test" onsubmit="return false;">
<input type="hidden" name="<?php $Company;?>" id="<?php $Company;?>" >
<input type="hidden" name="<?php $Code;?>" id="<?php $Code;?>" >
<input type="submit" value="submit" onClick="sendRequest()">
</form>
</div>
</body>


PHP


<?php

$companyName=($_REQUEST['$Company']);
$postCode=($_REQUEST['$Code']);

// connect to the database
mysql_connect("*****", "*****") or die(mysql_error());
mysql_select_db("******") or die(mysql_error());

mysql_query("UPDATE businesses SET number_likes = number_likes+1 WHERE company_name = '$companyName' AND postcode = '$postCode'");

?>


I would greatly appreciate any help.

Kind regards,
Mark.
4:19 pm on Sept 6, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


Welcome aboard nortski, the first thing I notice is this:

$('pr_likes').innerHTML= req.responseText;

but then your script doesn't output a response. See below for explanation of some of the other changes you see here.


<?php
$companyName=($_POST['Company']);
$postCode=($_POST['Code']);
// connect to the database
mysql_connect("*****", "*****") or die(mysql_error());
mysql_select_db("******") or die(mysql_error());
$result = mysql_query("UPDATE businesses SET number_likes = number_likes+1 WHERE
company_name = '$companyName' AND postcode = '$postCode'") or die(mysql_error());
//
if ($result) { echo "Thank You!"; }
else { echo "Oops! Could not add like!"; }

?>


That might provide some insight as to what is and is not working.

The second thing is this:
<input type="hidden" name="<?php $Company;?>" id="<?php $Company;?>" >
<input type="hidden" name="<?php $Code;?>" id="<?php $Code;?>" >

and this:

$companyName=($_REQUEST['$Company']);
$postCode=($_REQUEST['$Code']);

In the first case you have the form field name as an actual data value which will be very difficult to control. A better choice might be to do something like this:

<input type="hidden" name="Company" value="<?php $Company;?>" >
<input type="hidden" name="Code" value="<?php $Code;?>" >

Then for the form, you'd receive it like this:

$companyName=($_REQUEST['Company']);
$postCode=($_REQUEST['Code']);

(You can swap $_POST for $_REQUEST since your method in the ajax is post)

Another note, in the second case, you have a **variable** ($Company, etc.) but yet it's quoted with single quotes. Variables won't interpolate with single quotes

Last it's probably better to do this instead:

<form id="test" onsubmit="sendRequest(); return false;">
<input type="hidden" name="Company" value="<?php $Company;?>" >
<input type="hidden" name="Code" value="<?php $Code;?>" >
<input type="submit" value="submit">
</form>

Or, add return false to to the end of sendRequest

function showResponse(req){
$('pr_likes').innerHTML= req.responseText;
return false;
}

and do

<form id="test" onsubmit="return sendRequest();">
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members