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
Related
<html>
<head>
<title>test radio button</title>
</head>
<body>
<form method="post" action="">
<input type="radio" name="row1"/>row1
<input type="radio" name="row2"/>row2
<input type="radio" name="row3"/>row3
<input type="submit" value="update"/>
</form>
</body>
</html>
I have a table called table_test. In this table there is a column named as
row1, row2 and row3.
I want to update value of each column in radio button at a time. I need to update one column when I submit the form.
I have searched so many times google but I can't find the solution. Please show me how I can implement this in PHP.
The name of the radio button has to be the same. You use a 'value' attribute to identify the value on the server.
<input type="radio" value="row1" name="myRadio"/>row1
<input type="radio" value="row2" name="myRadio"/>row2
<input type="radio" value="row3" name="myRadio"/>row3
Now, you can get the value in PHP:
echo $_POST['myRadio'];
To use this value in a query, you could do this:
mysql_query(sprintf("INSERT INTO myTable SET %s = 'myValue'", $_POST['myRadio']));
Be warned! Use something like PDO if you don't want your server to get hacked.
Is it possible to submit forms with input checkboxes, each containing the same name, to a PHP script?
Is it possible to loop through the names to get all the values?
I am building a message system, and users can add/remove recipients dynamically. When they do, a hidden checkbox is generated in the form containing the value, yet I'm not sure what to do with the name. On the php end, on top of the recipients a subject and a message are submitted, and the script needs to loop through each name and perform various SQL tasks. I know there are much better ways of doing this, and feel free to suggest, but I'd really like to know if it can get done this way. Comment if you need to see code, but I warn you, it's really confusing.
<input type="checkbox" name="samename[]">
// on the post/get:
foreach( $_POST['samename'] as $eachId ){
// do whatever you want. build the where in a query, ' set = '.$eachId
}
Yes you can, use the same name with [] after it, it will cause all of the values to be stored in an array on PHP.
<input type=checkbox value=1 name=check[]>
<input type=checkbox value=2 name=check[]>
<input type=checkbox value=3 name=check[]>
<input type=checkbox value=4 name=check[]>
<input type=checkbox value=5 name=check[]>
Yes you can, array of post, look at this example:
<?php
print_r($_POST);
?>
<form action="form.php" method="POST">
<input type="checkbox" name="vehicle[]" value="Bike" /> I have a bike<br />
<input type="checkbox" name="vehicle[]" value="Car" /> I have a car
<input type="submit" value="Submit" />
</form>
Notice how vehicle has the square brackets?
Im trying to create a form using PHP and I cant seem to find a tutorial on what I need so thought Id ask on here.
I have a multiple checkbox option on my page...
<li>
<label>What service are you enquiring about?</label>
<input type="checkbox" value="Static guarding" name="service">Static guarding<br>
<input type="checkbox" value="Mobile Patrols" name="service">Mobile Patrols<br>
<input type="checkbox" value="Alarm response escorting" name="service">Alarm response escorting<br>
<input type="checkbox" value="Alarm response/ Keyholding" name="service">Alarm response/ Keyholding<br>
<input type="checkbox" value="Other" name="service">Other<input type="hidden" value="Other" name="service"></span>
</li>
I'm not sure however how to collect all checkbox values using POST method?
if i use
$service = $_POST['service'];
I only get 'other' returned
Name the fields like service[] instead of service, then you'll be able to access it as array. After that, you can apply regular functions to arrays:
Check if a certain value was selected:
if (in_array("Other", $_POST['service'])) { /* Other was selected */}
Get a single newline-separated string with all selected options:
echo implode("\n", $_POST['service']);
Loop through all selected checkboxes:
foreach ($_POST['service'] as $service) {
echo "You selected: $service <br>";
}
Currently it's just catching your last hidden input. Why do you have that hidden input there at all? If you want to gather information if the "Other" box is checked, then you have to hide the
<input type="text" name="other" style="display:none;"/>
and you can show it with javascript when the "Other" box is checked. Something like that.
Just make the name attribute service[]
<li>
<label>What service are you enquiring about?</label>
<input type="checkbox" value="Static guarding" name="service[]">Static guarding<br />
<input type="checkbox" value="Mobile Patrols" name="service[]">Mobile Patrols<br />
<input type="checkbox" value="Alarm response escorting" name="service[]">Alarm response escorting<br />
<input type="checkbox" value="Alarm response/ Keyholding" name="service[]">Alarm response/ Keyholding<br />
<input type="checkbox" value="Other" name="service[]">Other</span>
</li>
Then in your PHP you can access it like so
$service = $_POST['service'];
echo $service[0]; // Output will be the value of the first selected checkbox
echo $service[1]; // Output will be the value of the second selected checkbox
print_r($service); //Output will be an array of values of the selected checkboxes
etc...
<input type="checkbox" value="Other" name="service">Other<input type="hidden" value="Other" name="service"></span>
You've got a hidden input field with the same name as the checkbox. "later" fields with the same name as an earlier one will overwrite the previous field's values. This means that your form, as posted above, will ALWAYS submit service=Other.
Given the phrasing of your question in the html, it sounds more like you'd want a radio button, which allows only ONE of a group of same-name fields to be selected. Checkboxes are an "AND" situation, radio buttons correspond to "OR"
I wanted users to filter searches by categories
I have 3 PHP files. One named searchbycity.php, searchbystate.php, and the default search.php
My question is, how would I set it up so I could click on a radio button and the search bar
would know which php file to search for with that info?
Here's how I have the whole radio button thing laid out so far
<input type='text' size='70' name='search'>
<input type='image' value='search' src='images/tickmark.png'></a><br>
Search by
<input type="radio" onclick="eng = this.value;" checked name="sengines"
value="http://www.google.com/search?q=" />
City
<input type="radio" onclick="eng = this.value;" name="sengines"
value="http://www.altavista.com/web/results?q=" />State
(Ignore the Google search and the AltaVista search, I got it from a website:P)
You could write this html code (note the substitution of radio strings values with integers):
<form action="search.php" method="GET">
<input type='text' size='70' name='search'>
<input type='image' value='search' src='images/tickmark.png'></a><br>
Search by
<input type="radio" onclick="eng = this.value;" checked name="sengines"
value="1" />
City
<input type="radio" onclick="eng = this.value;" name="sengines"
value="2" />State
<input type="submit" />
</form>
When the user push the send button the search.php page will be execute (server side). This page may contains the following code:
<?php
if(is_integer($_GET['sengines']) && is_string($_GET['search'])){
switch($_GET['sengines']){
case 1: include_once "searchbycity.php";
searchByCity($_GET['search']);
break;
case 2: include_once "searchbystate.php";
searchByState($_GET['search']);
break;
}
}
?>
So, if the user selected the first radio button the searchbycity.php file and the hypothetical function called searchByCity present in the file will be called passing the $_GET['search'] value submitted by form. Else, will be included the searchbystate.php file and... the logic will be analogue.
Pay attention: the data sended by form must be sanitized by using the filter functions. In the code there is a first level of checking by using is_integer and is_string functions.
I need help ... that too from scratch as now am learning php. just variable declaration i am learning.
I have created a form with 5 check boxes. when i select any 1 or any 2, 3.. or any combination, i should get the data which is already stored for that option in MySQL database.
My form is this:
<form method="post" action="search.php" name="search_form" onsubmit="return checkCheckBoxes(this);">
<input type="checkbox" name="search1" value="qwerty_keypad" id="search1">QWERTY Keypad
<input type="checkbox" name="search2" value="touch_screen" id="search2"> Touch Screen
<input type="checkbox" name="search3" value="usb" id="search3"> SUB Drive
<input type="checkbox" name="search4" value="mobile_tracker" id="search4">Mobile Tracker
<input type="checkbox" name="search5" value="Backup" id="search5">Phone backup on MMC
<input type="submit" value="Search" /> </form>
what i should write in search.php.
Please help me ... please
Thanks in advance
Use an array to submit the values
<input type="checkbox" name="search[connectivity]" value="usb" id="search3"> USB
<input type="checkbox" name="search[display]" value="touchscreen" id="search4">Touchscreen
Afterwards you build your query based on those values:
foreach($_POST['search'] as $k=> $search){
$where[]= $k." = '".mysql_real_escape_string($search)."'";
}
$query = "Select * from table where ".implode(' AND ',$where);