Grabbing URL query parameter

In my HTML file I have links like this:
<a href="http://localhost/Appointments/Frontend/forms.php?btype=hairstyle">hair</a><br>

And with PHP code I can grab what is in that URL query parameter and use it for this:

<input type="hidden" id="buztype" value="<?php if (($_GET['btype'])=='hairstyle') {echo '1';}" ?> "/>

The value that is being echoed above it gets to be sent in the server.
And here is my problem:
When the user clicks the submit button(so that data can go to the server) the URL that appears in address bar is this…http://localhost/Appointments/Frontend/forms.php.

That said the PHP code in the value attribute of the input element above is useless,I get the familiar undefined index notice.

What can I do?I hope you understood my problem.

In your form code, you will have something like

<input type="text" name="firstname" size="20" maxlength="20">

If you’re using the POST method, in your PHP code that will be

echo $_POST['firstname'];

if I understand the question correctly. If not, what is the value of the method attribute in your form tag?

And what is the action attribute of the form?

You should first establish if $_GET['btype'] is set before testing it.

1 Like

This is the action attribute(the form tag of the html):

<form action="forms.php" method="post">

So as the tag is above no query parameter is attached to it.
I need the query parameters that appear in the URL(when this is clicked,as shown in my first post).
When the link(that displays the form) is clicked the query parameters are shown in the URL,these are lost though when clicking the submit button.

I’m not following your logic here.

This is how I read it.
You reach the form page via a link <a href="http://localhost/Appointments/Frontend/forms.php?btype=hairstyle">hair</a>
You get the variable from the link and use it to put a value in the form field which then gets posted.
If you want the same variable in the url after form submittion, it would need to be in the action too.

<form  action="forms.php?btype=<?php echo $_GET['btype'] ; ?>" method="post">

^ as mentioned before, you should really check if this is set first.

But I don’t know why you need that variable now. Does not the value in the hidden field carry that info?

Just noticed, your hidden field has no name.

Correct - if you use POST, the parameters are not passed as part of the URL. But as @SamA74 said, you seem to be going back to the same php script. If it’s possible that you’ll be getting to that same script either from a link that passes the details in as part of the URL, or as part of the post submit, you could do something like

$btype = "";  // create it so it exists regardless
$bcode = "";
if (isset($_GET['btype'])) { 
  $btype = $_GET['btype'];
  }
if (isset($_POST['btype'])) { 
  $btype = $_POST['btype'];
  }
if ($btype=="hairstyle") $bcode = "1";
?>
<input type="hidden" name="btype" id="buztype" value="<?php echo $bcode; ?> "/>

So you can deal with the input whether it comes from GET, POST or neither, and not have the conditional embedded inside the html code. But you must have a name parameter on the input or you won’t know how to access the field in the destination code.

I was afraid someone would say that…

Nonetheless You have got it right.

I am going to start from the above since it a very good starting point.
Yes I get the variable from the link and put it in the value attribute of an input tag(which is hidden)so that it gets POSTED to the server.(that is the logic here).This code attempts to do that-by reading the $_GET variable:

<input type="hidden" id="buztype" name="buztype" value="<?php if (($_GET['btype'])=='hairstyle')
                      {echo '1';} ?> "/>

When the submit button is clicked though the URL does not contain the query parameter.
Is there any way that the variable is retained after clicking the submit button without resorting to this solution(unless there is no alternative)you showed me:

<form action="forms.php?btype=<?php echo $_GET['btype'] ; ?>" method="post">

Making the form method get would put variables in the url, but then you won’t be using post for the form.

But the thing I can’t work out is why you need that variable after submission. Knowing that may reveal a better solution, for example, storing the info in a session or something, or using the value of the hidden field to determine what it should be.
I can’t follow what you are trying to achieve.

Ok then I will tell you what I want to do overall(the general picture).So far we have focused in the how.Maybe it is better to start with the what.(I might sound tedious but I cannot find other way explaining it better)

Just check this link http://hastebin.com/lakuvuxelu.scala to get I better undesranding-hopefully.

the problem is how am I going to send in ther server the btype whose link the user clicked

Since you are using s form with post method, why not put that data into hidden form input and retrieve that data from the $_POST array instead of getting from the url?

Though it appears you are already doing that here

<input type="hidden" id="buztype"  value="<?php if (($_GET['btype'])=='hairstyle')   {echo '1';}"   ?> "/>

Which is why I’m confused about what you are trying to do.

As said before, to put the variables in the url, you either insert them into the form action or use get instead of post method.
The other option to retain data from page to page is to use a session to store it.
Those are your options as far as I can see.

I agree and you are correct in what you say above.Till that point it’s OK.

The problem begins when clicking the submit button and no query parameters appear in the URL…at which $_GET gets to be udefined…

This…

This…

Well,it is complicated…I begun looking into inserting the GET variable into the form action such as this:
<form action="forms.php?btype=<?php echo $_GET['btype'] ; ?>" method="post">
On clicking the submit button it works and the value of btype gets inserted into the server and the reason for this is that the referrer page has in it’s URL the query parameter related to btype.
Nonetheless the important thing to point here is that if for any reason I have to click again the submit button(cause I did not fill an input field and got a warning about) we have a problem…$_GET btype gets to be udefined and that is because the referrer page does not contain btype as a query parameter in the URL.

Bottom line:the method of the form action does not do the job completely…probably I will try sending form data with GET and the reason I have not done it so far is cause is cause I have heard that with GET there are security implications.

I will see what I do…

Maybe using a session would be the way to go.
Or a hidden input.
Is there some special reason you want it to be a url variable? Or do you just need to carry data over from one page to the next?
I’m still confused about why you need this.

Another way to do this:

<form action="forms.php?btype=<?php echo $_GET['btype'] ; ?>" method="post">

as @SamA74 said earlier, is:

<form action="forms.php" method="get">
<input type="hidden" name="btype" value="<?php echo $_GET['btype']; ?>">

That way, all your form parameters are sent to forms.php in the $_GET array.

Maybe

It is a URL variable anyway cause it is embedded in the link and yes the data must be carried from page to page(this is unavoidable,page 1:links,page 2:form)till it reaches it’s destination…the server

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.