How do i select buttons using php and enter into database? - php

Here is my current code..
<form action="js.php" method="post">
<input type="button" id='approve' value="Yes" onclick="a()" class="approve" />
<input type="button" id='reject' value="No" onclick="r()" class="reject"/>
<input type="submit" name="submitted" value="TRUE" />
</form>
This is my form and the page title is js.php..
I want that if the user clicks on any of the button the name of the button should bge entered into the database into a table called response under the heading response.
How Do i do that?Thanks..
I have done some PHP part...
<?php
require_once('connect.php');
if(isset($_POST['submitted']))
{
//if on button click
mysqli_select_db($connect,"check");
//entering query
}
?>
here is what Im using to enter the data
<?php
require_once('connect.php');
//if on button click
mysqli_select_db($connect,"pubd");
//entering query
if ($_POST['action'] == 'approve') {
// user clicked Yes
$yes = "insert into response (button) values ('approve')";
$yesr = mysqli_query($connect,$yes) or die(mysqli_error($connect));
} else if ($_POST['action'] == 'reject') {
$no = "insert into response (button) values ('reject')";
$nor = mysqli_query($connect,$no) or die(mysqli_error($connect));
}
?>
<form action="js.php" method="post">
<input type="button" id='approve' name="action" value="Approve" onclick="a()" class="approve" />
<input type="button" id='reject' value="Reject" name="action" onclick="r()" class="reject"/>
</form>

If you're just looking for an answer how to check which button was clicked, here it is:
HTML:
<input type="submit" id='approve' value="Yes" name="action" class="approve" />
<input type="submit" id='reject' value="No" name="action" class="reject"/>
PHP:
if ($_POST['action'] == 'Yes') {
// user clicked Yes
} else if ($_POST['action'] == 'No') {
// user clicked No
}

Related

hide element by checkbox html and php only

I wanna make a checkbox that when user check and submit its appear specific elements.
this is the selection form in admin.php
<form action="/html/codes/html_form_handler.cfm" method="get">
<input type="radio" name="page_type" value="adv" > advertisement<br>
<input type="radio" name="page_type" value="ann" > announcement<br>
<input type="radio" name="page_type" value="pic" > picture <br>
<input type="submit" value="Submit">
</form>
and the selected part will be shown in user.php. each option has its own php file so I got 3 files adv.php, ann.php and pic.php. and using <?php include 'adv.php'; ?> to show in user.php
any suggestion how to do it ??
Update your form like this;
<form action="/html/codes/html_form_handler.cfm" method="get">
<input type="radio" name="page_type" value="adv" > advertisement<br>
<input type="radio" name="page_type" value="ann" > announcement<br>
<input type="radio" name="page_type" value="pic" > picture <br>
<input type="submit" value="Submit">
</form>
And at the backend;
$pages = array ("adv", "ann", "pic");
$page_type = $_GET["page_type"];
// Validate page type
if (in_array($page_type, $pages)) {
include $page_type . ".php";
} else {
die("Invalid page type");
}

Multiple submit on one form

I have two buttons for a register form.
<input type="submit" name="submit1" value="Pay Now" class="submit" id="submit1" />
<input type="submit" name="submit2" value="Pay Later" class="submit" id="submit2" />
Check to see if either button is pushed
if((isset($_POST['submit1'])) or (isset($_POST['submit2'])))
Then PHP code to sanitize and validate data for either input
Now I want to have the "Pay Now" go to one page, and the "Pay Later" go to a different page, but I can n not figure it out. Thanks
You can use $_SESSION. After set session, redirect page and get session data
if(isset($_POST['submit1']) || isset($_POST['submit2'])) {
$_SESSION['post'] = $_POST;
if($_POST['submit1'])
header("Location: pay_now.php");
elseif($_POST['submit2'])
header("Location: pay_later.php");
}
pay_now.php or pay_later.php
$data = $_SESSION['post'];
Use form action like this:
<form action="" method="post">
<input type="submit" name="submit1" value="Pay Now" class="submit" id="submit1" onclick="this.form.action='page1.php'" />
<input type="submit" name="submit2" value="Pay Later" class="submit" id="submit2" onclick="this.form.action='page2.php'" />
</form>
Try this
if(isset($_POST['submit']))
{
if($_POST['submit'] == 'Pay Now')
{
echo $_POST['submit'];
}
if($_POST['submit'] == 'Pay Later')
{
echo $_POST['submit']
}
}
You can do it in PHP but you can also do it in JS using onlick event.
<form method="POST" action="" name="dynamicForm">
<input type="button" name="submit" value="Pay Now" class="submit" id="submit1" onclick="buttonClicked(1);" />
<input type="button" name="submit" value="Pay Later" class="submit" id="submit2" onclick="buttonClicked(2);" />
</form>
<script type="text/javascript">
function buttonClicked(type) {
if (type === 1) {
document.dynamicForm.action = 'firstUrl';
} else {
document.dynamicForm.action = 'secondUrl';
}
document.dynamicForm.submit();
}
</script>

form action not calling script php

Edit: calling the script works but still having a problem with passing my parameters.
here is processdonateditems.php
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['id']) && isset($_POST['type'])) {
$scripttype =mysql_real_escape_string($_POST['type']);
if($scripttype == "cancel") {
....
here are my buttons which have to submit this form
<input type="button" value="Confirm Sent" class="btn" <?php if($fetch_donate3['claim_status']=='1'){?> enabled <?php } else {?> disabled <?php }?>>
<input type="button" value="Cancel Donation" class="btn")
...
</form>
Your buttons have type="button". If you want some of them to submit the form they should contain
type="submit"
If that's not the case then use javascript to submit the form like that:
document.getElementById("change_donate").submit();
Edit:
to receive $_POST['id'] and $_POST['type'] you need to have those elements inside the form. Like that for example:
<input type="hidden" name="id" value="" />
<input type="hidden" name="type" value="" />

PHP: Submit checkbox to database

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');
?>

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 :)

Categories