Finish Final Stage of Social Networking Button

Hi,

I am trying to complete a button which allows someone to follow somone else. It is designed to be a social networking button. I could get the button to work whever the page is loaded which is highly undersirable but I cant make it work when the button is pressed.

I have really stuck on this, does anyone have any suggestions on what I can try to get it to work.

<?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();

        }
	}	
	}
	
	
?>

Where’s the code for the button itself?

Hi,

This is the code including the button. Apologies I didn’t add it earlier.

<?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">
<submit button>
</form>
</div>

If that’s not all the code of your form and it actually works, then you should add name attribute to you hidden field.

<input type="hidden" id="followbutton" name="followbutton" value="true">

Thanks,

I added but it has had no affect. Is there anyway I can strip it down or test in some way. I have tried using a debugger but I dont understand how they work.

&lt;?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}&lt;br&gt;Error: " . mysql_error();

        }
	}	
	}
	
	
	
?&gt;




       &lt;?php if($errors['profileid']) print '&lt;div class="invalid"&gt;' . $errors['profileid'] . ''; ?&gt;



&lt;/div&gt;
&lt;div class="followbuttonbox"&gt;
&lt;a href="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;?ID=&lt;?php echo $profileid; ?&gt;"&gt;&lt;img src="/images/follow.png"  id="followbutton"   /&gt;&lt;/a&gt;
&lt;input type="hidden" id="followbutton" name="followbutton"  value="true"&gt;
&lt;submit button&gt;
&lt;/form&gt;

Is that all the code in your php file? If so, then you must add more html and make a valid form.
Anyway you can check for php errors setting this in the beginning of your file


error_reporting(E_ALL);
ini_set('display_errors', 1);

Also, to check what values are submitted from form, you can use


var_dump($_POST);

Many thanks, Im not totally sure what you mean by “add more html and make a valid form”.

I dont want the form to appear on the page. Do you which parts of the HTML are missing?

When I use the vardump it echoes the following but I dont know what this means.

array(0) { }

Well, by add more html I mean that currently your page is incomplete and the form to get submit on button click you need a valid form.
In your case, something like this


<!DOCTYPE html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>title</title>
</head>
<body>
    <form action="" method="post">
        <input type="hidden" id="followbutton" name="followbutton"  value="true">
        <input type="submit">
    </form>
</body>
</html>

var_dump($_POST) outputting array(0) { } means, nothing was sent to script in POST variable, in other words, form was not submitted.

Thanks,

Is this part correct, this is the part which stops the code from working when someone visits the page.

if(isset($_POST['followbutton']) && $_POST['followbutton'] == 'true'){

When I echo the profileid and followerid it does display the correct usersid as I expect but I just cant insert them into the database.

Hi,

Does anyone have any suggestions please? I’m so confused I’m not even sure what this part is trying to do.

Hi,

What Ive noticed is that when I try to echo the two ID inside the isset code they do not publish on the screen. I have marked them with “THIS DOES NOT ECHO”.

Does help in anyway or explain what the issue is?


echo $profileid;
echo $followerid;


if(isset($_POST['followbutton']) && $_POST['followbutton'] == 'true'){




echo $profileid;                THIS DOES NOT ECHO
echo $followerid;                  THIS DOES NOT ECHO






                $query = "INSERT INTO `follow` (`user_id`, `follow_user_id`) VALUES ('{$profileid}', '{$followerid}')";
                $result = mysql_query($query);


                if (!$result)
                {
                                $errors[] = "Query: {$query}&lt;br&gt;Error: " . mysql_error();

                                }
                }


                        echo $profileid;
echo $followerid;

This line checks if followbutton is sent to script via post and if it’s value is ‘true’, if that criteria is not met, then code inside this if statement is not executed.


if(isset($_POST['followbutton']) && $_POST['followbutton'] == 'true'){

POST variable is passed to script via HTTP POST method, usually submitted via form, as in your script. In your script social button submits the form triggering the condition in if statement to be true and therefore running the code inside it. As I mentioned previously the problem was in your html code, preventing correctly submitting the form.
I would suggest put some effort into learning PHP, instead of just using scripts found on the web.

Hi,

At the moment I reading a French dictionary to find one word.

I used this and set up a seperate page but that didn’t work either.

<!DOCTYPE html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>title</title>
</head>
<body>
    <form action="" method="post">
        <input type="hidden" id="followbutton" name="followbutton"  value="true">
        <input type="submit">
    </form>
</body>
</html>