<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.
Related
I'm currently working on at the displaying of information from a database. I was making a summary site where you can only see the important things of a table. After that i made the first element as an <input type="submit"> in a <form>, so u can click it and come to the detail site. My problem is now: The value of this input type has to be my ID, so i can query correctly on me detail site. I was wondering if it is possible to use something like a placeholder, so that the ID is the value, but on the input type is written other text.
My input:
<form method="post" action="Details.php">
<input type="submit" placeholder = "test" name="Overview" onclick="parent.location='Details.php'" value="<?php echo $data[$i1]; ?>">
</form>
How it currently looks
I want it that the input type still has the same value, but is displaying on the website something else like "test".
Greetings!
No, but buttons can have different values and labels.
<button name="foo" value="bar">baz</button>
Since you are using a form-tag per row, you can add a hidden input-field in the form and set the value of the submit-button to whatever you like.
<form method="post" action="Details.php">
<input type="hidden" name="id" value="<?php echo $data[$i1]; ?>" />
<input type="submit" name="Overview" value="test" />
</form>
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'm new to MySQL and as a learning project I'd like to make a recipe database. I'd like to the user to be able to enter ingredients through a simple HTML form but I'm stuck in how to label the form so that I can enter several ingredients into the database at once.
I'd like to do something like this:
<form method="post" action="insert.php">
Ingredient 1: <input type="text" name="ingredient"><br />
Ingredient 2: <input type="text" name="ingredient"><br />
Ingredient 3: <input type="text" name="ingredient"><br />
<input type="submit" value="Submit">
</form>
When I do this, I add rows to the table but they're all empty. I know it's got something to do with me using "ingredient" (the table value where I want to add the ingredient name) several times in the form, but I just don't know how to solve it.
I would absolutely love some input on how to make it work.
write it like
Ingredient 1: <input type="text" name="ingredient[]"><br />
Ingredient 2: <input type="text" name="ingredient[]"><br />
and when you will get the REQUEST array in php, you will actually
get an array of names
like
$ing = $_POST['ingredient']; // $ing will be indexed array
You can't use same name for multiple textboxes like you have done. The last input box's value will overwrite all of the other ones' since they have the same name. Either you have to use different names for each input texts or define name as array like :
Ingredient 1: <input type="text" name="ingredient[]"><br />
Ingredient 2: <input type="text" name="ingredient[]"><br />
Ingredient 3: <input type="text" name="ingredient[]"><br />
So $_REQUEST['ingredient'] or $_POST['ingredient'] will be a normal PHP array from which you can get the value of each textbox like $_REQUEST['ingredient'][x] where x is some integer index, which is valid as long as count($_REQUEST['addCart']) > x.
Working Code :
index.html
<!DOCTYPE html>
<html>
<head>
<title> Recipes </title>
</head>
<body>
<form method="post" action="register.php">
Ingredient 1 : <input type="text" name="ing1"/><br/>
Ingredient 2 : <input type="text" name="ing2"/><br/>
Ingredient 3 : <input type="text" name="ing3"/><br/><br/>
<input type="submit" value="Enter" name="submit"/>
</form>
</body>
</html>
register.php
<?php
// coding to check database connection
$connection = mysqli_connect('localhost','root','adm','recipe');
/*
root - username
adm - passowrd
recipe - database name
*/
//checking whether submit button is clicked or not
if(isset($_POST['submit']))
{
// Escaping special char & getting the values we entered in form through POST method
$ing1 = mysqli_real_escape_string($connection,$_POST['ing1']);
$ing2 = mysqli_real_escape_string($connection,$_POST['ing2']);
$ing3 = mysqli_real_escape_string($connection,$_POST['ing3']);
//data is table name
$query = "INSERT into data VALUES('','$ing1','$ing2','$ing3')";
$result = mysqli_query($connection,$query);
echo "<p>Successfully Entered</p>";
?>
Click here to go to home
<?
}
?>
Create a database and name it as recipe and a table as data. Table data structure:
Output :
This question will show what a newbie I am. The situation is this. It's a photo contest.
People upload a photo and to the right on the same line is a checkbox.
Voters check the box if they like the photo. They can select up to, say, 5 photos.
Keeping it simple, my problem isn't with MySQL, but with the form. Each row has a checkbox, which is a form. The SUBMIT button is the problem. The only way I can figure out to have submit work is by putting a submit button with each checkbox. Of course, that's ridiculous. What I want is to read all the checked boxes and have ONE submit button when the voter is finished. Spent hours on this and can't see how to have the SUBMIT by itself that the voter can click and have all the checked values inserted into the database a one time.
Any notions? I know this sound very primitive, but just getting into this.
Thanks ahead of time for any help
You can enclose all the checkboxes in a single html form tag and have the submit button inside it. It'll automatically submit data in all checkboxes
Or
You can use javascript/jQuery to run through all the checkboxes. And submit the form.
Well, there are all sorts of javascript ways to go out and grab the data you want and submit it, but let's keep it simple.
[voteform.html]
<form action="process_script.php">
<img src="image1"><input type="checkbox" name="vote_for_image[]" value="1">
<img src="image2"><input type="checkbox" name="vote_for_image[]" value="2">
<input type="submit">
</form>
... then ... (please note the [RETURN ERROR...] and [DATABASE...] blocks are just place holders for you to fill in code)
[process_script.php]
<?
if (count($_GET['vote_for_image'] > 5) {
[RETURN ERROR 'Please select no more than 5 images to vote for']
}
else {
foreach ($_GET['vote_for_image'] as $index => $image_id) {
[DATABASE INSERT OR UPDATE FOR $image_id]
}
}
?>
Set the checkbox[] as name attribute for each checkbox and on submission you can address the valiues as arrays
<body>
<form action="checkbox.php" method="post">
<input type="checkbox" name="checkbox[]" value="a">
<input type="checkbox" name="checkbox[]" value="b">
<input type="checkbox" name="checkbox[]" value="c">
<input type="checkbox" name="checkbox[]" value="d">
<input type="submit" name="Submit" value="Submit">
</form>
<?php
if(isset($_POST['Submit']))
{
echo $_POST['checkbox'];
}
?>
</body>
the above will give you
Array (
[0] => a
[1]=>b
[2] => c
[3]=>d
)
Try this code
//HTML
<form method='post'>
<input type='checkbox' name='photo[]' value='1' />
<img src='test1.jpg' />
<input type='checkbox' name='photo[]' value='2'/>
<img src='test2.jpg' />
<input type='submit' value="Submit" name="submit" />
</form>
//PHP
if(isset($_POST['submit']))
{
if(isset($_POST['photo']))
{
if(count($_POST['photo']) > 5)
{
// Display error msg
}
else
{
// Contains all ids voted on
$img_ids=explode(',',$_POST['photo']);
// insert or update DB for the image ids
}
}
}
This question is similar to my previous question but not the same ... please check out....I am using totaly 3 webpages; form elements are distributed among two pages, "eg1.html" and "eg2.html", but all the form elements should be submitted to "eg.php".
Here is the code for eg.php which accepts the form elements from both eg1.html and eg2.html:
$size=$_POST["fontsize"];
$label=$_POST["label"];
$age=$_POST["age"];
$sex =$_POST["sex"];
code for eg1.html
<html>
<body>
<form action="eg.php" method="post">
<input type="radio" name="fontsize" value="3"/>
click here to select other options which includes age and sex
<input type="radio" name="label" value="stacks"/>
<input type="submit" name = "down" value = "down">
</form>
</body>
Now What would be the code for eg2.html? just check out sample partial html code :but needs to be compleated....
<input type="radio" name="age" value="3"/>
<input type="radio" name="sex" value="female"/>
The code should work exactly like this:
First user will open eg1.php he selects only one option that is "fontsize" .. next he clicks on the "link to eg2.html" to select two more options "age" and "sex" after selecting... he will be redirected back to eg1.php where he has to select one more option that is "label" ... then he will submit the form to eg.php. Which will hold all form elements those are 'fontsize' 'age' 'sex' and 'label' .....
I have seen many website using this technique please check out cooltext.com where user will get an option to click on the font image which will redirect him to fonts page after selecting one of the fonts images he will be redirected back to homepage,where he can select some other form elements or form elements and finally submits the form .... i have also seen many websites using this technique , i think this can be done using JQUERY/JavaScript but not sure ...please help me to fix this problem guyz,.,,,
Using js you can have the entire form on one page and divide it in steps like this
<form action="eg.php" method="post">
<div class="step1">
<input type="radio" name="fontsize" value="3"/>
click here to select other options which includes age and sex
<input type="radio" name="label" value="stacks"/>
<input type="submit" name = "down" value = "down">
</div>
<div class="step2">
click here to go back to step1
<input type="radio" name="age" value="3"/>
<input type="radio" name="sex" value="female"/>
<input type="submit" value="Submit"/>
</div>
</form>
js:
$('#step1_submit').click(function(){
$('#step1').hide();
$('#step2').show();
});
$('#step2_back').click(function(){
$('#step1').show();
$('#step2').hide();
});