The form was working for a while but I moved some code around and can't revert (A lesson learned), now the code isn't working like it's supposed to. In a nutshell, User clicks delete button > Delete button sends specific Entry ID to function, Function gets form ID, "Confirms delete", then should send to my delete_entry.php code. Now, form is always submitting even when I hit "NO" and I added an alert to tell me which route it goes.
I've tried commenting out the form, and it still submits, so I don't think it's explicitly tied to the confirm function (Could be crazy though)
if ($entry['SubEntryID'] == "0") { ?>
<div id="post"><!-- This is the main post-->
<div id="main">
<div id="title">
<?php echo $entry['EntryID']; ?>
<?php echo $entry['Title']; ?>
</div><br />
<p><?php echo $entry['Body']; ?></p><br />
<div id="postinfo">
At: <?php echo $entry['CreateDate']; ?><br/>
Posted by - <?php echo $entry['UserName']; ?>
</div>
<!-- Start the form for the delete button -->
<form action="Delete_entry.php" method="post" id="Delete<?php echo $entry['EntryID'];?>">
<input type="hidden" readonly="true" name="EntryID" value="<?php echo $entry['EntryID']; ?>" />
<input type="hidden" readonly="true" name="UserID" value="<?php echo $entry['UserID']; ?>" />
<?php if ($userid == 1 || $userid == $entry['UserID']) { ?>
<input onclick="confirmdelete('Delete<?php echo $entry['EntryID'];?>')" type="image" src="redx.png" alt="Delete Post" id="btnDelete" value="Delete Post"/>
<?php } ?><br />
</form>
</div>
function confirmdelete(eid) {
var retval = confirm("Are you sure you want to delete?" + eid + "?");
if (retval == true) {
alert("You said Yes");
document.getElementById(eid).submit();
}
else
{
alert("You said No");
}
}
I want the delete button to call the function, the function to respond by submitting the form if it's true, and cancelling the form submission if false. The form page should only get called if confirm is true, and then submit()
Firstly the confirmdelete() is not returning any true/false back so there is no code that stops the form from submission. Secondly, a form will submit anyway even if the function returns false because you have 2 submits (the form and the onclick).
Move the function call to onsubmit() of the form. Remove the onclick because the input type image submits the form by default. Here is the code:
Form:
<form action="Delete_entry.php" method="post"
id="Delete<?php echo $entry['EntryID'];?>"
onsubmit="return confirmdelete(<?php echo $entry['EntryID'];?>)">
Then the button (onclick removed):
<input type="image" src="redx.png" alt="Delete Post" id="btnDelete" value="Delete Post" />
Finally the JavaScript (returns true/false):
function confirmdelete(eid) {
if (confirm("Are you sure you want to delete?" + eid + "?")) {
alert("You said Yes");
return true;
}
else
{
alert("You said No");
return false;
}
}
Related
I'm developing a web application and I want to get the answer(yes or no) from user when the deactivate button is pressed. I want to ask him if you want or not to deactivate the account. I'm using PHP to set active status to = 1 when deactivated and 0 to activated.
I want to get the result and verify in PHP if the query runs or not.
I would appreciate if someone helps me, thanks. Here is the code:
if(isset($_POST['desativar']))
{
$login = $_SESSION['login'];
mysqli_query($conexao,"UPDATE usuarios SET ativo_usuario = 1 WHERE login_usuario='$login'");
}
You can use onsubmit attribute.
Example
function confirmDesactiv()
{
return confirm("Are you sure ?")
}
<form method="POST" action="yourphp.php" onsubmit="return confirmDesactiv()">
<button type="submit">Delete my account</button>
</form>
try it.
function confirm_delete(){
if(confirm("Are you sure you want to delete this..?") === true){
return true;
}else{
return false;
}
}
<input type="button" Onclick="confirm_delete()">
You can use PHP to make something like a confirmation box. Below is a little smaple code to show you how:
<?php
session_start();
if (isset($_POST['submit'])) {
$_SESSION['postdata'] = $_POST;
header("Location: ".$_SERVER['PHP_SELF']);
exit;
} else if (isset($_POST['confirm']) && $_POST['confirm'] == 'yes') {
// Do stuff that should only be done after confirming
} elseif (isset($_POST['confirm']) && $_POST['confirm'] == 'no') {
// Cancelled, redirect back to page
header("Location: ".$_SERVER['PHP_SELF']);
exit;
}
?>
<?php if (isset($_SESSION['postdata']) && !isset($_POST['confirm'])): ?>
<form method="POST" action="<?= $_SERVER['PHP_SELF']; ?>">
Are you sure you want to do this?<br />
<input type="submit" name="confirm" value="yes">
<input type="submit" name="confirm" value="no">
</form>
<?php else: ?>
<!-- Your form here -->
<?php endif; ?>
If you don't want to use AJAX, and assuming your page is called index.php:
<?
$deactivateStatusDisplay = "display: none"; // Initially, do not display a status.
$deactivateStatusMsg = ""; //Status message is initialized as blank.
$queryRan = false; // Just a flag to know if the user clicked deactivation link.
if( 1 === $_GET['deactivate'] ){
$deactivateStatusDisplay = "display: block";
$deactivateStatusMsg = "Your account will be deactivated.";
$queryRan = true;
}
?>
<!DOCTYPE html >
<html>
<head>
<!-- ALL YOUR METADATA -->
</head>
<body>
<div>Do you want to deactivate your account?</div>
DEACTIVATE
<div id="status" style="<? echo $deactivateStatusDisplay ?>"><? echo $deactivateStatusMsg ?></div>
</body>
</html>
**JS**
<script type="text/javascript">
function confirmDesactiv()
{
var flag = confirm("Are you sure ?");
if(flag)
window.open("yourphp.php?id=" + 1);
else
window.open("yourphp.php?id=" + 0);
}
</script>
**HTML**
<form method="POST" action="">
<button type="submit" onclick="confirmDesactiv()">Desactiv my account</button>
</form>
**PHP**
<?php
echo $_GET['id'];
if(isset($_GET['id']) && $_GET['id'])
{
//your update query will come here
}
?>
I have a php page with 2 submit buttons and 2 radio buttons:
<?php
$choiceIdx = 1;
$language = 'English';
if($_GET)
{
if(isset( $_GET['choice'] ))
{
$choiceIdx = $_GET['choice'];
}
if(isset( $_GET['language'] ))
{
$language = $_GET['language'];
}
}
?>
<form method="get">
<button type='submit' name='choice' value='1'>Choice1</button>
<button type='submit' name='choice' value='2'>Choice2</button>
<input id="English" type="radio" name="language" value="English" <?php echo ($language=='English')?'checked':'' ?> onchange="this.form.submit();" />
<label for="English">English</label>
<input id="Slovenian" type="radio" name="language" value="Slovenian" <?php echo ($language=='Slovenian')?'checked':'' ?> onchange="this.form.submit();" />
<label for="Slovenian">Slovenian</label>
</form>
If I click on Slovenian radio button, I get:
http://localhost/index.php?language=Slovenian
If I then click on Choice2 submit button, "language" is saved and I get:
http://localhost/index.php?choice=2&language=Slovenian
If I then click on English radio button, "choice" is not saved and I get:
http://localhost/index.php?language=English
This is my first php page and after hours of googling i added this line:
<input type="hidden" name="choice" value="<?php echo $choiceIdx; ?>">
The "choice" is now saved, but i get:
http://localhost/index.php?choice=1&language=Slovenian&choice=2
I don't want it twice in url. Please explain what i am doing wrong. Thank you!
EDIT: I want to use GET (and not POST) because the URL has to be saved as a bookmark.
Here is an alternate version (as a followup to my first answer) that updates the hidden value when clicking the choice-button:
<script>
function setChoice(val) {
document.getElementById('hiddenChoice').value=val;
}
</script>
<form method="get">
<button type='submit' onClick="setChoice(1);">Choice1</button>
<button type='submit' onClick="setChoice(2);">Choice2</button>
<input type='hidden' id='hiddenChoice' name='choice' value='<?php echo $choiceIdx; ?>'>
<input id="English" type="radio" name="language" value="English" <?php echo ($language=='English')?'checked':'' ?> onchange="this.form.submit();" />
<label for="English">English</label>
<input id="Slovenian" type="radio" name="language" value="Slovenian" <?php echo ($language=='Slovenian')?'checked':'' ?> onchange="this.form.submit();" />
<label for="Slovenian">Slovenian</label>
</form>
If you have more values to retrieve you might want to create a more sofisticated and less specific js-function. You could easily pass in the id of the target input f.e.
Also you should rethink if it's realy neccessary to always submit the form, or if it might be better to first collect all the data and only send one form back to the server.
Add that to your form:
<input type='hidden' name='choiceStored' value='<?php echo $choiceIdx; ?>'>
This will store the last received val for choice and re-send it at the next form submit.
and change your php to:
$choiceIdx = 1;
$language = 'English';
if($_GET)
{
// eighter get new value
if(isset( $_GET['choice'] ))
{
$choiceIdx = $_GET['choice'];
// or if we don't have a new value, take the 'stored' one:
} elseif (isset($_GET['choiceStored']))
{
$choiceIdx = $_GET['choiceStored'];
}
if(isset( $_GET['language'] ))
{
$language = $_GET['language'];
}
}
You are passing the same name twice. 'choice' has been defined as both the hidden value name and the button value name. To be able to differentiate, you should change the hidden value name to something like 'savedchoice'. And reference it by that name
I want to submit the form and go to the action url only if one checkbox is selected.For that I used javascript as well as php. But it's not working properly.Once I select only one checkbox and preceed after that if I go back to the same page with the given code and go for the edit option again, it is not keeping a check if more than 1 or no checkbox is checked.
I'm not using checkboxes because for the same form i've a delete button which can delete multiple enteries at one time
<form name="form1" method="post" action="">
<input type="submit" name="Edit" value="Edit" />
JavaScript
<script language=Javascript>
function changeaction()
{document.form1.action='4a2edit.php';}
</script>
Php
<?php
if(isset($_POST['Edit'])){
$checkbox = $_POST['checkbox'];
if(count($_POST['checkbox'])>1)
{
echo"<br> <Font color=red> ***More than 1 checkbox selected.Make sure that only one checkbox must be selected.***</Font>";
}
else if (count($_POST['checkbox'])==0)
{
echo"<br><Font color=red> ***No checkbox is selected.***</Font>";
}
else
{
echo "<script language=Javascript>
changeaction();
</script>";
}}
?>
Sorry, the missing code is :
<form name="form1" method="post" action="">
<?php
//Some php code
while($row = mysql_fetch_array($result))
{
echo "<input name=\"checkbox[]\" type=\"checkbox\" value=".$row['ID']."> ".$row['ID'];
}
?>
<input type="submit" name="Edit" value="Edit" />
I have a search bar that uses a javascript function to submit the form when the user hits Enter (which is working) because it doesn't have a submit button, but I need to use php to handle the data in the textbox on post. The form is submitting, but on post it's not able to grab what was in the search textbox.
Here's the code:
<form id="siteWideSearch" name="siteSearch" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<input id="homeSearch" type="text" maxlength="100" onkeypress="startSiteSearch(event);" />
</form>
Javascript:
if (event.keyCode == 13) {
document.getElementById("siteWideSearch").submit();
}
PHP:
if($_SERVER["REQUEST_METHOD"] == "POST") {
echo "<script type=\"text/javascript\">window.alert(\"Post reached. Yay!!\");</script>";
echo "<script type=\"text/javascript\">window.alert(\"Search Criteria: ".trim($_POST['homeSearch'])."\");</script>";
}
I get the popup saying that post was reached, but the second popup just outputs "Search Criteria: " and nothing else.
You're missing the name attribute on your form input. Without it that value is not submitted.
<input id="homeSearch" type="text" maxlength="100" onkeypress="startSiteSearch(event);" />
should be:
<input name="homeSearch" id="homeSearch" type="text" maxlength="100" onkeypress="startSiteSearch(event);" />
I have a product page whos products are created dynamically from mysql along with "add to cart" buttons and quantity inputs on each item. These items are submitted via AJAX. I'm able to submit the correct product but the success message returned from ajax uses a hidden div which hides the add to cart button then displays text that your item in now in the cart. It hides ALL of the cart button and shows the hidden div containing the success message in every form. Any help to solve this is appreciated!
My PHP/HTML
//inside while loop
?>
<div class="form">
<form action="cart_action.php" method="post" name="form" id="form_<?php echo $item_no; ?>">
<input type="hidden" class="text" name="order_code" value="<?php echo $item_no; ?>" id="order_code<?php echo $item_no; ?>" />
<input type="hidden" class="text" name="minorder" value="<?php echo $min; ?>" id="min_<?php echo $item_no; ?>" />
<br><br><div style="float:left; padding:5px;"><b>Qty</b>: <input class="text" type="text" name="quantity" value="<?php echo $min; ?>" size="3" id="quan_<?php echo $item_no; ?>" ?></div>
<div id="status"></div>
<input type="image" src="images/add_to_cart.png" class="psubmit" alt="submit" name="submit" id="submit_<?php echo $item_no; ?>" />
</div><div class="added"><b><div style="color: rgb(85, 176, 90);"><br /><b>Added to Cart </b>(View Cart)</div></b></div>
</form>
<?php
} //end loop
JAVASCRIPT:
$(document).ready(function() {
$('form').submit(function(){
// Catching the DOM element which trigger the event
var $form = $(this);
var orderCode = $form.find('input[name=order_code]');
var quantity = $form.find('input[name=quantity]');
var minOrder = $form.find('input[name=minorder]');
if (quantity.val() == '') {
quantity.addClass('hightlight');
$form.find("#status").html('<font color="red">Please Enter A Quantity!</font>');
return false;
} else quantity.removeClass('hightlight');
if (quantity.val() % minOrder.val()) {
quantity.addClass('hightlight');
$form.find("#status").html('<font color="red">Invalid Multiple!</font>');
return false;
} else quantity.removeClass('hightlight');
//organize the data properly
var data = 'order_code=' + orderCode.val() + '&quantity=' + quantity.val();
//disabled all the text fields
//$('.text').attr('disabled','true');
//show the loading sign
$('.loading').show();
//start the ajax
$.ajax({
//this is the php file that processes the data and send mail
url: "cart_action.php",
//GET method is used
type: "GET",
//pass the data
data: data,
//Do not cache the page
cache: false,
//success
success: function (html) {
//if process.php returned 1/true (send mail success)
if (html==1) {
//hide the form
$('.form').fadeOut('slow');
//show the success message
$('.added').fadeIn('slow');
//if process.php returned 0/false (send mail failed)
} else
//hide the form
$('.form').fadeOut('slow');
//show the success message
$('.added').fadeIn('slow');
}
});
//cancel the submit button default behaviours
return false;
e.preventDefault();
});
});
In your success handler, change the selectors to start from $form:
$form.fadeOut('slow');
$form.children('.added').fadeIn('slow');