When I click submit button of form I get two variables with same name in url. Why this happens.
The form is following
<select name='name1' form='select_form'>...</select>
<select name='name2' form='select_form'>...</select>
<form id='select_form' action='index.php' method='get'>
<input type='text' name='date_start' value='val1' id='datepicker1'>
<input type='text' name='date_end' value='val2' id='datepicker2'>
<button id='submit' type='submit' value='Submit'>Select</button>
</form>
When I click submit button I can see the following url
index.php?name1=val_name1&name2=val_name2&date_start=2016-08-01+00%3A00%3A00&date_start=&date_end=2016-08-03+00%3A00%3A00&date_end=2016-08-03+00%3A00%3A00
As you can see there are two date_start variables. What is the reason?
This has very bad impact, because when I change only one value (for example only date_start) then after clicking submit I have the following
index.php?name1=val_name1&name2=val_name2&date_start=2016-08-01+00%3A00%3A00&date_start=&date_end=2016-08-03+00%3A00%3A00&date_end=
So second value of date_end is empty.
Related
I made a html and a php page. I put 3 images in html page showing different genre of movies- horror, fantasy, romance. I want the images to work as form submit button and should get redirected to the php page and the php page should get the genre of the image.
What I already tried- I tried lot of different things but nothing worked.
And I wonder how php page will take different inputs from different images using $_POST method.
Expected output-
Suppose if user clicked on image of genre 'horror', then in php page value of $genre should be Horror.
You can use an <img> tag inside a <button> element.
<form method="post">
<button name="genre" value="horror" type="submit"><img src="./img/horror.jpg"></button>
<button name="genre" value="comedy" type="submit"><img src="./img/comedy.jpg"></button>
</form>
You can access the value of the submit button in your PHP using $_POST['genre'] (or whatever the name attribute of your buttons is)
The images should have an href tag that redirects to your PHP page. Note that this method will mean that you should get the value through $_REQUEST variables and not POST or GET. For example lets say href='myphp.php?genre=horror'. and in the PHP file $genre = $_REQUEST['genre'];
You could try something similar to the following:
<input class='genre' type='image' src='/images/genres/horror.jpg' data-genre='horror' />
<input class='genre' type='image' src='/images/genres/sci-fi.jpg' data-genre='sci-fi' />
<input class='genre' type='image' src='/images/genres/chickflick.jpg' data-genre='chickflick' />
<script>
Array.prototype.slice.call( document.querySelectorAll('input.genre') ).forEach( function(input){
input.addEventListener('click', function(e){
e.preventDefault();
location.href='info.php?genre='+this.dataset.genre
});
})
</script>
Assign each input a dataset attribute which you query later in the click handler. That dataset value is then used to construct the url...
Alternatively a slightly different approach would be to POST the data by setting the value of a hidden field to the dataset attribute value- like:
<form name='genres' method='post'>
<input class='genre' type='image' src='/images/genres/horror.jpg' data-genre='horror' />
<input class='genre' type='image' src='/images/genres/sci-fi.jpg' data-genre='sci-fi' />
<input class='genre' type='image' src='/images/genres/chickflick.jpg' data-genre='chickflick' />
<input class='genre' type='image' src='/images/genres/thriller.jpg' data-genre='thriller' />
<input class='genre' type='image' src='/images/genres/adventure.jpg' data-genre='adventure' />
<input class='genre' type='image' src='/images/genres/period-drama.jpg' data-genre='period-drama' />
<input type='hidden' name='genre' />
</form>
<script>
let form=document.forms.genres;
let genre=form.genre;
Array.prototype.slice.call( document.querySelectorAll('input.genre') ).forEach( function(input){
input.addEventListener('click', function(e){
e.preventDefault();
/* set hidden input value */
genre.value=this.dataset.genre;
/* append the genre to the qction/querystring */
form.action='?genre='+this.dataset.genre;
/* go */
form.submit();
});
})
</script>
I want to pass the value from this page to next page pay.php file below is my form (index.php )
<form action=\"/modules/gateways/pay.php\" method=\"post\" id=\"checkout[id]\">
<input type=\"hidden\" name=\"user\" value=\"[userid]\">
<input type=\"hidden\" name=\"decription\" value=\"[itemname]\">
<input type=\"hidden\" name=\"amount\" id=\"amount[id]\" value=\"[price]\">
<input type=\"hidden\" name=\"type\" value=\"deposit\">
</form>
and in pay.php, I am trying to get the value with this code:
$m_amount = number_format($_POST['amount'], 2, ".", "");
$m_desc = strip_tags($_POST['decription']);
I am not getting values in pay.php.
You need to check your HTML.
<form action="/modules/gateways/pay.php" method="post" id="checkout[id]">
<input type="hidden" name="user" value="123">
<input type="hidden" name="decription" value="itemone">
<input type="hidden" name="amount" id="amount_id" value="538">
<input type="hidden" name="type" value="deposit">
</form>
and in pay.php try to receive to simply print POST variable
print_r($_POST);
Include JQuery or a similar JS framework or use native JS to submit this form.
Without a submit-button you have to submit this form by JS or a JS-framework.
In JQuery you have to write something similar to this example:
$('#checkout[FORMID]').submit();
This will submit the form. For sure you have to complete this but basicly this is the missing call.
I saw your form contains hidden inputs. If this form depends on another form/action or else you should either include the hidden form in the depoending form or submit it by itself using JS/JQuery/something similar.
<form action='login.php' method='post'>
<center> Username : <input type='text' name='username'> Password : <input type='password' name='pass'> <input type='submit' value='Login'></center>
</form>
<input type='submit' value='Registration'>
I wondering if it is possible to have my Registration button next to the login without having anything to do with the login.php. If I move it inside of the form then it's going to the login.php and not the reg.php.
Do not use submit button in anchor(<a></a>) tag
Just provide a text in it
Registration
If you want a button in it
Try to use type button not submit (Untested from me)
<input type='button' value='Registration'>
If you can - you may consider changing the form action to some "formHandler.php", giving both of the submits the same name, and testing the inputs value (each one has to be different) in the formHanlder.php
$real_action=$_REQUEST['submit_input_name'];
switch ($real_action){
case 'register':.....
case 'login':.......
}
Is there a way to get only a certain set of information from a form? Eg
<form>
<input name='I want this' value='one' type=text />
<input name='and this' value='one' type=text />
<input name='but not this' value='one' type=text />
</form>
Where, obviously, i only want the first two fields but not the third one? I've got a user inventory on my website that looks like this:
<form action="" method="POST">
<input name='item_id' value='1' type='hidden'>
<input type='button' name='slot1' value='1'>
<input type='button' name='slot2' value='2'>
<input name='item_id' value='2' type='hidden'>
<input type='button' name='slot1' value='1'>
<input type='button' name='slot2' value='2'>
</form>
I want the users to be able to select, item 1 and equip it to slot 1 but The only way i can think of doing this right now is to have them all be separate forms. and i feel like that would be bad coding.
Yes, using jquery, select only the first element and second elements value, post it using ajax, and retrieve and process data server side.
var i1 = $('form').eq(0).find('[name="item_id"]').val() //Values from first form only
var i2 = $('form').eq(0).find('[name="slot1"]').val()
$.ajax({
url: "test.php",
data: {i1:i1, i2:i2}, //Send this to php file.
}).done(function() {
$(this).addClass("done");
});
When you submit a form the values of all inputs associated with that form will be added to the $_POST (array) variable. You can always choose to ignore values when certain conditions apply. If that's not an option, I think you should opt for separate forms.
Another thing you could do—I do not understand the context of your problem, so I'm not sure if it applies to your situation—is have a user choose between "Item 1" and "Item 2" via radio buttons in your form. You can then base your form handling logic on the choice people made in the form.
The case :
<form id='frm' name ='frm' action='test.php'>
<div style='display:none'>
<input type='text' name='name' value ='' />
</div>
<input type='submit' value='Submit' />
</form>
For example , How can i submit the from given above with its inputs ?
Issue : The "name" input wont be passed !
Programatically, you can just trigger the submit event on the form element:
$('#frm').submit();
Edit: Actually, after reading your markup more carefully, you are using an input named name, this element can cause "clashing" problems with the form's name attribute.
Consider changing the name of your input.
See also:
Unsafe Names for HTML Form Controls
As a user, you'd just click the submit button. The visibility of a form element doesn't change the fact that it gets submitted with the form.
Programmatically:
document.getElementById('frm').submit();
If you're not trying to show the input, why not use type="hidden" and dispense with the style?
document.getElementById('frm').submit();
Here is how to submit hidden variables in a FORM:
<input type='hidden' name='name' value ='' />