Jump to content

Recommended Posts

Is there anyway I can make it so that the code only runs when I click the button.

 

 

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

 

Should I be using the name from the button?

Hi,

 

Is there a way to only allow the code to run if the button is pressed?

 

I thought I could use this. I thought it said that the followbutton has to be clicked?

 

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

 

Im going to add a duplicate logic. I dont know how to do this but I am trying to find out with another page. I shall also add one so that a user cant follow themselves. Although the button will be removed if the user is logged in and viewing their own profile.

 

You are the one who who mentioned $_POST in your replies #4 and #6 ::)

@justlukeyou -

 

We're becoming more and more frustrated with your posts and topics because you're not demonstrating the ability to learn from your previous problems. So, we continue down this cycle of you asking a basic question, one of us answering it in about a clear and concise manner that we can, you not understanding it, and the whole thing boiling down into an exercise in frustration as we attempt to explain how something simple like variable assignment, or print_r(), or whatever else is nearly self-evident works instead of actually addressing the problem.

 

As I've said before, you need to work on your ability to read and comprehend. The PHP manual is written at a middle school level. You need to be able to read it, understand it, and use that knowledge in your own code if you're ever going to be able to do this for real. I'm not saying this to beat you down. It's just the reality of the situation. No one is ever going to hire a developer that can't read documentation.

 

It's for your own benefit. At some point, we'll get so frustrated that we'll just stop answering your questions. We're all volunteers here, so none of us get paid for helping others. At some point, we all get to the point where it's simply not worth our time or effort to answer certain questions. After 726+ posts, and being registered here since 2007, you need to show us that you're growing as a developer, that you can understand what PHP warnings and errors mean, and that you can do basic debugging yourself. Otherwise, those that do answer your questions will become more snarky and show less patience until no one will even bother.

 

It's just human nature. To put it in other terms, you wouldn't expect a garage to explain how a wheel works every time you went in, or a kitchen explain how heat cooks food every time you ate, would you? Because that's what your topics boil down to here - an explanation of things you should be able to grasp by reading documentation, or simply writing your own small test scrips. And soon, we'll simply not bother to explain it.

 

Again, I'm not trying to be mean or insulting. I'm just trying to explain where we're all at. We're losing patience, so you need to show us that our efforts won't be wasted. You need to grow.

I appreciate your concerns but my site is coming on well. I can now things in minutes six months ago would take literally weeks!

 

I just understand the PHP site. For example, what page explains clearly how to only post when the button is clicked.

 

The code I have currently works but without seeing how it should be done I dont know how it should be done properly.

I just understand the PHP site. For example, what page explains clearly how to only post when the button is clicked.

 

Back to basics.

 

Button clicking and posting is done by the browser on the client.

PHP runs on the server in response to the post.

 

PHP therefore has no control over button clicking. For that you need to process on the client - eg javascript

 

 

As opposed to what? If either of the IDs are not set, don't execute the code. If you are wanting someone to prevent using Refresh to insert a new record there are several options. Here are two:

 

1. After the record is added, redirect the user to a different (or the same) page using a header() function. This will wipe out any GET/POST values.

2. Put in logic to prevent a duplicate records

Hi,

 

With point 1. (1. After the record is added, redirect the user to a different (or the same) page using a header() function. This will wipe out any GET/POST values.) I'm stuggling to understand how this would be useful as the code runs even by following the page.

 

"After the record is added". What if i dont want the record to be added?

I added the followbutton post. It doesn't create any errors but now it doesn't echo the profileid to run the query when the button is pressed.

 

 

<div class="followbuttonarea">

<?php

$errors = array();
if(!isset($_SESSION['userID']))

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


//Validate the input vars
{
       $errors[] = "\$_SESSION['userID'] value is not set.";
}
if(!isset($row['id']))
{
       $errors[] = "\$row['id'] value is not set";
}


     if 
	  ($userid == $follow_user_id)  {
           $errors['cantfollowself'] = 'You cannot follow yourself.';		     
	}   



if(!count($error))
{
       //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>
<div class="followbutton"> 
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?ID=<?php echo $profileid; ?>"><img src="/images/follow.png" id="followbutton" /></a>
</div>

The code I have currently works but without seeing how it should be done I dont know how it should be done properly.

 

And, like I've said many times before, this is exactly the wrong way to approach it. Programming is not like learning Photoshop. Going at it like "I want to make a button that posts things" and then trying to find a tutorial that only does that is hurting you.

 

You really need to focus on understanding how things work in general. When the description of print_r() says something along the lines of "Prints out the contents of the variable in human readable format" you need to know what a variable is and what human readable format means. Likewise, you need to learn the difference between server side programming (which is what PHP is, and cannot interact with the web browser directly) and client side programming (Javascript, which is used to directly manipulate HTML (which is where buttons live *hint*)).

 

So, quick quiz:

 

Do you know what a variable is?

Do you know what an array is?

Do you know what a loop is?

Can you write a while-loop?

Can you explain the differences between a while-loop, for-loop, and a foreach?

Can you explain the difference between = and ==?

Can you explain the difference between return and echo?

Can you write and execute your own function?

Can you explain the differences between PHP and Javascript?

 

If the answer to any of those questions is anything but a confident "Yes," stop what you're doing, get yourself a good book (http://www.amazon.com/PHP-MySQL-Dynamic-Web-Sites/dp/0321784073/ref=sr_1_2?s=books&ie=UTF8&qid=1356658475&sr=1-2&keywords=ullman), and actually learn.

Edited by KevinM1

Hi,

 

I am spent last few hours trying to use print_r however I am finding something I cant understand.

 

The first set of prints displays the $followerid and $profileid. However the second set of prints underneath the POST do not display anything. Any suggestions why this maybe please? I have tried to juggle things around but its still not displaying inside the POST function. However it is now echoing the rows into the button.

 

 

 

$followerid = intval($_SESSION['userID']);
       $profileid  = intval($row['id']);


   print_r ($followerid); 
   print_r ($profileid);
   print_r ($_POST['followbutton']);
   print_r ($followbutton);


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

       print_r ($followerid); 
   print_r ($profileid);
   print_r ($_POST['followbutton']);
   print_r ($followbutton);

 

 

 

 

<?php



$followerid = intval($_SESSION['userID']);
       $profileid  = intval($row['id']);


   print_r ($followerid); 
   print_r ($profileid);
   print_r ($_POST['followbutton']);
   print_r ($followbutton);


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

print_r ($followerid); 
   print_r ($profileid);
   print_r ($_POST['followbutton']);
   print_r ($followbutton);



   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


       $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
       print_r ($followerid); 
   print_r ($profileid);
   print_r ($followbutton);
   print_r ($_POST['followbutton']);

   ?>

      <?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" /> 

Edited by justlukeyou

Print_r() is wasted on ordinary variables, you can just use echo on those.

 

It is most useful on arrays or objects.

 

If you want to know what's stored in your $_SESSION array

echo '<pre>' . print_r($_SESSION, true) . '</pre>';

 

If you want to know what was POSTed from your form

echo '<pre>' . print_r($_POST, true) . '</pre>';

 

The <pre> tags make the output more readable.

Cheers dude,

 

These are the results I get. I take it the POST is faulty.

 

echo '<pre>' . print_r($_SESSION, true) . '</pre>';

(
   [userLoggedIn] => 1
   [userEmail] => email@yahoo.com
   [userID] => 355
   [userfirstname] => Tony
   [usersurname] => Bobbins
)

 

echo '<pre>' . print_r($_POST, true) . '</pre>';

Array
(
)

 

Also when I use the following code it displays a 1. Does that mean anything. Does it just mean it is being used once?

 


echo print_r ($followbutton);

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.