Chris
Chris

Reputation: 41

GET form method wont append parameters

I am working for a school project written in PHP for some weeks and i am stuck.

I am using for my structure a page index.php?page=.... where page could be home , game, aboutus,contact etc.

Now I am in index.php?page=game and I want to send using a form with GET method some parameters like game_name, created_by and stage_paused. Here comes the problem because when I press the Submit button I lose my "page=game" parameter and is something like index.php?game_name=...&created_by=...., and I don't have anymore my page=game, and by default my script check the page parameter and if not it renders the home page.

I checked w3 standards and they say that GET method shoud APPEND my form parameters with the one that is specified in the action ="" on the form, but in my case I lose my page parameter.

I don't get it why it doesn't remember my current parameters when I submit the form.

I would appreciate very much your help and sorry for my bad english.

Thanks !

Upvotes: 4

Views: 5144

Answers (3)

nickb
nickb

Reputation: 59699

Add the page parameter to either:

  1. Your <form> action URL, or
  2. Your <form> as a hidden <input> element, like so:

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

Upvotes: 5

Bailey Parker
Bailey Parker

Reputation: 15905

You need to add a hidden field for page:

 <input type="hidden" name="page" value="game" />

You could also change the form's method to POST and add the page to the action:

<form action="index.php?page=game" method="POST">

Upvotes: 2

misberner
misberner

Reputation: 3688

A simple hidden input element should do the trick:

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

This way, the page parameter supplied via the URL is also considered as part of your form and therefore appended to the form action URL.

Upvotes: 3

Related Questions