Jump to content

Recommended Posts

I have a query that I have run. I am able to output the dataset into a simple table it updates on my page beautifully. In the following code what I want to be able to do is along with the other columns i output have a 4th column that contains a button on each row and when a user clicks a button on the page it selects that particular row.

 

This is the code I have creating my table atm.

 

echo "<table alingn=\"center\" width=\"400px;\">";
		echo "<tr>
				  <td>ID</td>
				  <td>FirstName</td>
				  <td>LastName</td>
			  </tr>";
	   	while ($row = mysql_fetch_assoc($result)){
    		echo "<tr>
    		          <td>".$row['ID']."</td>
        		      <td>".$row['FName']."</td>
        		      <td>".$row['LName']."</td>
       		      </tr>";
   			}

   			echo "</table>";   

 

Basically everytime it loops through I would want the row that shows the ID to be a button that would execute a php script but pass it that ID. I dont really want the ID shown on the page.

 

 

Link to comment
https://forums.phpfreaks.com/topic/263908-formatting-output-from-a-query/
Share on other sites

Use a hidden input tag.

 

I gave this a shot but I get an error

 

while ($row = mysql_fetch_assoc($result)){
    		echo "<tr>
    		          <td>
				  	<form id=".$row['ID']." name=".$row['ID']." method="post" action="p_select.php">
				  		<input type="hidden" name="PID" value=".$row['ID'].">
						<input type="submit" value="Submit">
				  	</form>
				  </td>
        		      <td>".$row['FName']."</td>
        		      <td>".$row['LName']."</td>
       		      </tr>";
   			}

 

the error I get is Parse error: syntax error, unexpected T_STRING, expecting ',' or ';'   

The php string you are trying to echo starts and ends with double quotes - echo " ....... "; You are also trying to use double-quotes inside of that string around html attribute values. The first double-quote after the initial one closes the quoted string and anything after that is outside of the string and produces php syntax errors.

 

The easiest fix would be to use single-quotes for everything that is inside of that echo " ....... "; statement. You can also put php array variables directly inside of a double-quoted string by surrounding them with {}.

 

The following is the simplest php/html syntax that will do what you want -

 

while ($row = mysql_fetch_assoc($result)){
echo "<tr>
		<td>
			<form method='post' action='p_select.php'>
				<input type='hidden' name='PID' value='{$row['ID']}'>
				<input type='submit' value='Submit'>
			</form>
		</td>
		<td>{$row['FName']}</td>
		<td>{$row['LName']}</td>
	</tr>";
}

 

BTW - there's no need to give html elements id and name attributes unless you are actually going to reference those attributes. I did not include the id and name you had in your <form> tag. You might actually want to add a name attribute to the submit button so that you can test in the p_select.php code if a form was actually submitted.

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.