nortksi Posted September 5, 2011 Share Posted September 5, 2011 Hi guys, I am hoping someone can help me with this. I am pretty new to PHP and MySQL but I am learning all the time; however I have become stuck. I am developing a business directory and need to be able to let the user click a 'like' button which will then add to the total number of 'likes' for that business. At the moment I am able to retrieve all businesses and output them to the screen, the current number of likes is also displayed. The brief code for displaying th number of likes is: <div class="pr_likes"><?php echo $sr[$ii]['number_likes'];?> likes</div> <a href="#"><div class="pr_thumb"></div></a> Underneath the pr_likes class is a div that displays a roll over 'thumbs up' image. I realise that instead of this being a hyperlink it will most probably be a form? If possible I want it to add to the total number of 'likes' without leaving the current page as the retrieved businesses are returned randomly and I do not want to scramble the results. All help will be greatly appreciated. Kind regards, Mark. Quote Link to comment Share on other sites More sharing options...
voip03 Posted September 5, 2011 Share Posted September 5, 2011 Without leaving the current page, you want to show total number, once user click likes. You need ajax Quote Link to comment Share on other sites More sharing options...
nortksi Posted September 5, 2011 Author Share Posted September 5, 2011 Hi voip03, thanks for your reply. Ah right, I'm afraid my knowledge of Ajax is even less than my knowledge of PHP. Just a quick note, I also need to add to the number of likes once the button has been pressed. Regards, Mark. Quote Link to comment Share on other sites More sharing options...
voip03 Posted September 5, 2011 Share Posted September 5, 2011 This link will help you http://www.sitepoint.com/ajax-jquery/ http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/ http://www.php-learn-it.com/tutorials/starting_with_php_and_ajax.html read this post http://www.phpfreaks.com/forums/index.php?topic=343063.msg1618454#msg1618454 Quote Link to comment Share on other sites More sharing options...
nortksi Posted September 5, 2011 Author Share Posted September 5, 2011 Thanks! I shall give it a read. Quote Link to comment Share on other sites More sharing options...
nortksi Posted September 5, 2011 Author Share Posted September 5, 2011 Ok, having reading up on the suggested material I have gave it a go. However; I am unable to get this working. This is what I have so far: HTML FILE: <script type="text/javascript" src="prototype.js"></script> <script> function sendRequest() { new Ajax.Request("test.php", { method: 'post', postBody: '<?php $Company;?>'+$F('<?php $Company;?>')+'&<?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> TEST.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'"); ?> It is no doubt incorrect on so many levels. Any helpful pointers will be greatly appreciated Regards, Mark. Quote Link to comment Share on other sites More sharing options...
ZulfadlyAshBurn Posted September 7, 2011 Share Posted September 7, 2011 you will need the echo out your variables. and there is php in both scripts thus it must used .php extensions unless you parsed it in your server config. index.php <?php $Code = $sr[$ii]['postcode']; $Company = $sr[$ii]['company_name']; ?> <script type="text/javascript" src="prototype.js"></script> <script> function sendRequest() { new Ajax.Request("test.php", { method: 'post', postBody: '<?php echo $Company;?>'+$F('<?php echo $Company;?>')+'&<?php echo $Code;?>='+$F('<?php echo $Code;?>') onComplete: showResponse }); } function showResponse(req){ $('pr_likes').innerHTML= req.responseText; } </script> <body> <div class="pr_thumb"> <form id="test" onsubmit="return false;"> <input type="hidden" name="<?php echo $Company;?>" id="<?php echo $Company;?>" > <input type="hidden" name="<?php echo $Code;?>" id="<?php echo $Code;?>" > <input type="submit" value="submit" onClick="sendRequest()"> </form> </div> </body> test.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'"); ?> Quote Link to comment Share on other sites More sharing options...
nortksi Posted September 8, 2011 Author Share Posted September 8, 2011 Hi ZulfadlyAshBurn, thanks so much for taking the time help me! I have implemented the changes you suggested. Unfortunately; it still does not work I have checked the variable and field names which all appear correct. Any other thoughts? Kind regards, Mark. Quote Link to comment Share on other sites More sharing options...
ZulfadlyAshBurn Posted September 8, 2011 Share Posted September 8, 2011 is there any error? enable error reporting. paste it at the starting of the page. <?php error_reporting(E_ALL); ini_set('display_errors', '1'); ?> Quote Link to comment Share on other sites More sharing options...
nortksi Posted September 9, 2011 Author Share Posted September 9, 2011 is there any error? enable error reporting. Hi. Yes an error is reported: Notice: Undefined variable: ii in /content/Hosting/p/l/web/eventresults.php on line 25 Notice: Undefined variable: sr in /content/Hosting/p/lweb/eventresults.php on line 25 Notice: Undefined variable: ii in /content/Hosting/p/l/web/eventresults.php on line 26 Notice: Undefined variable: sr in /content/Hosting/p/l/web/eventresults.php on line 26 These lines are: $Code = $sr[$ii]['postcode']; $Company = $sr[$ii]['company_name']; I hope this can shed some light Regards, Mark. Quote Link to comment Share on other sites More sharing options...
ZulfadlyAshBurn Posted September 9, 2011 Share Posted September 9, 2011 it is saving that the is no variable set as sr & ii. do you have those variable set? Quote Link to comment Share on other sites More sharing options...
ZulfadlyAshBurn Posted September 9, 2011 Share Posted September 9, 2011 i am also wondering where you get those from? Quote Link to comment Share on other sites More sharing options...
nortksi Posted September 12, 2011 Author Share Posted September 12, 2011 Hi ZulfadlyAshBurn, sorry for the late reply, I've been out of the office over the weekend. I am assuming that the variables have been set as I can display the data being retrieved from my 'businesses' table in the MySQL database. For example; I can display the company name and postcode that these variables hold. Could it be something to do with where abouts in my code these variables are placed? I am in a difficult position because I am signed into a non-disclosure agreement with my employers so I can not reveal to much of my code. Kind regards, Mark. Quote Link to comment Share on other sites More sharing options...
nortksi Posted September 12, 2011 Author Share Posted September 12, 2011 Just thought, Should I try passing the variables in the URL with the form? Is that even possible? Thanks, Mark. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.