Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I understand how a PHP URL works - I think ... but I'm having problems getting the actual value of the variable to be passed in the example below.

Example

Note: I am adding the below form into a data cell (as part of a table being read via PHP).

$currentrowid = 1;

echo '<td>
  <div class="editdelete">
    <form action="phpindex.php?page=edit&thisrow=<?php echo $currentrowid;?>" method="post">
      <input type="submit" value="Edit" >
    </form>
  </div>
</td>';

... Some other section of code to read the URL output by the form above:

$val = $_POST['thisrow']; 
echo "the value is: " .$val; //Outputs "$currentrowid"

So, as you can see the code returns the actual name of the variable being passed, NOT the value of the variable being passed.

Any ideas here?

share|improve this question

4 Answers

up vote 3 down vote accepted

Since you are already within a PHP block, you should not wrap your variable within <?php ... ?>. This will give you an error.

To make this work, you can choose 1 of 2 options:

1) String Concatenation:

echo '... <form action="phpindex.php?page=edit&thisrow='.$currentrowid.'" method="post"> ...';

2) Wrap your string in " (double quotes) instead of ' (single quotes):

echo "... <form action=\"phpindex.php?page=edit&thisrow=$currentrowid\" method=\"post\"> ...";

Note that the second method forces you to escape all the double quotes inside of your string.

share|improve this answer
3  
@Even: because this is elementary PHP code. I do not mean to be rude, but if you didn't know that, you should seriously read up on PHP. – Joseph Silber Aug 23 '11 at 2:10
1  
@Evan You have better learn some php basics. – xdazz Aug 23 '11 at 2:12
@Evan: if you're getting an error that "there is an extra bracket", it must be in a part of the code you have not posted (although I do see that between $currentrowid=10; and <form there's no ?>). – Joseph Silber Aug 23 '11 at 2:15
@Josesph this error only exists when I add in the PHP part. It may just be web expression. In which case ... perhaps the comment above may not have been as necessary as you had expected ... – user725913 Aug 23 '11 at 2:17
@Joseph Please see my latest update, I think I left out an important part of the question - now it may make more sense as to why it is not working. I am not writing an HTML form, this form is being created using PHP. – user725913 Aug 23 '11 at 2:49
show 2 more comments

2 point.

  1. <form action="index.php?thisrow=<?php echo $currentrowid ?>" method="post">
  2. You should use $_POST not $_GET to get the post value.
share|improve this answer
He's not getting the post values from the form. He's getting the URL value, which he's correctly getting through $_GET. – Joseph Silber Aug 23 '11 at 2:11
@Joseph oops, you are right. Use $_GET to get query string value. – xdazz Aug 23 '11 at 2:19

As what was answered above,

<form action="index.php?thisrow=<?php echo $currentrowid; ?>" method="post">

is correct. The reason behind this is you are passing HTML and you have to use an echo from php to output to the html. Otherwise you just get exactly what you put, which is $currentrowid.

share|improve this answer

Not the easiest, but a quick way to solve your problem. Change your form method to get method="get">, then

$val = $_GET['thisrow']; 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.