justlukeyou Posted January 13, 2013 Share Posted January 13, 2013 Hi, I am trying to complete a button which inserts the ID number of the person logged in (followerid) against the ID of the profile page (profileid). So the person logged in wants to follow the user of the profile they are viewing. The code had a working inserts the ID as soon as the page is visited. However I am struggling to get it to work only when the button is pressed. I have tried various vardump and prints but cant seem to get anything to work. Does anyone have any suggestions please? <?php $followerid = intval($_SESSION['userID']); $profileid = intval($row['id']); if(isset($_POST['followbutton']) && $_POST['followbutton'] == 'true'){ if($profileid = $followerid) { $errors['profileid'] = "This is a test error."; } if(!$errors){ //Validation of input vars passed, attempt to run query //Force vars to be ints to be safe for query statement $followerid = intval($_SESSION['userID']); $profileid = intval($row['id']); $query = "INSERT INTO `follow` (`user_id`, `follow_user_id`) VALUES ('{$profileid}', '{$followerid}')"; $result = mysql_query($query); if (!$result) { $errors[] = "Query: {$query}<br>Error: " . mysql_error(); } } } ?> <div class="followbuttonbox"> <a href="<?php echo $_SERVER['PHP_SELF']; ?>?ID=<?php echo $profileid; ?>"><img src="/images/follow.png" id="followbutton" /></a> <input type="hidden" id="followbutton" value="true" /> </div> Quote Link to comment https://forums.phpfreaks.com/topic/273099-button-without-an-input-form/ Share on other sites More sharing options...
MDCode Posted January 14, 2013 Share Posted January 14, 2013 You first need a form for php to do anything. To get it on click you need to use javascript Quote Link to comment https://forums.phpfreaks.com/topic/273099-button-without-an-input-form/#findComment-1405414 Share on other sites More sharing options...
KevinM1 Posted January 14, 2013 Share Posted January 14, 2013 You either need a form or ajax. This comes down to the "Do you know the differences between PHP and Javascript?" question I asked you a few weeks ago. Quote Link to comment https://forums.phpfreaks.com/topic/273099-button-without-an-input-form/#findComment-1405470 Share on other sites More sharing options...
cyberRobot Posted January 14, 2013 Share Posted January 14, 2013 Have you considered merging the hidden field into the link? <div class="followbuttonbox"> <a href="<?php echo $_SERVER['PHP_SELF']; ?>?ID=<?php echo $profileid; ?>&followbutton=true"><img src="/images/follow.png" id="followbutton" /></a> </div> You would need to change and $_POST references to $_GET. Quote Link to comment https://forums.phpfreaks.com/topic/273099-button-without-an-input-form/#findComment-1405561 Share on other sites More sharing options...
justlukeyou Posted January 14, 2013 Author Share Posted January 14, 2013 Hi, I would prefer to continue using PHP. How would I add a form, Im sure almost there with it. The code was working when the page is visited. I just need to make it work only when the button is clicked. <form> <?php $followerid = intval($_SESSION['userID']); $profileid = intval($row['id']); if(isset($_POST['followbutton']) && $_POST['followbutton'] == 'true'){ if($profileid = $followerid) { $errors['profileid'] = "This is a test error."; } if(!$errors){ //Validation of input vars passed, attempt to run query //Force vars to be ints to be safe for query statement $followerid = intval($_SESSION['userID']); $profileid = intval($row['id']); $query = "INSERT INTO `follow` (`user_id`, `follow_user_id`) VALUES ('{$profileid}', '{$followerid}')"; $result = mysql_query($query); if (!$result) { $errors[] = "Query: {$query}<br>Error: " . mysql_error(); } } } ?> <?php if($errors['profileid']) print '<div class="invalid">' . $errors['profileid'] . ''; ?> </div> <div class="followbuttonbox"> <a href="<?php echo $_SERVER['PHP_SELF']; ?>?ID=<?php echo $profileid; ?>"><img src="/images/follow.png" id="followbutton" /></a> <input type="hidden" id="followbutton" value="true" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/273099-button-without-an-input-form/#findComment-1405673 Share on other sites More sharing options...
cyberRobot Posted January 14, 2013 Share Posted January 14, 2013 Here's a quick tutorial on building forms and processing them with PHP: http://www.tizag.com/phpT/forms.php Note that there are many other tutorials available through Google: https://www.google.c...rocessed by php Quote Link to comment https://forums.phpfreaks.com/topic/273099-button-without-an-input-form/#findComment-1405678 Share on other sites More sharing options...
justlukeyou Posted January 14, 2013 Author Share Posted January 14, 2013 Thanks, I have a number of forms which appear to be working fine. However I am stuck on creating one which has no input. The idea is to use it to insert the ID of the user. I have done a search, this is the closest I have found but it still low on detail. http://www.designerstalk.com/forums/help-me/53155-php-post-without-input.html I can echo the two values, its just a matter of inserting them into the database. echo $profileid; echo $followerid; Quote Link to comment https://forums.phpfreaks.com/topic/273099-button-without-an-input-form/#findComment-1405692 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.