Reputation: 13
I am still new to PHP but I have been making good progress on my site. However I seem to have hit a bit of a roadblock. I have a page that has a URL and in that URL there is a category Identifier. so it looks like;
www.website.com/createpage?cat=1
Now in the previous pages I have been passing the variable from page to page doing the following;
$cat = $_GET['cat'];
<a href="nextpage.php?cat=$cat">
However I have now come to the point where I need to use a form to process the information. I have a special script that runs on submit using the action= attribute.
If I try amending the link there to like the ones above, it just passes a string '$cat' through. I have read on several topics here that I can achieve the same thing by creating a hidden field in a form and calling it out on the new page like I would with the rest of the information. But in both cases it only passes a string through. If I echo the variable on the form before submission it reports the correct number, and if I put the processing script on the same page as the form it gets further than it does without (i have an error somewhere at the bottom of my script but I was expecting that)
Is there anything special about passing through values on forms that I am missing? Everyone post I read makes it sound simple, but I am struggling to replicate it on my own script.
I can post the script, but it is a hell of a long one.
echo '<form action="topic_process.php" method="POST">
<p>Topic Subject:<input name="topic_subject" type="text"></p><br>
<p>Post: <textarea name="post_content"></textarea></p><br>
<input type="hidden" name="cat" value="$cat">
<p><input type="submit" value="Create"></p>';
Upvotes: 0
Views: 61
Reputation: 67738
You can write it into a hidden input field of that form:
First, in PHP:
$cat = $_GET['cat'];
Later on, inside the HTML form:
<input type="hidden" value="<?php echo $cat; ?>" name="cat">
This will pass that value to the form processing page as a POST variable, which can be fetched (like all the other input values) as $_POST['cat']
Upvotes: 1
Reputation: 3864
Since your form string is delimited by single-quotes, you're sending $cat
as a string, you have to concatenate it :
echo '<form action="topic_process.php" method="POST">
<p>Topic Subject:<input name="topic_subject" type="text"></p><br>
<p>Post: <textarea name="post_content"></textarea></p><br>
<input type="hidden" name="cat" value="' . $cat . '">
<p><input type="submit" value="Create"></p>';
Please read this thread to understand your error
Upvotes: 1