-2

I am trying to run the below query which basically create a submit button. However when I am trying to click the Accept Request button it is not resolving the if condition and directly echo what is written in else condition. Can someone help why this would be happening?

<?php
  if(isset($_POST['acceptrequest'.$user_from]))
  {
    echo "you are now freind !";
  } else 
  {
    echo 'Error in reading - acceptrequest'.$user_from;
  }
?>


<form action = "friend_request.php" method = "POST">
<input type="submit" name = "acceptrequest<?php echo $user_from;?>"   
       value="Accept Request" style = "margin-left: 5px;" />
<input type="submit" name = "ignorerequest<?php echo $user_from;?>"  
       value="Ignore Request" style = "margin-left: 5px;" />
</form>
josliber
  • 43,891
  • 12
  • 98
  • 133
  • Don't put spaces between attribute names and values in the HTML tags. It should be `
    `
    – Geoff Atkins Sep 14 '15 at 15:03
  • 1
    where is `$user_from` initially being defined? this should be throwing you an undefined user_from variable notice. – Funk Forty Niner Sep 14 '15 at 15:04
  • possible duplicate of http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index – Funk Forty Niner Sep 14 '15 at 15:12
  • 1
    you're not helping here by not interacting. I am closing this tab now. @ me if you need me. *ciao!* – Funk Forty Niner Sep 14 '15 at 15:20
  • Add the code where `$user_from` is set, please. – Steven Moseley Sep 14 '15 at 15:44
  • @GeoffAtkins having spaces between name and value is not a problem (at least it should be one). And it is valid according to the specs [W3C: Attributes](http://dev.w3.org/html5/spec-LC/syntax.html#attributes-0) `[...]The attribute name, followed by zero or more space characters, followed by a single U+003D EQUALS SIGN character, followed by zero or more space characters, followed by the attribute value, [...]` – t.niese Sep 14 '15 at 15:46
  • the $user_from variable resolving as when ever I am submitting the button it showing me "Error in reading - acceptrequestpbhatt1984@gmail.com" .. for some reason the if part - if(isset($_POST['acceptrequest'.$user_from])) is not resolving – Akhil Prasad Godiyal Sep 15 '15 at 12:59

2 Answers2

0

Maybe You haven't set all variables. Try to define that and use: $

So create something like: $acceptrequest == something

David Stančík
  • 340
  • 6
  • 23
0

In general, you ought not use variables to define the name of a form input.

Specifically because of the "variable" nature of variables, you can't be sure that the name posted will match the name you're looking for.

In order to keep your application stateless, you should instead post the variable kn its own hidden input.

Below, I modified your form to post user_from as its own input, separate from acceptrequest and ignorerequest.

This should fix any state issues that you were experiencing before.

<?php
  if (isset($_POST['acceptrequest')) {
      echo "You are now friend with {$_POST['user_from']}!";
  } else if (isset($_POST['ignorerequest')) {
      echo "You ignored the request from {$_POST['user_from']}!";
  } else {
      echo 'Error in reading - acceptrequest'.$user_from;
  }
?>


<form action = "friend_request.php" method="POST">
<input type="hidden" name="user_from"
       value="<?php echo $user_from;?>" />
<input type="submit" name="acceptrequest"   
       value="Accept Request" style = "margin-left: 5px;" />
<input type="submit" name="ignorerequest"  
       value="Ignore Request" style = "margin-left: 5px;" />
</form>
Steven Moseley
  • 15,871
  • 4
  • 39
  • 50