SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    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.

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

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Where's the code for the button itself?

  3. #3
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

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


    Code:
    <?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>

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    If that's not all the code of your form and it actually works, then you should add name attribute to you hidden field.

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

  5. #5
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.

    Code:
    <?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" name="followbutton"  value="true">
    <submit button>
    </form>

  6. #6
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    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
    PHP Code:
    error_reporting(E_ALL);
    ini_set('display_errors'1); 
    Also, to check what values are submitted from form, you can use
    PHP Code:
    var_dump($_POST); 

  7. #7
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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) { }

  8. #8
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    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
    Code:
    <!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.

  9. #9
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

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

    Code:
    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.

  10. #10
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

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

  11. #11
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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?



    Code:
    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}<br>Error: " . mysql_error();
    
                                    }
                    }          
    
    
                            echo $profileid;
    echo $followerid;

  12. #12
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    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.
    PHP Code:
    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.

  13. #13
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.


    Code:
    <!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>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •