Jump to content

[SOLVED] Passing Variables by URL issue


mattwal

Recommended Posts

Hello,

 

I am working on a comment form for my blog and am having trouble getting the variable  post_id that was passed through the URL inserted into my comments table on my database.

 

this is the link that i use to pass the post_id variable from the index page:

<a href=\"javascript:comment('comment_form.php?aid={$row['post_id']}');\" 
				title=\"Make A Comment On This Post\">Comment</a>

 

From there it does pass it to the comment_form.php page as comment_form.php?aid=3. So I do know it is working that far. From there I need to insert the "aid=3" or "3" being the post_id into a hidden form value to be inseted into the comments table.

 

This is my table definitions for comments:

comment_id post_id name comment date_entered

 

This is the comment_form page script:


<?php $page_title = 'Matt-Walko: Comment Page'; ?>

<?php 


	//This  script adds a comment entry to the database via the $_GET['aid'] variable.

	//address error handling
	ini_set ('display_errors', 1);
	error_reporting (E_ALL & ~E_NOTICE);

	if(isset ($_POST['submit'])) { //Handles the form

		//Connect and select
		require_once ("./assets/mysql_connect.php"); 

		//Define the query
		$aid = $_GET["aid"];
		$query = "INSERT INTO comments (comment_id, post_id, name, comment, date_entered) 
		VALUES (0, '{$_GET['aid']}', '{$_POST['name']}','{$_POST['comment']}', NOW())";

		//Execute the query
		if (@mysql_query ($query)) {
			print '<p>The blog entry has been added.</p>';
		} else {
			print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $query.</p>";
		}


		mysql_close();

} //end of form handler

//Display the form
?>
<?php $_GET['aid'] ?>
<form  class="form" action="comment_form.php" method="post">

    Name:   <input type="text" name="name" size="40" maxsize="100" /><br />
    Teaser: <textarea name="comment" cols="40" rows="10"></textarea><br />
            <input type="hidden" name="postid" value="<?php $_GET['aid'] ?>" />
            <input type="submit" name="submit" value="Add Comment!" />
</form>


 

When I add my comment to the database everything but the post_id is entered correctly...

Heres an example:

 

comment_id 	post_id 	name 	comment 	              date_entered
1 	                       0 	Matt         This is a comment       2008-11-24 02:16:20

The post_id Should be 3 instead of 0 but I didnt do something right...

I cant find a decent example to work from and thought someone on here might be able to help. I really would be most appreciative.

 

Link to comment
https://forums.phpfreaks.com/topic/133994-solved-passing-variables-by-url-issue/
Share on other sites

try

echo $_GET['aid'];

 

to see what value it has.

 

This:

<input type="hidden" name="postid" value="<?php $_GET['aid'] ?>" />

will not work.

 

You have to echo the value:

<input type="hidden" name="postid" value="<?php echo $_GET['aid']; ?>" />

\And the form itself uses POST, and not GET, so the postid is passed to $_POST['aid']

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.