How to keep value received through GET method?

Web programming topics
Post Reply
Ranura
Corporal
Corporal
Posts: 6
Joined: Wed Jul 20, 2011 3:07 pm

How to keep value received through GET method?

Post by Ranura » Sun Dec 02, 2012 5:26 pm

I want to keep the value "$aut_id" which is received through GET method.
It is destroyed (as usual) when I click the submit button.
Insert query needs "$aut_id" value after the clicking of submit button.
How can I achieve this?
Following is my code

Code: Select all

<?php
	mysql_connect("localhost", "root", "123") or die (mysql_error ());
	mysql_select_db("popup_comment_box") or die(mysql_error());

	$name=$_POST['name'];
	$comment=$_POST['comment'];
	$submit=$_POST['submit'];
	$aut_id=$_GET['aut_id'];
	
	if($submit){
		if($name&&$comment){
			$query = "INSERT INTO tbl_comments 
			(name, comment, timestamp, aut_id) 
			SELECT '$name', '$comment', NOW(), aut_id 
			FROM tbl_automobile 
			WHERE aut_id = '$aut_id' LIMIT 1";
			
			mysql_query($query)  or die(mysql_error());
			
			echo "Your comment is successfully posted"."<br />";
			
		}else{
			echo "Please fill out all fields"."<br />";
		}
	}
?>
<html>
	<head>
	</head>

	<body>
		<form action="popup.php" enctype="multipart/form-data" method="post">
			<table>
				<tr><td>Name: <br><input type="text" name="name"/></td></tr>
				<tr><td>Comment: </td></tr>
				<tr><td><textarea name="comment" rows="10" cols="50"></textarea></td></tr>
				<tr>
					<td>
						<input type='submit' name='submit' value='Comment'>
					</td>
				</tr>
			</table>
		</form>
		
		<?php
			$strSQL = "SELECT * FROM tbl_comments ORDER BY comment_id DESC";
			$rs = mysql_query($strSQL) or die(mysql_error());
			while($row = mysql_fetch_array($rs)){
		?>
			<table width="430px">
			<tr>
				<td colspan="2" valign="top">
					<?php
						echo "by "."<i>".$row['name']."</i>";
					?>
				</td>
			</tr>
			<tr>
				<td valign="top">
					<?php
						echo $row['comment'];
					?>
				</td>
				<td valign="top">
					<?php
						echo "<p align='right'>".$row['timestamp']."</p>";
					?>
				</td>
			</tr>
			<tr>
				<td colspan="2" valign="top">
					<?php
						echo "Foreign key "."<i>".$row['aut_id']."</i>";
					?>
				</td>
			</tr>
			<hr size="1"/>
		</table>
		
		<?php } ?>
			
			
	</body>

</html>
User avatar
Rksk
Major
Major
Posts: 730
Joined: Thu Jan 07, 2010 4:19 pm
Location: Rathnapura, Sri Lanka

Re: How to keep value received through GET method?

Post by Rksk » Sun Dec 02, 2012 11:49 pm

Ranura wrote:I want to keep the value "$aut_id" which is received through GET method.
It is destroyed (as usual) when I click the submit button.
Insert query needs "$aut_id" value after the clicking of submit button.
How can I achieve this?
Following is my code

Code: Select all

<?php
    mysql_connect("localhost", "root", "123") or die (mysql_error ());
    mysql_select_db("popup_comment_box") or die(mysql_error());

    $name=$_POST['name'];
    $comment=$_POST['comment'];
    $submit=$_POST['submit'];
    $aut_id=$_GET['aut_id'];
    
    if($submit){
        if($name&&$comment){
            $query = "INSERT INTO tbl_comments 
            (name, comment, timestamp, aut_id) 
            SELECT '$name', '$comment', NOW(), aut_id 
            FROM tbl_automobile 
            WHERE aut_id = '$aut_id' LIMIT 1";
            
            mysql_query($query)  or die(mysql_error());
            
            echo "Your comment is successfully posted"."<br />";
            
        }else{
            echo "Please fill out all fields"."<br />";
        }
    }
?>
<html>
    <head>
    </head>

    <body>
        <form action="popup.php" enctype="multipart/form-data" method="post">
            <table>
                <tr><td>Name: <br><input type="text" name="name"/></td></tr>
                <tr><td>Comment: </td></tr>
                <tr><td><textarea name="comment" rows="10" cols="50"></textarea></td></tr>
                <tr>
                    <td>
                        <input type='submit' name='submit' value='Comment'>
                    </td>
                </tr>
            </table>
        </form>
        
        <?php
            $strSQL = "SELECT * FROM tbl_comments ORDER BY comment_id DESC";
            $rs = mysql_query($strSQL) or die(mysql_error());
            while($row = mysql_fetch_array($rs)){
        ?>
            <table width="430px">
            <tr>
                <td colspan="2" valign="top">
                    <?php
                        echo "by "."<i>".$row['name']."</i>";
                    ?>
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <?php
                        echo $row['comment'];
                    ?>
                </td>
                <td valign="top">
                    <?php
                        echo "<p align='right'>".$row['timestamp']."</p>";
                    ?>
                </td>
            </tr>
            <tr>
                <td colspan="2" valign="top">
                    <?php
                        echo "Foreign key "."<i>".$row['aut_id']."</i>";
                    ?>
                </td>
            </tr>
            <hr size="1"/>
        </table>
        
        <?php } ?>
            
            
    </body>

</html>
You haven't used any way to send $aut_id via POST or GET.

You may can send $aut_id via POST method my adding a hidden input box to your form.

Code: Select all

<input type="hidden" name="aut_id" value="<?php echo $aut_id; ?>" />
Also you can send $aut_id via GET method by adding it to end of the URL.

Code: Select all

<form action="popup.php?aut_id=<?php echo $aut_id; ?>" enctype="multipart/form-data" method="post">
Ranura
Corporal
Corporal
Posts: 6
Joined: Wed Jul 20, 2011 3:07 pm

Re: How to keep value received through GET method?

Post by Ranura » Mon Dec 03, 2012 8:05 am

Rksk wrote:
Ranura wrote:I want to keep the value "$aut_id" which is received through GET method.
It is destroyed (as usual) when I click the submit button.
Insert query needs "$aut_id" value after the clicking of submit button.
How can I achieve this?
Following is my code
You haven't used any way to send $aut_id via POST or GET.

You may can send $aut_id via POST method my adding a hidden input box to your form.

Code: Select all

<input type="hidden" name="aut_id" value="<?php echo $aut_id; ?>" />
Also you can send $aut_id via GET method by adding it to end of the URL.

Code: Select all

<form action="popup.php?aut_id=<?php echo $aut_id; ?>" enctype="multipart/form-data" method="post">
Thanks a lot for your help!!!
Found solution through your comments.
Post Reply

Return to “Web programming”