PHP get request with multiple parameters from different forms - php

I'm new to PHP and have a little problem. I'm trying to set up a get Request with two parameters. The URL should for example look like this:
search.php?query=Harry+Potter&page=movies
The problem is, that i get the two parameters 'query' and 'page' from two different forms.
My Code is the following:
Form 1:
<form action="search.php" method="get" class="navbar-form" role="search">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name="query">
<div class="input-group-btn">
<button class="btn btn-default c-nav-btn-height" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
Form 2:
<form class="btn-group-vertical" action="search.php" method="get">
<button id="bMovies" class="btn active-menu" type="submit" name="page" value="movies">Filme</button>
<button id="bSeries" class="btn" type="submit" name="page" value="series">Serien</button>
</form>
Are there any possibilities to combine two forms in one get Request?

I as able to fix the problem with some help from the comment section - thanks to #Magnus Eriksson. Unfortunately he deleted some of his comments, so I will answer this questions by myself.
To clarify, I know this is for sure not the best way to do it, but Hell Yeah, it works!
Solution:
Hidden input fields did it for me. I added one hidden input field in each form:
Form 1:
<form action="search.php" method="get" class="navbar-form" role="search">
<div class="input-group">
<input type="hidden" name="page" value="movies">
<input type="text" class="form-control" placeholder="Search" name="query">
<div class="input-group-btn">
<button class="btn btn-default c-nav-btn-height" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
Form 2:
<form class="btn-group-vertical" action="search.php" method="get">
<button id="bMovies" class="btn active-menu" type="submit" name="page" value="movies">Filme</button>
<button id="bSeries" class="btn" type="submit" name="page" value="series">Serien</button>
<input id="hiddenField" type="hidden" name="query" value="">
</form>
So I added a hidden input field named "page" in the first form and a hidden input field named "query" in the second form. With this little trick im able to get the same information in both forms, but only the fields I want to show are shown.
Since the Form 1 is submitted first, it is possible to copy the value of the visible input field into the invisible input field in Form 2. All i need is some extra PHP and JavaScript.
$query = $_GET["query"];
$page = $_GET["page"];
if ($page == "movies" || $page == "series") {
echo "<script>";
echo 'document.getElementById("hiddenField").value = "'. $query .'" ;';
echo "</script>";
}
else {
}
I store the two parameters from the first Form into variables and check if $page is 'movies' or 'series', because only these two options are allowed on this site. If it is true, it echos some JavaScript, which is adding the $query variable as the value of the hidden input field in the second form.
And again, i know there will be better ways to do it, but it worked for me and i wanted to share it with you.

well, not natively, and if you can avoid it, you should. if you can combine both forms on your page, do that. if.. for some reason you really can't, i suggest you have a look at this thread.. it has a javascript function to do what you are looking to accomplish
jQuery - submit multiple forms through single reqest, without Ajax

Related

how can I get the value of a input field using PHP if there are two forms on the page both of POST method

I want to get the value of an <input> field using PHP.
There are two forms on my page both of POST method.
<form method="POST">
<input type="text" name="first">
<input type="submit" name="submit" value="submit">
</form>
<form method="POST">
<input type="text" name="second">
<input type="submit" name="submit1" value="Post">
</form>
How do I get the value of the second input field? Even though if I use $_POST['second'] it shows me an error:
Undefined index: 'second'
The W3C specs define that an input can be associated to only one form. It is a sign of bad design when you need to have multiple forms and the backend has to know which data is present in other forms.
The <form> element can contain arbitrary element structures like tables, it can even contain the entire document body content. You should hardly need multiple forms.
A common use-case is to have multiple submit buttons having the same name instead. Only the pressed button will be part of the form data.
<form method="post">
<input type="text" name="text_input">
<button type="submit" name="action" value="add">submit</button>
<button type="submit" name="action" value="update">submit</button>
<button type="submit" name="action" value="delete">submit</button>
</form>
Again, do not do that, however, if you really want to share a field across multiple forms for some reason, this can only be done by javascript intercepting the form submit event. This will not work when scripts are disabled by the user.
document.querySelectorAll('form').forEach(e => {
e.addEventListener('submit', function() {
document.querySelectorAll('.multi-form-input').forEach(e => e.setAttribute('form', this.id));
})
})
<input class="multi-form-input" name="common_input" type="text">
<form id="form-1" method="post">
<button type="submit" name="action" value="1">submit</button>
</form>
<form id="form-2" method="post">
<button type="submit" name="action" value="2">submit</button>
</form>

PHP Form not submitting on specific pages, same code

I have the following Form code:
<form method=post action='/zoeken.php'>
<div style='position: relative;'>
<input type=search name=zoeken_opdracht placeholder='Zoeken...' value="<?php echo $_POST['zoeken_opdracht']; ?>">
<button name=zoeken><i class='fa fa-search'></i></button>
</div>
</form>
However, when I am submitting in on specific pages, it isn't submitting. The same code however works fine on all other pages (No product pages).
See examples of productpage here:
Productpage (Form not working)
You can find the other forms on any other page but the productpages. For instance the homepage.
Change button with submit
Careful that there are errors in the type search, in the form method, in some name and style
<form method="post" action="/zoeken.php">
<div style="position:relative;">
<input type="search" name="zoeken_opdracht" placeholder="Zoeken..." value="<?php echo $_POST['zoeken_opdracht']; ?>">
<input type="submit" name="zoeken"><i class="fa fa-search"></i>
</div>
</form>
<form method=post action='/zoeken.php'>
<div style='position: relative;'>
<input type="search" name="zoeken_opdracht" placeholder='Zoeken...' value="<?= $_POST['zoeken_opdracht']; ?>">
<button type="submit" name=zoeken><i class='fa fa-search'></i></button>
</div>
</form>
The problem is solved. I figured out that there was a preventDefault in my javascript code that was also triggered on the search form submission. I fixed this and now it works like a charm.
Thanks for the

html - how to give a submit button a variable for GET?

i have the following button:
<form method="GET" action="/cms/deleteBlog/">
<input class="btn btn-danger btn-small" type="submit" name="'.$blogID.'" value="Delete">
</form>
So right now i get the following url:
cms/deleteBlog/?1=Delete
obviously i want something like the following:
cms/deleteBlog/id=1
So i tried the following (which is obviously not working):
name="id='.$blogID.'"
So how do i solve this? i've been looking around on the internet, and i know the solution is quite easy. But i just can't remember the way how it is done!
Add a hidden form field.
<input type="hidden" name="id" value="1" />
Also, you should never use GET to delete or modify data. Read Why should you delete using an HTTP POST or DELETE, rather than GET? for insight.
Why not use a hidden input field, e.g:
<form method="GET" action="/cms/deleteBlog/">
<input type="hidden" name="id" value="'.$blogID.'">
<input class="btn btn-danger btn-small" type="submit" value="Delete">
</form>
Or, if you want pretty URLs:
<form method="GET" action="/cms/deleteBlog/id='.$blogID.'">
<input class="btn btn-danger btn-small" type="submit" value="Delete">
</form>

PHP How get at values for a list of buttons

I want a list of buttons in a form and on postback I'd like to interrogate the list to see what status the buttons are at (by background colour if you are interested.)
The following codes says that Buttonx is undefined
However using the text boxes it works fine.
In javascript I can get at the array of buttons - at least in my real program.
If this is not possible, and an explanation would be useful, does anyone have a workaround at how I can get at the buttons in my postback. (In the real code the buttons are dynamically created depending on an sql query list)
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
var_dump($_POST['Buttonx']);
}
?>
<form name="RegisterStudents" action="" method="post">
<button type="submit" name="myButton">Submit</button>
<!--
<input type="text" name="Buttonx[]" id="B0" value="0" />
<input type="text" name="Buttonx[]" id="B1" value="1" />
<input type="text" name="Buttonx[]" id="B2" value="2" />
<input type="text" name="Buttonx[]" id="B3" value="3" />
-->
<button type="button" name="Buttonx[]" id="B0" >0</button>
<button type="button" name="Buttonx[]" id="B1" >1</button>
<button type="button" name="Buttonx[]" id="B2" >2</button>
<button type="button" name="Buttonx[]" id="B3" >3</button>
</form>
Thanks
Gordon
Using forms you cannot pass the form elements with type="button" to server their values are not passed to server instead you have to use any other type to send the information to server
on client side you can access their values this is the default nature of html
You cannot pass the type="button" to the server, instead use checkboxes?
You have the id="B0" or so but instead it should be value="B0" and not "id"
The <button> is not really mature in browsers yet. Look at this link: http://www.w3schools.com/tags/tag%5Fbutton.asp
If you use the <button> element in an HTML form, different browsers may
submit different values. Use <input> to create buttons in an HTML form.

PHP: Getting the value of a radio button and storing it into a database

I have never really used radio buttons before. So what I am trying to achieve is, using two radio buttons 'Public' and 'Private'. This is for the user's profile. My HTML code for the buttons is;
<div class="btn-group" data-toggle="buttons-radio">
<button type="button" class="btn btn-primary" id="privacy" name="privacy" value="0">Private</button>
<button type="button" class="btn btn-primary" id="privacy" name="privacy" value="1">Public</button>
</div>
As for the PHP, I'm not sure how to get the value and store it. Any help is appreciated!
Use a form.
<form method="get" action="yourscript.php">
<input type="radio" id="private" name="privacy" value="0">
<label for="private">Private</label>
<input type="radio" id="public" name="privacy" value="1">
<label for="public">Public</label>
<input type="submit" value="Save">
</form>
Change your buttons to input elements, which, together with the form, provide a HTML native way to send data to a script without the need for Javascript (although you can use Javascript to enhance the usability later on).
Using the label tag, you can assign a text to a radiobutton. This allows this text to be clicked as well, and it also provides better support for screen readers, because they can actually see which text belongs to the radiobutton.
An id needs to be unique, so I changed it to private and public. The ids are also used to link the label to.
The name determines by which name the value is sent to your script.
In PHP, you can use the superglobal $_GET. $_GET['privacy'] will contain '0' or '1' depending on the choice made. If the method of the form was post, you could use the superglobal $_POST instead, or you can use $_REQUEST, which contains values from either, so in that case your script doesn't care whether the values were send in the url (get) or in the chunk of invisible post data that is sent along with the request.
change type to radio and donot use the same id for morethan one element id is supposed to be unique
Also when you post the form type=button values will not be passed with the form
<div class="btn-group" data-toggle="buttons-radio">
<input type="radio" class="btn btn-primary" name="privacy" value="0"/>Private
<input type="radio" class="btn btn-primary" name="privacy" value="1"/>Public
</div>
assuming its posted on $_POST['privacy'] should contain either a 1 or a 0
also dont use an ID twice - they are supposed to be unique
You don't have form and submit button.
HTML page:
<div class="btn-group" data-toggle="buttons-radio ">
<form name="privacyForm" action="action.php" method="post">
<button type="radio" class="btn btn-primary" name="privacy" value="0">Private</button>
<button type="radio" class="btn btn-primary" name="privacy" value="1">Public</button>
<input type="submit" value="Submit">
</form>
</div>
action.php
<?php
echo $_POST['privacy']; // should be 0 or 1
?>
i have assumes that your form is something as follows.
<form name="sample" action="form_submit.php" method="POST">
<div class="btn-group" data-toggle="buttons-radio">
<input type="button" class="btn btn-primary" id="private" name="privacy" value="0">Private</input>
<label for="private">Private</label>
<input type="button" class="btn btn-primary" id="public" name="privacy" value="1">Public</input>
<label for="public">Public</label>
</div>
<input type="submit" name ="submit" value ="Submit"/>
</form>
you can use the following code inside the action (form_submit.php) to get the selected radio button value.
form_submit.php
<?php
$selected_radio = $_POST['privacy'];
print $selected_radio;
?>
make sure that you are not duplicating the IDs because they should be unique.

Categories