How to delete from mysql using bootstrap modal? - php

I want to delete accounts according to the selection of a drop down list.
This is my markup for the form -
<form action="" method="post">
<select class="form-control" size="8" id="userName" name="userName">
<option value="9" data-userId="uid9">dilani</option>
<option value="6" data-userId="uid6">kamala</option>
<option value="12" data-userId="uid12">senudhi</option>
<option value="2" data-userId="uid2">testuser</option>
<option value="4" data-userId="uid4">tharanga</option>
</select>
<br>
<button type="submit" class="btn btn-primary" >Terminate</button>
<br>
</form>
In PHP, I am checking an user is submitting the form and have selected an username from dropdown before delete the account.
My PHP code is something like this -
// Check for a form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Check for username is selected from dropdown list:
if (isset($_POST['userName'])) {
$userid = $_POST['userName'];
//echo $userid;
// my delete query ---
} else {
$error_msg .= ' You didn\' select either username or domain name to Terminate.';
}
}
My problem is, If the form have submitted successfully then I need to display a conformation dialog box before to delete the account.
I tried it javascript onsubmit but its not working for me. As soon as I click on Terminate button the confirmation is display ignoring the selection of dropdown. That mean I don't need to select an item from username list to get this work.
This is how I checked it -
<form action="" method="post" onSubmit="return confirm('are you sure?')">
Can anybody guide me to do this using Bootstrap modal?
Thank You.

Capture the form on submit, toss out a confirm box. Return true if they click Yes, return false if they click No.
$('form').on('submit', function(){
e.preventDefault();
if(confirm('Are you sure you wish to delete this user?')){
return true;
} else{
return false;
}
});

Either you can submit via ajax, or if you need to just confirm that you can use something like
<form id="target" action="destination.html">
<input id="btn" type="button" value="Go"/>
</form>
$(function() {
$("#btn").click(function(){
if (confirm("Click OK to continue?")){
$('form#target').submit();
} else {
//return false;
}
});
});

Related

Popup on if statement in php

I have a html form which looks like this:
<form action="submitOrder.php" method="get">
<select name="orderForm">
<?php
echo '<option value=" "> </option>';
while($row = \mssql_fetch_array($employeeOrderResult))
{
echo '<option value="'.$row[EMPLOYEE].'">'.$row[EMPLOYEE].'</option>';
}
?>
<option value="Gæst">Gæst</option>
<option value="Praktikant-01">Praktikant-01</option>
<option value="Praktikant-02">Praktikant-02</option>
<option value="Praktikant-03">Praktikant-03</option>
</select>
<br>
Vare: <input type ="text" name="varenr"><br>
Antal: <input type="text" name="antal"><br>
<input type="submit" value="Bestil">
</form>
It fetches som data from a database and adds some special guests.
Now, when it confirms it redirects to a page which has this code in it:
<?php
$ofAntal = $_GET['antal'];
$ofMedarbejder = $_GET['orderForm'];
$ofDato = date('Y-m-d H:i:s');
$ofVareNr = $_GET['varenr'];
$sql = "INSERT INTO Bestillinger(bestillingsAntal,medarbejder,dato,vareNr) VALUES('$ofAntal','$ofMedarbejder','$ofDato','$ofVareNr')";
$validation = mysql_query($sql, $MySQLcon);
if(!$validation)
{
die('Couldnt enter data ' . mysql_error());
}
echo 'Entered data succesfully';
?>
Now, I need a confirmation popup of some kind, if the amount (ofAntal) is above 1, and Ive looked into several solutions. The problem is i started working with PHP tuesday morning, and i cant find a solution that works for me.
All it has to do, is submit the data is yes is clicked, and cancel it if the user clicks no/cancel. This is ofc done in an IF statement, thats not the issue, the issue is how to implement it properly.
ANY help is highly appreciated :)
Use javascript confirm box on onclick attribute for submit button. It will give you yes and cancel options.
<input type="submit" value="Bestil" onclick="confirm("Are you sure ?");">
Orelse
You can write a function in javascript to check that
<input type="submit" value="Bestil" onclick="myfunct();">
function myfunct(){
if(document.getElementById("antal").value > 1)
confirm("Are you sure");
return true;
}

getting selected radio button value in a popup

I have a submit button named "details:, and a table which had radio buttons in each rows.
On selecting radio button and then clicking on my details button I want a popup to appear.
I have used something like this: I am not sure if the code I tried is really pathetic, but plz help me.
The form I used is GET.
This is my radio button:
<input type="radio" name="ID" value="<?php echo $id; ?>" class="radioValue" />
This is my submit button:
<input type="submit" name="details1" value="Details" id="btndetails" class="btn" />
<div id="notice">
<div class="pop">
<?php
if(isset($_GET['details1']))
{
if(isset($_GET['ID']))
{
$n=$_GET['ID'];
echo$n;
//do some sql queries with the id
}
else echo"Fail";
}
?>
</div>
</div>
I have used jquery to ensure the clicks:
(function($)
{
$(function()
{
$('#btndetails').bind('click', function(e)
{
// Prevents the default action to be triggered.
if($('.radioValue').is(':checked'))
{
e.preventDefault();
$("#notice").css("display", "block");
}
else
{
<?php echo"Please select the radio button";?>');
}
});
});
})(jQuery);
the div notice is display:none;
Currently on clicking the radio button + details button i am getting a popup with msg "fail". How can I get my radio button value in the pop up? Is my method wrong? I need to get the radio value in php so that i can use it to retrieve my DB.
I dont want to use any plugins for popups. I want it only by using php,html, css .
Please help me.
This might be your problem:
<input type="submit" name="details1" value="Details" id="btndetails" class="btn" />
<div id="notice">
<div class="pop">
<?php
if(isset($_GET['details1']))
{
if(isset($_GET['ID']))
{
$n=$_GET['ID'];
echo$n;
//do some sql queries with the id
}
else echo"Fail";
}
?> // the first problem I found is here
</div>
</div>
also check your URL are you getting id parameter in query string

jQuery making forms disappear and appear after each other

As the title says This is the code that I tried with. The forms must appear one by one because information from previous forms determine how the next ones will look.
$(document).ready(function(){
$('#first_form').submit(function(){
$('#first_form').fadeOut('fast');
$('#second_form').fadeIn('fast');
});
});
<form action="new_patch.php" method="POST" id="first_form">
Title: <input type="text" name="patch" placeholder="Patch 4.20">
<br/>
Number of Champions: <input type="number" name="champ_number" min="1" max="99">
<br/>
<input type="submit" value="submit">
</form>
<form action="new_patch.php" method="POST" id="second_form" style="display: none;" >
<input type="text" value="text">
<input type="submit" value="submit">
<?php
$champ_number = null;
if(isset($_POST['champ_number']))
{
$champ_number = $_POST['champ_number'];
for($champ_number;$champ_number>0;$champ_number--)
{
echo "<br/>Champion ".$champ_number."<input type=\"number\" name=".$champ_number." min=\"1\" max=\"99\">";
}
}
?>
</form>
You're mixing client-side and server-side form code. Submitting the form will reload the page entirely, so from the looks of your code it will fade in the new form when the old form is submitted, but then reload the page so the old form will show again anyway.
You could either:
Let the PHP determine how the next form appears based on the submission of the first form, e.g. if (isset($_POST["First_form_submit"]) { Show second form... }
Probably better and more user-friendly: make the second form appear below once the user has filled in the relevant inputs on the first form before they've submitted
you can use:
$('#first_form').submit(function(){
$('#first_form').fadeOut(function() {
$('#second_form').fadeIn('fast');
});
return false;
});
From the jQuery documentation the syntax is fadeIn( [duration ] [, complete ] ) it accepts a duration and a onComplete callback that you can use to execute the next action when the first is completed.
I did this once too, just add a submit class to the button and make it like this:
<input type="submit" value="submit" class="submit">
Change script to a click function.
$(document).ready(function(event){
event.preventDefault();
$('.submit').click(function(){
$('#first_form').fadeOut(400);
$('#second_form').fadeIn(400);
});
});
PS, also you need to prevent submit default...otherwise it will just submit the form, see this JSfiddle

Check if form is filled before submitting

I have a form called choose_dates.php that submits to a file called process.php. The form consists of a textbox, a dropdown list and a submit button. I have set it up so that you can submit either one value, or the other, or both at the same time. I would like to have it such that if the user has put a value in the textbox AND the dropdown list, then a prompt will ask if that is what he/she really wants to do. The code below doesn't seem to do that when the submit button is pressed. The rest of my code (that I have not placed on here) works fine, this is more of a user interface issue.
<form name="dates" action="process.php" method="POST">
<input type="text" name="submitDate">
<select name="removeException">
<option value="some-value">display dropdown stuff</option>
.
.
</select>
<input type="submit" value="submit"
<?php
if($_POST['submitDate'] != "" and $_POST['removeException'] != "")
{
echo " onclick=\"return confirm('Are you sure you want to submit both values at the same time?')\" ";
}
?>
tabindex="2">
</form>
And of course, please ask any questions if what I said isn't clear enough. Regards.
Add onsumbit="return checks();" in form tag.
checks is a Javascript function that verify everything is good, if not, return false and the form will not be submited. If true, the form will be submited normally. just move your onclick to onsumbit in form.
You need to do that on the client side using javascript ( preferably ). The post data will be submitted when the form is submitted. Try adding this function as your form's onsubmit event
function func(){
var a = document.getElementsByName('removeException'),
b = document.getElementsByName('submitDate');
if(a[0].value!=null && b[0].value!=null){
var c = confirm('Are you sure you want to submit both values at the same time?')
if(c){
return true;
}else{
return false;
}
}
}
Then
<form name="dates" action="process.php" method="POST" onSubmit='return func()'>

Setting PHP cookie from dropdown menu

I have a PHP script designed to allow users to decide which language a page is displayed in. The information is stored in a cookie and then read when needed to display the correct content.
Currently, I use an HTML dropdown box to allow the user to select the language and then they must press the form submit button to set the cookie. How can I make it so when they select the language in the dropdown menu it automatically selects that and submits the form? I hope you can understand my question.
My current PHP code is:
<?php
$user_lang = null;
if (isset($_POST["setc"])) {
$expire = time() + 60 * 60 * 24 * 30;
setcookie("mycookie", $_POST["sel"], $expire);
header("location: " . $_SERVER["PHP_SELF"]);
} else if (isset($_COOKIE["mycookie"])) {
$user_lang = $_COOKIE["mycookie"];
}
?>
<meta charset='utf-8'>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<select name="sel" size="1">
<option value="en"<?php echo (!is_null($user_lang) && $user_lang === "en" ? " selected" : ""); ?>>English</option>
<option value="es"<?php echo (!is_null($user_lang) && $user_lang === "es" ? " selected" : ""); ?>>Español</option>
<option value="fr"<?php echo (!is_null($user_lang) && $user_lang === "fr" ? " selected" : ""); ?>>Français</option>
<option value="de"<?php echo (!is_null($user_lang) && $user_lang === "de" ? " selected" : ""); ?>>Deutsch</option>
</select>
<input name="setc" value="save setting" type="submit">
</form>
Change your select tag to:
<select name="sel" size="1" onchange="this.form.submit();">
to make the form submit when users selects a language.
If you wish to do that without JavaScript you can use multiple submit buttons method instead:
<input name="set_language[en]" value="English" type="submit">
<input name="set_language[es]" value="Español" type="submit">
<input name="set_language[fr]" value="Français" type="submit">
<input name="set_language[de]" value="Deutsch" type="submit">
Processing this form is simple as it is:
if (isset($_POST["set_language"])) {
$language = key($_POST["set_language"]);
// $language contains user-selected language code now
}
This can't be done with a select field but without JavaScript or any other client-side scripting language.
You need to do it using JavaScript:
<select name="sel" size="1" onchange="this.form.submit();">
Or if you use jQuery:
$("select[name='sel']").change(function() {
$(this).parent().submit();
});
This will submit the form whenever a user changes value in the dropdown list.
You can add an onchange event to the select:
<select onChange="document.forms[0].submit();">
Remember that it's a good thing to leave the submit button for those users who don't have javascript enabled. You can hide the button using javascript, so it won't clutter the window of those that doe have a regular browser.
I think you have to use javascript like the others said, but you can also use the submit button as a backup if the user doesn't have javascript activated:
<noscript><input value="save setting" type="submit"></noscript>
And then, since the submit button will only be shown when the user has javascript disabled, you will need to add a hidden input to check if the form has been submitted on the server side
<input type="hidden" name="setc" value="true" />
The answer is in JavaScript not PHP. You could do it unobtrusively with JavaScript like this:
<script>
window.onload = function() {
document.getElementById("idOfDropDown").addEventListener("change", function() {
document.forms["formName"].submit();
});
}
</script>

Categories