1

I am trying pass a parameter in action tag in form, just the way I am doing in href tag but this isn't working can I know why? or should I just use a href tag in form, will that overwrite action in form? Here is my code:

<!DOCTYPE html>
<html lang="en">
<?php 
   $ty=$_GET['param'];
   $name=$_GET['param1'];
   if($ty=='teacher')
   {
      $web = "<a href='teacherrepute.php?a=$name'>My repute score</a>"; 
      $rep = "<a href='teacherreported.php?a=$name'>My reported sites</a>";
      $blk = "<a href='newblocktryteacher.php?a=$name'>Block this site</a>";
      $unblk = "<a href='newtryunblockteacher.php?a=$name>Unblock this site";
   }
   else
   {
      $web = "<a href='pupilrepute.php?a=$name'>My repute score</a>"; 
      $rep = "<a href='pupilreported.php?a=$name'>My reported sites</a>";
      $blk = "<a href='newblocktrypupil.php?a=$name'>Block this site</a>";
      $unblk = "<a href='newtryunblockpupil.php?a=$name>Unblock this site";
   }
   // $type=$_GET['param2'];
   $courseA='A';
   $courseB='B';
?>
<body>
   <a href="reporttable.html"><?php echo $rep; ?></a>
   <FORM action = <?php echo $blk; ?>  method ="POST";>
      Block : <input type ="text" name = "url" /></br>
      <br>
      <input type="submit" value="block" />   
      <br>
   </FORM>
</body>
</html>
4
  • 2
    action attribute takes a url string, not an html string, for instance it should end up looking like action="pupilreported.php?a=somename" Commented Apr 27, 2015 at 16:53
  • @PatrickEvans i didn't want to do that because the php i run is decided based on the user so, i have put a if-else statement to decide that ! so i have to pass a parameter in the form u see, so clicking on button will start php based on the category of user Commented Apr 27, 2015 at 16:58
  • 1
    I am aware of that, I'm not saying to hard code the action, i am saying that with your current code, your action attribute is going to end up looking like action=<a href='newblocktrypupil.php?a=$name'>Block this site</a> which is invalid Commented Apr 27, 2015 at 16:59
  • @PatrickEvans so what do you suggest i do ? Commented Apr 27, 2015 at 17:09

3 Answers 3

1

1) As you already creating link based on condition then you can directly echo that variable inside markup. e.g.

 <?php echo $rep; ?>

2) Instead of passing html in form action just you pass that script name. e.g

newblocktryteacher.php?a=somename or newblocktrypupil.php?a=somename

Based on these two points your code will be

<!DOCTYPE html>
<html lang="en">
<?php 
   $ty=$_GET['param'];
   $name=$_GET['param1'];
   if($ty=='teacher')
   {
      $web = "<a href='teacherrepute.php?a=$name'>My repute score</a>"; 
      $rep = "<a href='teacherreported.php?a=$name'>My reported sites</a>";
      $blk = "newblocktryteacher.php?a=$name";
      $unblk = "<a href='newtryunblockteacher.php?a=$name>Unblock this site";
   }
   else
   {
      $web = "<a href='pupilrepute.php?a=$name'>My repute score</a>"; 
      $rep = "<a href='pupilreported.php?a=$name'>My reported sites</a>";
      $blk = "newblocktrypupil.php?a=$name";
      $unblk = "<a href='newtryunblockpupil.php?a=$name>Unblock this site</a>";
   }
   // $type=$_GET['param2'];
   $courseA='A';
   $courseB='B';
?>
<body>

   <?php echo $rep; ?>
   <form action="<?php echo $blk; ?>" method="POST">
      Block : <input type="text" name="url" /></br>
      <br>
      <input type="submit" value="block" />   
      <br>
   </form>
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

Your $blk isn't formatted in a way proper for <form>:

$blk = "<a href='newblocktryteacher.php?a=$name'>Block this site</a>";
<FORM action = <?php echo $blk; ?>  method ="POST";>

Assuming $name is bob in this example:

<FORM action = <a href='newblocktryteacher.php?a=bob'>Block this site</a>  method ="POST";>

As you can see, this is not correct. All you need is the URL itself. Also, remove the semicolon after "POST".

2 Comments

i have to categorize the user to teacher or pupil and then use the apropriate php , i cant hard code it to just teacher (i.e newblocktryteacher.php?a='bob')
You're missing the point. You can use PHP for the name. The issue is you're including <a> tags in a <form>'s action tag, and that makes no sense.
0

Your $blk is not a valid action form, because it's a <a> tag(link). So, $blk should be newblocktrypupil.php?a=$name' instead.

2 Comments

Why should the OP "try this"? Please add an explanation of what you did and why you did it that way not only for the OP but for future visitors to SO.
@Rizier123 can you suggest an answer after reading all my comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.