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.
Related
I just want to ask if how can I save only 1 checkbox if they have the same name given .
for example I have this checkbox and only one of them needs to save on my table
<input type="checkbox" name="loan" value="First Loan Application"> First Loan Application
<input type="checkbox" name="loan" value="Renewal"> Renewal
and on my table I have a field that is called loan
can I do just like this on my controller
function insert_data(){
$data = array(
'loan' => $this->input->post('loan'),
....
);
}
and on my view I have this
<form id="myForm" method="post" action="<?php echo base_url(); ?>main/insert_data">
<input type="checkbox" name="loan" value="First Loan Application"> First Loan Application
<input type="checkbox" name="loan" value="Renewal"> Renewal
</form>
Could someone help me if I am doing it correctly or I am missing something.
As M.Hemant said, you should use radio button, so users can choose only one of them.
Beside, you should design your database with tinyint type for that column. For example, store 0 for First Loan Application, 1 for Renewal.
Then, the database will use less space than varchar type, and easier indexing.
<input type="radio" name="loan" value="0"> First Loan Application
<input type="radio" name="loan" value="1"> Renewal
It is easy to use radio buttons when you want user to select only one choice but if you want to give checkboxes for multiple selection, here is an easy approach for this. In your html for input type checkbox change name attribute to an array like this.
<form method='post'>
<input type='checkbox' name='test[]' value='1'/>1
<input type='checkbox' name='test[]' value='2'/>2
<input type='checkbox' name='test[]' value='3'/>3
<button type='submit' name='sub'>sub</button>
</form>
Now in the php part of your file check for this test array using isset function
if(isset($_REQUEST['sub'])){
print_r($_REQUEST['test']);
}
Here when print_r statement is executed you can see the value that are checked in checkboxes.Now you can use implode function that will convert the array into the string which can be stored in database in a single column
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>
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.
I've got a bunch of <input type='text' name='input0' /> fields.
The user can add them dynamically so we'd get something like:
<input type='text' name='input0' />
<input type='text' name='input1' />
<input type='text' name='input2' />
...etc. We don't know how many there will be in total. When the form gets submitted, I want to loop through each of these input fields and assign them to a $_POST variable. These are NOT the only input fields in the form, there are other elements such as radio buttons, checkboxes and other text fields. My problem is I need to somehow identify these particular dynamically-generated text fields, and loop through them. How can I do this, when all I get on the server side are the names of the input fields?
Use:
<input type='text' name='input[]' />
then you can:
foreach($_POST['input'] as $input){
echo $input;
}
I want to submit Multiple Forms with 1 button and 1 action target using PHP. Is is possible ?
HTML
<form name="myform" action="test_post.php" method="post">
Name: <input type='text' name='name' />
</form>
<form name="myform2" action="test_post.php" method="post">
Class: <input type='text' name='class' />
</form>
Search
JS
function submitform()
{
document.myform.submit();
document.myform2.submit();
}
PHP (test_post.php)
echo $name = $_POST['name'];
echo $class = $_POST['class'];
I tried with that code but it just show $_POST['class'] value. For name it show error : Undefined index: name in...
Please advice.
you dont need one form per input, you can have a million in one one form so ..
<form name="myform" action="test_post.php" method="post">
Name: <input type='text' name='name' />
Class: <input type='text' name='class' />
</form>
Search
should be fine, and you don't really need js to submit. a html submit input is better supported
<input id="submit" type="submit" value="submit">
You actually want two fields on one form - which you can then submit with no problems:
<form name="myform" action="test_post.php" method="post">
Name: <input type='text' name='name' />
Class: <input type='text' name='class' />
<input type='submit'>
</form>
Or you can submit it by using some JS anyhow if you do other thigns in the JS code.
if jquery is an option, then .deferred might be what you are looking for.
function submitform(){
//define a variable where we will store deferred objects
var def = true;
$("form").each(function() {
var postResult = $.Deferred();
//.when takes a deferred object as a param.
// if we don't pass a deferred object .when treats it as resolved.
// as our initial value of def=true, the first .when starts immediately
$.when(def).then(function(){
$.post('post_destination.php', form.serialize()).done(function(){
//the chain will fail after the first failed post request
//if you want all the requests to complete in any case change the above .done to .always
post.resolve();
});
});
// now we reassign def with the deferred object for the next post request
def = postResult;
});
}
and here's the link to when I asked the question some time ago. How to submit multiple jquery posts to a page one after another if database updates successfully?