Using $_GET and $_POST together in a form

Can you retrieve a variable from the URL using $_GET, directly within a form action, and still make the form method post?

For example [php] ‘<form action="pagename.php’."?id=".$_GET[‘id’]." method=“post”>[/php]

Basically, I need the form to read the record ID # from the URL, submit data using that same ID, and then reload the page with the same ID appended onto the URL again.
Is this possible?

well if you wanna
but you have to use {$_REQUEST[‘id’]}

Thanks cabalsdemon. I’m gonna risk sounding stupid here, (not very seasoned w/ php), so:

Is $_REQUEST in place of or in addition to the code snippet below?

I’m not familiar with it, and the manual entry for $_REQUEST confuzzled me even more.
Would I use this {$_REQUEST[‘id’]} prior to <form… > or within the form tags?

Actually, strike that last comment - I’m all set, thank you again for your help.

Depends on where the information is coming from for the id. If its from another form, then just put it in a hidden input, then you can use post. If its coming from a url, then you can use get. request is depreciated anyways.

That’s the kicker, it’s coming from both - the first time this page is accessed, it comes from a link (URL).
But the form is on the same page, and the intention is to use this form over and over once you get there, to keep adding records. So first time you get there, it comes from the URL. Each time after that, it will come from the form.

Use a hidden input and an if statement, something like
// page top, under session_start() if there is one
if(isset($_GET[‘id’])) {
$id = $_GET[‘id’];
} else {
$id = $_POST[‘id’];

You’ll need to use a hidden input in your form for the id for that to work

You give that a whirl, not really sure if it’ll work though since the form action is redirecting to another page.

Thanks richei. I don’t know if I’ll ever get it to work the way I have been approaching it - I may need a separate page to add records. if(isset($_GET[‘id’])) will always be true, because the form is reloading the same page / script with the id value in the URL, and then the script immediately checks if the URL has an ?id value.

But if I have , I could possibly utilize this - make that the first logical check. That way, it’ll ignore the URL on all occasions other than the first time the page is accessed.

[php]if(isset($_POST[‘submitted’])) {
$id = $_POST[‘id’];
} else {
$id = $_GET[‘id’];

I’m gonna keep wrestling with this thing, I am determined to make it work.
Thanks again BTW - I really appreciate your time and brain power here - I have been driving myself nuts trying to logic this out alone.

OK, I have concluded that what I hoped to do is logically impossible within the same script / click.
The form needs to assign the ID, but needs to know the ID to know what to assign.
It’s like asking a mother to give birth to herself. ???
I’m gonna have to redirect and add a whole other click to this process. The user will have to forgive me. ::slight_smile:

Maybe down the road I can get it AJAXed up so an applet does this for me. But I have a long way to go before I reach that part of the process.

Well, let me ask you this - when the user enters the page for the first time, where is the action id coming from?

Essentially, I’m building a prog which allows you to create a questionnaire (of sorts).

I have 2 database tables: TemplateHeaders & TemplateQuestions. This is a one to many relationship - one header, many questions.
TempHeaders just contains the template ID (Primary key), Template Name and who that template belongs to, (Company ID - Foreign Key).
TempQuestions contains the Question ID (primary key), the parent Template ID (Foreign key), and the text of the question.

TempDashboard.php lists out ALL the templates currently in development, (from the TempHeaders table). Each item in this list has a link beside it to edit that item, using a second script, TempDev.php. <-- This page lists all questions added to that template so far, from the TempQuestions table, and has a form to keep on adding to it.

So from the main page, TempDashboard.php, the link is formed like this:

// and then

This link is where the ID is coming from, first time the page is accessed. But each time a question is added, I need a way to pass the Parent Template ID back into the TempQuestions table along with it.

It’s pretty simple, but I wanted to submit the question, write to the database, read from the database and reload the same page (so it shows the newly added question), all in the same click.
Does that make sense?

(I realize that one more click per submission is not that big of a deal, but if you’re adding loads of questions then the second clicks will get old fast.)

so you are starting with a empty id then right
then you would want start with what richei gave you on the pages that accept the link and if they access them right off the bat you may want to redirect them to the actual first page they need to be on unless they are users then maybe have a session to take them back to the page they were on

are the links they pick going to be drop down or actual links

do you have a first page made up yet

Yes, I am starting with an empty ID. The only place this ID exists is in the URL.

The first page is mostly made up - at least enough to test it out. (Meaning the one listing out all the templates in development?)

read more of the above i edited it
thanx heh didnt know you would read it that fast
post link to my I.M so i can see page and we swill see what you are wanting to do

just a link to the page in my I.M and richei’s if he wants just want to see you basic layout of a page the someone would see

None of this is online yet, I’m just working locally using WAMP.
I’m gonna get some web-space when I’m closer to having a working app, but I’m right at the beginning now, & have got a ways to go to get this whole program built.

I will definitely do what you suggested regarding the sessions too - they will always be logged in as users, & if they accessed the page off the bat without the ID, then the script would fail.
The links aren’t in a drop down, it’s just a list in an html table, like:

[td]Template 1[/td][td]In Development[/td][td]Edit[/td][td]Delete[/td]
[td]Template 2[/td][td]In Development[/td][td]Edit[/td][td]Delete[/td]
[td]Template 3[/td][td]Published[/td][td]Edit[/td][td]Delete[/td]


You guys are obviously regulars here, so I’m gonna come here all the time from now on. Along the way, as I get closer & upload it somewhere, I’d like to keep tapping into your knowledge. I feel like a baby trying to run before I can walk right now!

I keep saying this over and over, but thank you again for helping me out here.

Sponsor our Newsletter | Privacy Policy | Terms of Service