Jump to content

Recommended Posts

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>


Link to comment
https://forums.phpfreaks.com/topic/273099-button-without-an-input-form/
Share on other sites

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.

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>

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; 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.