PHP: Submit checkbox to database - php

I am having trouble submitted checkbox values and details to my database.
This is my HTML:
<form method="get" action="check.php">
<input type="checkbox" name="checkAccount"/>
<input type="hidden" name="table_name" value="masterChart" />
<input type="hidden" name="column_name" value="account" />
<p><a href='check.php'><input type="submit" class="btn btn-primary" value="Submit" /></a></p>
</form>
This is the check.php:
$table = $_GET['table_name'];
$column = $_GET['account'];
$dbc = mysqli_connect('localhost', 'root', 'root', 'database') or die('Connection error!');
if ($value = 1) {
$checkbox = "INSERT INTO login_table_display(`user`, `table`, `column`, `value`) VALUES(`:user`, '$table', '$column', `$value`)";
mysqli_query($dbc, $checkbox) or die('Database error, check!');
}
header('location:index.php');
As you can see above, I used variables to get other details for that checkbox to insert into the table as well.
After I press submit if the checkbox is checked, this is what's seen in the url:
http://localhost/admin/check.php?checkAccount=on&table_name=masterChart&column_name=account
Any suggestions or help will be appreciated!

The classic way to submit data is to add the value attribute to your checkboxes element in your form. On server side you have to ckeck the value for "null".
<input type="checkbox" name="checkAccount" value="putyourvaluehere"/>

Your Html is not ok
It should be
<form method="get" action="check.php">
<input type="checkbox" name="checkAccount"/>
<input type="hidden" name="table_name" value="masterChart" />
<input type="hidden" name="column_name" value="account" />
<p><input type="submit" class="btn btn-primary" value="Submit" /></p>
</form>
Also
if(isset($_POST['checkAccount']) {
Should Be
if( isset($_POST['checkAccount']) ) {

Checkbox value will be submitted only when it's checked. Use isset($_GET['checkAccount']) for this:
$var= isset($_GET['checkAccount']) ? 1 : 0; // Or whatever values you use in DB

Try this:
First you have to edit your html code as below;
<form method="get" action="check.php">
<input type="checkbox" name="checkAccount" value='cool'/>
<input type="hidden" name="table_name" value="masterChart" />
<input type="hidden" name="column_name" value="account" />
<p><input type="submit" class="btn btn-primary" value="Submit" /></p>
</form>
you are not giving value to check box and using submit button inside a tag, it's not good practice.

Replace:
if(isset($_POST['checkAccount'])
to:
if(isset($_GET['checkAccount'])

// your html code shoul be like this
<form method="get" action="check.php">
<input type="checkbox" name="checkAccount"/>
<input type="hidden" name="table_name" value="masterChart" />
<input type="hidden" name="column_name" value="account" />
<p><input type="submit" class="btn btn-primary" value="Submit" /></p>
</form>
<?php
$table = $_GET['table_name'];
$column = $_GET['account'];
$value = isset($_GET['checkAccount']) ? 1 : 0;
$dbc = mysqli_connect('localhost', 'root', 'root', 'database') or die('Connection error!');
if ($value == 1) {
$checkbox = "INSERT INTO login_table_display('user', 'table', 'column', 'value') VALUES(':user', '$table', '$column', '$value')";
mysqli_query($dbc, $checkbox) or die('Database error, check!');
}
header('location:index.php');
?>

Related

dynamically get data based on different button submission

I have 3 buttons to submit. let's say button A, B, C.
Each button press gonna bring data from DB based on the button press. But the template of data is the same.
my question is it possible to dynamically change the query. especially WHERE field in the query.
$sql = "SELECT Name FROM DevicesList WHERE Device='A' ";
What I want is WHERE part should change based on Button press.
//my forms are as follows
<div class="button_style1">
<form action="displayData.php" method="get" target="_blank">
<input type="submit" id ="mybutton1" value="A" />
</form>
</div>
<div class="button_style2">
<form action="displayData.php" method="get" target="_blank">
<input type="submit" id ="mybutton2" value="B" />
</form>
</div>
<div class="button_style3">
<form action="displayData.php" method="get" target="_blank">
<input type="submit" id ="mybutton3" value="C" />
</form>
</div>
//I am trying to avoid creating different pages for each button press. Just one page (displayData.php) but with different data based on button press.
Yes, definitely you can do it.
Give same name to each elements:
<input type="submit" name="submit" id ="mybutton1" value="A" />
And in the posted form, get which button is submitted:
if (isset($_POST['submit')) {
if ($_POST['submit']) {
$var = mysqli_real_escape_string($_POST['submit']);
$sql = "SELECT Name FROM DevicesList WHERE Device='" . $var . "'";
}
}
At one time, only one submit button will submit.
Therefore, you will every time get the name of the submit button and that is what you are comparing in SQL.
You can use hidden fields to pass some datas.
In example :
<div class="button_style1">
<form action="displayData.php" method="get" target="_blank">
<input type="hidden" name="MyHiddenData" value="A">
<input type="submit" id ="mybutton1" value="A" />
</form>
</div>
<div class="button_style2">
<form action="displayData.php" method="get" target="_blank">
<input type="hidden" name="MyHiddenData" value="B">
<input type="submit" id ="mybutton2" value="B" />
</form>
</div>
<div class="button_style3">
<form action="displayData.php" method="get" target="_blank">
<input type="hidden" name="MyHiddenData" value="C">
<input type="submit" id ="mybutton3" value="C" />
</form>
</div>
And then, in displayData.php
$MyDevice = $_GET['MyHiddenData'];
You can do this like below.
<div class="button_style1">
<form action="displayData.php" method="get" target="_blank">
<input type="submit" name="submit" id ="mybutton1" value="A" />
</form>
</div>
<div class="button_style2">
<form action="displayData.php" method="get" target="_blank">
<input type="submit" name="submit" id ="mybutton2" value="B" />
</form>
</div>
<div class="button_style3">
<form action="displayData.php" method="get" target="_blank">
<input type="submit" name="submit" id ="mybutton3" value="C" />
</form>
</div>
on the displayData.php
if (isset($_GET['submit']) && in_array($_GET['submit'], array('A', 'B', 'C'))) {
$variable = mysqli_real_escape_string($con, $_GET['submit']); //$con - database connection object
$sql = "SELECT Name FROM DevicesList WHERE Device='" + $variable + "' ";
}

How to record only int value from html(php) to mysql database table?

My question is: How to insert number(int) to countTable when buttons are click?
Here is my sample code, Thank you.
HTML
<form action="check.php" method="POST">
<button name="a">a</button>
<button name="b">b</button>
<button name="c">c</button>
</form>
check.php
require('connect.php');
$A = $_POST["a"];
$B = $_POST["b"];
$C = $_POST["c"];
$sql = "INSERT INTO countTable (numA,numB,numC)
VALUES(:numA,:numB,:numC)";
$pre = $conn->prepare($sql);
$pre->bindValue(':numA',$A);
$pre->bindValue(':numB',$B);
$pre->bindValue(':numC',$C);
$r = $pre->execute();
mysql countTable
create table countTable (
id varbinary(64) not null,
numA int(100),
numB int(100),
numC int(100),
primary key(id)
);
Buttons are used for creating events, not so much for capturing form input. You are not actually setting or capturing any values, so you should expect your POST vars to be empty anyway.
Based on what you are trying to accomplish, I would recommend the following:
<form action="check.php" method="POST">
<input type="hidden" name="option" value="A" />
<input type="submit" value="A" />
</form>
<form action="check.php" method="POST">
<input type="hidden" name="option" value="B" />
<input type="submit" value="B" />
</form>
<form action="check.php" method="POST">
<input type="hidden" name="option" value="C" />
<input type="submit" value="C" />
</form>
//PHP side
$selected_value = $_POST['option'];
//... do more stuff
He we have 3 different forms, all with a hidden value representing. This hidden value will be sent via POST once the form is submitted.

Two buttons into same form

I'm writing a form and this is its structure:
HTML:
<form method="post" action="script.php">
<input type="text" name="nombre" >
<input type="submit">
<input type="submit">
</form>
PHP:
<?php
if(isset($_POST))
$options = array();
$options[] = "";
// here I use the value input in "name" text input,
// so I need to get it from the form
for ($i=0;$i<=count($buscar)-1;$i++) {
$options[] = "<option value='{$i}'>{$dato}</option>";
}
echo '<select class="" id="articles" size="1" name="articles">';
echo implode("\n", $options);
echo '</select>';
?>
Is there any way to tell the first submit to let the php be executed (because it creates a select item)?
Then, when the select is selected, click on the second submit and send the complete form?
You can do in two ways:
Different Naming:
<input type="submit" name="sub1" />
<input type="submit" name="sub2" />
And you can check it using:
isset($_REQUEST["sub1"])
isset($_REQUEST["sub2"])
Passing Value:
<input type="submit" name="sub" value="Submit Here" />
<input type="submit" name="sub" value="Submit There" />
And you can check it using:
($_REQUEST["sub"] == "Submit Here")
($_REQUEST["sub"] == "Submit There")

HTML form second button wont search my database

I have a database set up called customer. I also have a form that allows me to enter data into that database when submit, which is working fine.
However I also want to be able to search the database on the same page. I added a search field and button, and tried to use an if statement to determine which button was pressed.
The second button for searching the database doesn't seem to be doing anything when pressed, it doesn't seem to even enter the else if statement and I can't see why.
I have the following code:
<?php
require("header.php");
connect('final');//connect to DB
header ("location:/xampp/newCustomer.php");
if (isset($_POST['add'])) {
//do stuff for submit button here which works fine
}
else if (isset($_POST['btnSearch'])){
echo 'searching'; // test if button is working
$query = $_POST['searching'];
$data = mysql_query("SELECT *FROM customer WHERE 'First_Name' LIKE '$query'") ;
if($data === FALSE) {
$error = 'Query error:'.mysql_error();
echo $error;
}
else
{
$test = array();
$colNames = array();
while($results = mysql_fetch_assoc($data))
{
$test[] = $results;
}
$anymatches=mysql_num_rows($data);
if ($anymatches != 0)
{
$colNames = array_keys(reset($test));
}
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
}
}
}
?>
With my form setup like this:
<form name="add" action="newCustomer.php" method="post">
<label><span></span> <input type="text" name="query" palceholder="Type to Search" id="seaching"/></label>
<br />
<label><span>Name</span> <input type="text" name="addFname" /></label>
<button type="button" name="btnSearch" value="Search" id="btnSearch" onclick="this.form.action">Search</button></label>
<input type="hidden" name="searching" value="true" />
<input type="hidden" name="searching" value="true" />
<button type="submit" name="add" value="add" id="btnSub" >Add</button></label>
</form>
</html>
you use type="button", that gives it a button structure but a button dont sumbit a form only a submit does that.
<form name="add" action="newCustomer.php" method="post">
<label><span></span> <input type="text" name="query" palceholder="Type to Search" id="seaching"/></label>
<br />
<label><span>Name</span> <input type="text" name="addFname" /></label>
<button type="button" name="btnSearch" value="Search" id="btnSearch" onclick="this.form.action">Search</button></label>
<input type="hidden" name="searching" value="true" />
<input type="hidden" name="searching" value="true" />
<button type="submit" name="add" value="add" id="btnSub" >Add</button></label>
</form>
</html>
Try this:
HTML:
<form action="NewCustomer.php" method="post">
<table>
<tr>
<td> Name: </td>
<td><input type="text" name="name"></td>
<td><input type="submit" name="add" value="add"></td>
</tr>
</form>
<form action="NewCustomer.php" method="post">
<tr>
<td>Search: </td>
<td><input type="text" name="search"></td>
<td><input type="submit" name="search" value="search"></td>
</tr>
</table>
</form>
And PHP:
<?php
if (isset($_POST['add'])) {
echo "add";
}
if (isset($_POST['search'])){
echo "search";
}
?>
I think this is what you need :)

Erase value if checkbox is checked

I would like the following:
Data = "123";
if checkbox clicked {Data="";}
Submit-button
That was the best way I could explain it.
I have tried, but I can't get the checkbox to overwrite the data with an empty value.
<form action="form.php" method="post">
<input type="text" value="" name="data" />
<input type="checkbox" value="checkbox" name="checkbox" />
<input type="submit" value="Submit" name="submit" />
</form>
That is your form, and this is your PHP
<?php
if(isset($_POST['submit']))
{
if(isset($_POST['checkbox']))
{
$_POST['data'] = "";
}
/* the rest of your form */
}
?>

Categories