Deleting aMySQL database with a button in PHP HTML - php

I wanted to delete a row in mysql phpmyadmin database through a button in AssetApprovalForm.php, but when I clicked on the decline button in AssetApprovalForm.php nothing happens.
This is the image for AssetApprovalForm.php
This is my code in AssetApprovalForm.php
<form action='delete.php?SN="<?php echo $SerialNumber; ?>"' method="post">
<div class="row">
<div class="col-sm-6">
<div class="inputfield">
<input type="text" id="decline" name="decline" value="Decline" class="btn" >
</div>
</div>
</form>
<div class="col-sm-6">
<div class="inputfield">
<input type="text" id="submit" name="accept" value="Approve" class="btn">
</div>
</div>
</div>
I have included JavaScript in my AssetApprovalForm.php to perform a pop up after the button is clicked.
<script>
$("#decline").click(function(){
swal("Bruh!", "This ticket has been declined", "error");
});
</script>
<script>
$("#submit").click(function(){
swal("Nice!", "This ticket has been approved", "success");
});
</script>
This is my code for delete.php
<?php
include 'db_connection.php';
$SerialNumber = $_GET["serial"];
$query = "DELETE * FROM waiting_approval WHERE SerialNumber = '" . $SerialNumber . "'";
if(mysqli_query($conn, $query))
{
mysqli_close($conn);
header('Location: AssetApprovalList.php');
exit;
}
else
{
echo "Error declining request";
}
?>

Current:-
<div class="col-sm-6">
<div class="inputfield">
<input type="decline" id="decline" name="decline" value="Decline" class="btn">
</div>
</div>
<div class="col-sm-6">
<div class="inputfield">
<input type="text" id="submit" name="accept" value="Approve" class="btn">
</div>
</div>
I think your input type is wrong it should be type="text" not type="decline"
and for submit button it should be type="submit" not type="text"

Related

php echo file get content on textarea when press button

Hello i have code like this
<html>
<div class="col-xs-3">
<div class="form-group">
<button type="button" id="get_sock" name="get_sock" class="btn-info">Get Socks5</button>
<div>
<textarea name="socks" id="socks" class="form-control" rows="7" placeholder="127.0.0.1:8080"><?php
if (isset($_POST['get_sock'])) {
$grab = file_get_contents("http://smansara.net/fake/grab.php");
echo $grab;
}
?></textarea>
</div>
</div>
</div>
</html>
but i have a problem when i click the button, nothing happens when i click the button, please review my code sir
Add Form tag Before button then it works correctly
<html>
<div class="col-xs-3">
<div class="form-group">
<form action="" method="POST">
<input type="submit" id="get_sock" name="get_sock" class="btn-info" value="get_sock">
</form>
<div>
<textarea name="socks" id="socks" class="form-control" rows="7" placeholder="127.0.0.1:8080">
<?php
if (isset($_POST['get_sock'])) {
$grab = file_get_contents("http://smansara.net/fake/grab.php");
print_r($grab);
}
?></textarea>
</div>
</div>
</div>
</html>
You need to create an Ajax function on this button click then also need to create an php file which will simply read file content and echo them.
So when you Ajax will called on button click then in response you will get file content the you can add that response to your text area in success.
If you don't know about Ajax then search on internet ,.,here i have given an demo example
$(".yourbuttonclass").click(function(){
$.ajax({url: "phpfilename.php", success: function(result){
$("#textarea_id").html(result);
}});
});
You should probably wrap your input inside a form and then change button into submit type in order to make the $_POST['get_sock'] available for your if statement. For Example (Somehow can't make the </form> tag appears at the end or my snippet)
<html>
<form method="post">
<div class="col-xs-3">
<div class="form-group">
<input type="submit" id="get_sock" name="get_sock" class="btn-info" value="Get Sock">
<div>
<textarea name="socks" id="socks" class="form-control" rows="7" placeholder="127.0.0.1:8080"><?php
if (isset($_post['get_sock'])) {
$grab = file_get_contents("http://smansara.net/fake/grab.php");
echo $grab;
}
?></textarea>
</div>
</div>
</div>
$_post is not correct. Use $_POST
<html>
<div class="col-xs-3">
<div class="form-group">
<button type="button" id="get_sock" name="get_sock" class="btn-info">Get Socks5</button>
<div>
<textarea name="socks" id="socks" class="form-control" rows="7" placeholder="127.0.0.1:8080">
<?php if(isset($_POST['get_sock'])){
$grab = file_get_contents("http://smansara.net/fake/grab.php");
echo $grab;
} ?>
</textarea>
</div>
</div>
</div>
You can use input type='button' so that form won't submit if you have.Form is not necessary for ajax call.Try like this:
<html>
<div class="col-xs-3">
<div class="form-group">
<input type="button" id="get_sock" name="get_sock" class="btn-info" value='Get Socks5'>
<div>
<textarea name="socks" id="socks" class="form-control" rows="7" placeholder="127.0.0.1:8080"></textarea>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(function(){
$('#get_sock').on('click',function(){
var contents='<?=nl2br(file_get_contents("http://smansara.net/fake/grab.php"))?>';
var content = contents.split('</br>').join('\n');
$('#socks').html(content);
});
//ajax
$.ajax({
type:'POST',
url :'filename.php',//returns the file_get_contents
success:function(data){
$('#socks').html(data);
}
});
});
</script>
</html>
<html>
<div class="col-xs-3">
<div class="form-group">
<form action="" method="POST">
<input type="submit" id="get_sock" name="get_sock" class="btn-info" value="get_sock">
</form>
<div>
<?php
$tareas='<textarea name="socks" id="socks" class="form-control" rows="7" placeholder="127.0.0.1:8080">';
$tareae='</textarea>';
(isset($_POST['get_sock']))?$grab=file_get_contents("http://smansara.net/fake/grab.php"):$grab='';
echo $tareas,$grab,$tareae;?>
</div>
</div>
</div>
</html>

How to Activate Submit button once all fields are filled

I have the following code displaying input fields in a form. I want to have the submit button active only once all fields are filled. I cant seem to figure out where I've gone wrong. I have omitted some text inputs here for space.
Form:
<?php
if(#$_GET['q']==4 && !(#$_GET['step']) ) {
echo '
<div class="row">
<span class="title1" style="margin-left:40%;font-size:30px;"><b>Enter Quiz Details</b></span><br /><br />
<div class="col-md-3"></div><div class="col-md-6"> <form class="form-horizontal title1" name="form" action="update.php?q=addquiz" method="POST">
<fieldset>
<!-- Text input-->
<div class="form-group">
<div class="col-md-12">
<label for="name">Enter Title</label>
<input id="name" name="name" class="form-control input-md" type="text">
</div>
</div>
<div class="form-group">
<label class="col-md-12 control-label" for=""></label>
<div class="col-md-12">
<input type="submit" style="margin-left:45%" class="btn btn-primary" value="Submit" class="btn btn-primary" id="submit" disabled="disabled"/>
</div>
</div>
</fieldset>
</form>
</div>';
}
?>
<script>
(function() {
$('form input').keyup(function() {
var empty = false;
$('form > input').each(function() {
if ($(this).val() == '') {
empty = true;
}
});
if (empty) {
$('#submit').attr('disabled', 'disabled'); //Leave as disabled if any of the fields are empty
} else {
$('#submit').removeAttr('disabled');//Remove the disabled attribute once all fields are filled
}
});
});
</script>
The code is exactly as it appears here. If I've missed something, kindly point me in a direction. Thank you.
You are using the attribute as disabled="disabled" buts its an attribute having no value you should use it like this <input type="submit" style="margin-left:45%" class="btn btn-primary" value="Submit" class="btn btn-primary" id="submit" disabled/>
Here I have created a working JSFiddle for you check it and do correction https://jsfiddle.net/g1cra5f8/

php - Can not insert multiple text inputs into database

This is my html code.
<form class="form-submit" method="post" action="sign-up-form.php" >
<div id="change-color0">
<label><span class="turn-white0">01</span>Họ tên đầy đủ của bạn</label>
<input type="text" id="input" name="content[]" class="addtodo0">
</div>
<div id="change-color1">
<label><span class="turn-white1">02</span>Số chứng minh thư nhân dân của bạn</label>
<input type="text" id="input" name="content[]" class="addtodo1">
</div>
<div id="change-color2">
<label><span class="turn-white2">03</span>Địa chỉ thường trú của bạn</label>
<input type="text" id="input" name="content[]" class="addtodo2">
</div>
<div id="change-color3">
<label><span class="turn-white3">04</span>Tại sao bạn muốn trở thành học viên của dự án</label>
<input type="text" id="input" name="content[]" class="addtodo3">
</div>
<div id="change-color4">
<label><span class="turn-white4">05</span>Bạn nghĩ mình là ai</label>
<input type="text" id="input" name="content[]" class="addtodo4">
</div>
<div id="change-color5">
<label><span class="turn-white5">06</span>Trong mắt người khác bạn là ai</label>
<input type="text" id="input" name="content[]" class="addtodo5">
</div>
<div id="change-color6">
<label><span class="turn-white6">07</span>Bạn sợ nhất điều gì</label>
<input type="text" id="input" name="content[]" class="addtodo6">
</div>
<div id="change-color7">
<div class="row" id="log-out">
<div id="submit-button">
<button class="btn btn-sharp">
<a name="submit" value="submit" id="submit-form">Gửi đơn</a>
</button>
</div>
<div id="log-out-button">
ĐĂNG XUẤT
</div>
</div>
</form>
And this is my php code to insert the text fields into my database
<?php
if (isset($_POST["submit"])){
$conn = mysql_connect("localhost", "root","");
mysql_select_db("db");
foreach ($_POST['content'] as $content) {
$data = mysql_real_escape_string($content);
mysql_query("INSERT INTO form ( content ) VALUES ('".$data."')") or die(mysql_error());
}
}
header("location: thank-you.html");
?>
Text editor doesnt show any error but the database shows nothing. I've tried
implode()
UPDATE (php code)
I updated my code using PDO like this.
<?php
if (isset($_POST["submit"])){
$pdo = new PDO("mysql: host = localhost; dbname = db","root","");
die(var_dump($_POST['content']));
foreach ($_POST['content'] as $content) {
$query = $pdo->prepare("INSERT INTO form(content) VALUES (:content)");
$query->bindParam(":content", $content);
$query->execute();
}
}
?>
And thing does not show up still.
You should try this. I read this on stackover and it works.
if (isset($_POST["submit"]))
foreach ($_POST['content'] as $content) {
$data1 = mysql_real_escape_string($content);
mysql_query("INSERT INTO form (content) VALUES ('$data1')") or die(mysql_error());
}
}

php insert radio button value only working from mobile

I have a form which was working the day before, and now it's only working from my iphone. I have 4 columns in a mySQL, id, submissiondate, housenumber, answer, hostname
they all work when submitting from my iphone.
If you try from a desktop, the 'answers' which are 3 radio buttons, don't work, and insert a blank record.
wondering if anyone has encountered this before.
if (isset($_POST["submit"])) {
$Answer = $_POST['Answer'];
if ($_POST['housenumber'] == "") {
echo "<div class='container' style='text-align:left'><div role='alert' class='alert alert-danger'>
<p class='lead'><strong>ERROR!</strong> All fields are required</p></div></div>";
} else {
$hostname='XXXXXXXXXXX';
$username='XXXXXXXXXXX';
$password='XXXXXXXXXXX';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=qlfence",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "INSERT INTO qlfence (id, SubmissionDate, housenumber, Answer, hostname)
VALUES ('id', NOW(),'".$_POST["housenumber"]."','".$_POST["Answer"]."','".$_POST["hostname"]."')";
if ($dbh->query($sql)) {
echo "<div class='container' style='text-align:left'><div class='alert alert-success' role='alert'><p class='lead'><strong>Awesome!</strong> Thanks for your input.</p></div></div>";
}
else{
echo "<script type= 'text/javascript'>alert('Opps, something went wrong. We will loo into');</script>";
}
header('Location: /index.php');
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
}
?>
<form action="#" method="post" class="" id="sendmessage" name="sendmessage" >
<input type="hidden" class="form-control" id="hostname" name="hostname" readonly value="<?php echo $hostname; ?>" >
<div class="row">
<div class="col-md-2"> </div>
<div class="col-md-2"><center><input type="text" class="form-control" id="housenumber" name="housenumber" style="width: 35%; border: 2px solid black;"></center></div>
<div class="col-md-6"><label for="housenumber"><span class="lead" >Quarrie Lane #</span></label> </div>
<div class="col-md-2"> </div>
<br>
<br><br>
</div>
<div class="row">
<div class="col-md-2"> </div>
<div class="col-md-2"><input type="radio" id="a1" name="Answer" class="form-control" value="I am Willing to do the fence as soon as everyone is ready."></div>
<div class="col-md-6"><label for="a1"><p class="lead">Willing to do the fence as soon as everyone is ready.</p></label></div>
<div class="col-md-2"> </div>
</div>
<div class="row">
<div class="col-md-2"> </div>
<div class="col-md-2"><input type="radio" id="a2" name="Answer" class="form-control" value="I would rather wait until next summer."></div>
<div class="col-md-6"><label for="a2"><p class="lead">I would rather wait until next summer.</p></label></div>
<div class="col-md-2"> </div>
</div>
<div class="row">
<div class="col-md-2"> </div>
<div class="col-md-2"><input type="radio" id="a3" name="Answer" class="form-control" value="I am not the owner."></div>
<div class="col-md-6"><label for="a3"><p class="lead">Not the owner.</p></label></div>
<div class="col-md-2"> </div>
</div>
<input name="submit" id="submit" type="submit" value="Submit Answer" class="btn btn-lg btn-primary btn-block"></input>
</form>
The only reason why you doesn't recieve $_POST['Answer'] is that you doesn't check any radios of answers before submitting your form.
Besides security issues, your code is working. But your should consider to add more security to your sql-queries.
As a workaround, you can check one of radios by default, like this:
<input checked="true" type="radio" id="a3" name="Answer" class="form-control" value="I am not the owner." />
in this case you will always receive your $_POST['Answer'].

PHP - how to upload 3 images with 3 inputs in one form php

I'm trying to upload 3 images to my server
I have 3 file inputs in the same form.
so far, I receive the following parameters through post (3 images)
$license_img, $car_pic, $driver_pic
And here I try to upload thouse images to server.
if($license_img!= '') {
$license_decoded = base64_decode($license_img);
$license_pic_path = '....images/licenses/'.$email.'.jpg';
file_put_contents($license_pic_path, $license_decoded);
}
if($driver_pic != '') {
$driver_decoded = base64_decode($driver_pic);
$driver_pic_path ='....images/profiles/'.$email.'.jpg';
file_put_contents($driver_pic_path, $driver_decoded);
}
if($car_pic != '') {
$car_decoded = base64_decode($car_pic);
$car_pic_path = '....images/cars/'.$email.'.jpg';
file_put_contents($car_pic_path, $car_decoded);
}
In another script I upload one picture with file_put_content and it works just fine...
How is the right way to upload multiple images with one form?
Update
<script>
function getPicture(img) {
var file = document.getElementById(img);
file.click(); // open file
}
function onImgSelected(event) {
var pieces = event.target.value.split("\\\");
var filename = pieces[pieces.length-1];
if(event.target.id == "driver_img") {
document.getElementById("path_driver_img").value = filename;
} else if(event.target.id == "car_img"){
document.getElementById("path_car_img").value = filename;
} else {
document.getElementById("path_licenta_img").value = filename;
}
}
</script>
<!-- accept="jpeg,jpg,png,bmp" -->
<input type="file" style="display: none" accept="jpeg,jpg,png,bmp" name="driver_img" id="driver_img" onChange="onImgSelected(event)" />
<input type="file" style="display: none" accept="jpeg,jpg,png,bmp" name="car_img" id="car_img" onChange="onImgSelected(event)" />
<input type="file" style="display: none" accept="jpeg,jpg,png,bmp" name="license_img" id="license_img" onChange="onImgSelected(event)" />
<div class="form-group col-xs-12 space-bottom">
<label class="control-label">Driver Picture</label>
<div class="input-group">
<input type="text" class="form-control" id="path_driver_img" readonly>
<span class="input-group-btn">
<button class="btn btn-default" onClick="getPicture(\'driver_img\')" type="button">Up</button>
</span>
</div>
</div>
<div class="form-group col-xs-12 space-bottom">
<label class="control-label">Car Picture</label>
<div class="input-group">
<input type="text" class="form-control" id="path_car_img" readonly>
<span class="input-group-btn">
<button class="btn btn-default" onClick="getPicture(\'car_img\')" type="button">Up</button>
</span>
</div>
</div>
<div class="form-group col-xs-12 space-bottom">
<label class="control-label">Taxi License Image <font color="red">*</font></label>
<div class="input-group">
<input type="text" class="form-control" id="path_licenta_img" readonly>
<span class="input-group-btn">
<button class="btn btn-default" onClick="getPicture(\'license_img\')" type="button">Up</button>
</span>
</div>
</div>
</div>
<div class="col-xs-12"><br>
<div class="col-xs-12" align="center"> <input type="submit" value="Submit" class="btn btn-success btn-md"></div>
</div>
This is a very simple script for uploading images
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file01" /><br />
<input type="file" name="file02" /><br />
<input type="file" name="file03" /><br />
<input type="text" name="text" /><br />
<input type="submit" value="Upload images" />
</form>
<?php
echo '$_FILES:'."<br /><pre>";
var_dump($_FILES);
echo "</pre>";
echo '$_POST:'."<br /><pre>";
var_dump($_POST);
echo "</pre>";

Categories