I can't unset a session variable properly using a "remove button" click. I have two pages: product page and shopping cart page, but problem is that I have a "remove button" in the shopping cart which is not working properly.
When I click on "remove button" it should run unset($_SESSION['product1'].
But when I select another item then it can display "previous session item" so it means remove button does not run unset($_SESSION['product1'] properly. How do I solve this issue?
Product Page
session_start();
$id=$_REQUEST['id'];
$_SESSION['pid1']= $_POST['ids']; //Product ID//
$_SESSION['product1'][]=$_POST['product'];
<form method="post">
<input type="hidden" name="product[]" value="<?php echo $row1['product']; ?>" />
<input type="hidden" name="ids" value="<?php echo $id?>" />
</form>
<input type="submit" class="button1" name="addtocart" value="Add To Cart"
/>
</form>
Shopping Cart Page
session_start();
$pid=$_SESSION['pid1'];
function remove_product($pid){
$pid=intval($pid);
$max=count($_SESSION['product1']);
for($i=0;$i<$max;$i++){
if($pid==$_SESSION['product1'][$i]['pid']){
unset($_SESSION['product1'][$i]);
break;
}
}
$_SESSION['product1']=array_values($_SESSION['product1']);
}
if($_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
remove_product($_SESSION['product1'][$pid]);
}
<form name="form1" method="post">
<input type="hidden" name="pid" />
<input type="hidden" name="command" />
<input type="button" class="button2" value="Remove"
onclick="javascript:del(<?php echo $pid?>)"/>
Shopping Cart Page Javascript
<script language="javascript">
function del(pid){
if(confirm('Do you really mean to delete this item')){
document.form1.pid.value=pid;
document.form1.command.value='delete';
document.form1.submit();
}
}
function clear_cart(){
if(confirm('This will empty your shopping cart, continue?')){
document.form1.command.value='clear';
document.form1.submit();
}
}
</script>
It might be caused by
$pid=$_SESSION['pid1']; instead of $pid=$_POST['pid1'];
event that is tied to tag wrapped around a button, you coud just use
<input type="button" class="button2" value="Remove" onclick="del('<?php echo $pid?>');"/>
Related
Hi guys i am working for project in php I have <form> with save button <input> to update the data in the database
The idea what i want to do , instead to click every single page to update the data, i want to do it automatically by using for example javascript function for example every 10 min every single page will update auto
Manually if I click Save button it will update data in DB
once i click the page i can see there is some update and it need to save in db, i need to do this automatic click save button for every single pages every 10 min
Here is an example of what i did
<form action="/test.php" method="post">
<input type="text" name="test1" value="<?= $test->from; ?>" />
<input type="text" name="test2" value="<?= $test->to; ?>" />
<input type="text" name="test3" value="<?= $test->distance; ?>" />
<input type="text" name="test4" value="<?= $test->time; ?>" />
<input type="hidden" name="action" value="<?= $action ?>" />
<input type="hidden" name="id" value="<?= $test->id ?>" />
<input type="submit" name="submit" value="<?= $title ?>" />
</form>
And here for the script
window.onload(function () {
setTimeout("document.forms[0].submit();", 10000);
});
I need your help guys to solve this problem it will save my time instead of open every single page and click update button
Try this simple change in your script. I hope it will help.
function autoSubmit(){
setTimeout(document.forms[0].submit(), 10000);
}
window.onload = autoSubmit()
Can we define name of a submit button as list of array??. If yes then if S1 was clicked the values of the text input should be transfer to one of the input box provided in new div and if S2 was clicked then value should be go below the previous input box. and number of input box in new div depend upon the number of submit button.
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
name="Main" method="POST">
<input type="text" name="p1[]" value="">
<input type="text" name="p1[]" value="">
<button type="submit" name="S[]">S1</button>
<button type="submit" name="S[]">S2</button>
<button type="submit" name="S[]">S3</button>
</form>
<?php
if (isset($_REQUEST['submit'])) {
foreach($_POST['p1'] as $i)
{
echo "Hobbies are :".$i."<br>";
}
}
?>
Not getting any output from this.
I am trying to make a list editing page, so far, I am just printing list values from a database into <li> elements, and I also made the list sortable. Ideally what I want is to also be able to double click the list items to edit. The first problem I'm facing is submitting the <li> items in $_POST format in order. I tried using serialize but that only gives the order the values are in, not the values themselves. I also tried using hidden inputs before each <li>, but again the sorting problem happens. This is my code so far:
HTML / PHP:
echo('<div class="jumbotron">
<form id="formOne" action="editListTest.php" method="get">
<input type="hidden" value="'.$dbname.'" name="dbname"></input>
<p>Title</p>
<input style="text-align:center; width: 90%" class="form-control input-md" type="text" name="title" value="'.$listname.'">
<pstyle="padding-top:10px">Items</p>
<input type="hidden" name="listitems" id="listitems" value="">
<ol id="sortable" class="rankings">');
$stmt2 = $mysqli2->prepare("SELECT listitems FROM {$dbname}");
$stmt2->execute();
$stmt2->bind_result($item);
$number = 0;
while ($stmt2->fetch()) {
$number += 1;
echo('<li class="notep" id="notep_'.$number.'"">'.$item.'</li>');
}
?>
</ol>
</input>
<p>
</br>
<input type="button" <?php if ($_SESSION) {echo('class="btn btn-primary" ');}else{echo('class="btn btn-primary" disabled="disabled" ');} ?>type="submit" id="more_fields" onclick="add_fields();" style="font-size:28; max-height:34px; line-height:34px; padding-top:0px; width:34px; padding-left:9px" value="+" />
<input type="hidden" value="<?php echo($private)?>" name="oldprivate" id="oldprivate"/>
<input type="button" <?php if ($_SESSION) {echo('class="btn btn-primary" ');}else{echo('class="btn btn-primary" disabled="disabled" ');} ?>type="submit" id="less_fields" onclick="remove_fields();" style="font-size:28; max-height:34px; line-height:34px; padding-top:0px; width:34px; padding-left:7px" value="-" />
<button <?php if ($_SESSION) {echo('class="btn btn-primary" ');}else{echo('class="btn btn-primary" disabled="disabled" ');} ?>type="submit">Submit Edits</button>
</p>
<p>
Keep This List <?php echo($privateString) ?> : <input style="vertical-align:1px" type="checkbox" name="keepprivate" value="1" checked>
</p>
</form>
Javascript/Jquery:
$('#formOne').submit(function(){
var cerial = $( "#sortable" ).sortable( "serialize");
$('#listitems').val(cerial);
HTML Output (The ListItems are draggable right now, but I cant send them as an array in the order they are in after ing been dragged):
If anyone has any ideas that would be great, also if you have any ideas about double clicking to edit <li> items, that would be great. I have tried using jQuery to switch class and contenteditable but I've sort of hit a dead end with that. Also, just so you know: editListTest.php is the page this code is on, so it's just submitting to itself for now, but it will be submitting to a processing PHP page that will insert edits into a MYSQL database.
Any help is greatly appreciated,
Thank You
This question already has answers here:
How to show a confirm message before delete?
(33 answers)
Closed 7 years ago.
i have retrieved a record from mysql table using php. Each record is retrieved with a form and a delete button. the user is supposed to click the delete button and then the record on that row is deleted. Problem is i want to display an alert before deletion, how do i do this since the record to be deleted has an ID stored in a hidden field inside each form. my retrieved table below;
echo'<tr><td>'.$sn.'</td>
<td>'. htmlspecialchars($r['Year']).'</td>
<td><form method="post" action="awardyear.php">
<input type="hidden" name="yearId" value="'.$r['YearID'].'"/>
</form>
</td>
<td><form method="post" id="awardForm" action="awardyear.php">
<input type="hidden" name="yearId" value="'.$r['YearID'].'"/>
<input type="submit" name="deleteYear" class="btn btn-danger" value="Delete">
</form></td>
</tr>';
Any help will be appreciated, thanks.
Try ..
echo'<tr><td>'.$sn.'</td>
<td>'. htmlspecialchars($r['Year']).'</td>
<td><form method="post" action="awardyear.php">
<input type="hidden" name="yearId" value="'.$r['YearID'].'"/>
</form>
</td>
<td><form method="post" id="awardForm" action="awardyear.php">
<input type="hidden" name="yearId" value="'.$r['YearID'].'"/>
<input onclick="return myConfirm();" type="submit" name="deleteYear" class="btn btn-danger" value="Delete">
</form></td>
</tr>';
// Java Script for this
function myConfirm() {
var result = confirm("Want to delete?");
if (result) {
return true;
} else {
return false;
}
}
Change the form to a link and use JS to redirect to the delete page on confirm.
HTML:
<a href='#' onclick='areYouSure("awardyear.php?deleteYear=Delete&yearId=$YearID")'>DELETE</a>
Javascript function:
function areYouSure(location) {
if(confirm("Are you sure you want to delete this entry?")) {
document.location.href = location;
return true;
} else return false;
}
Try this
HTML
echo'<tr><td>'.$sn.'</td>
<td>'. htmlspecialchars($r['Year']).'</td>
<td><form method="post" action="awardyear.php">
<input type="hidden" name="yearId" value="'.$r['YearID'].'"/>
</form>
</td>
<td><form onsubmit="return deleteThis();" method="post" id="awardForm" action="awardyear.php">
<input type="hidden" name="yearId" value="'.$r['YearID'].'"/>
<input type="submit" name="deleteYear" class="btn btn-danger" value="Delete">
</form></td>
</tr>';
JAVASCRIPT
function deleteThis(){
if(confirm("Are you sure you want to delete?")){
return true;
}else{
return false;
}
}
Use this to alert before delete
$('#awardForm').find('.btn-danger').on('click',function(){
var hiddenValue = $("input[name='yearId']").val();
alert('You sure want to delete '+hiddenValue+' ?');
});
I have 2 forms, 1st form is to submit an input value to the next page, and on the next page there's the 2nd form which is a search map function.
the 1st form is displayed on the homepage
<form role="search-map" method="" id="search-map" action="/find">
<h4>Where To Buy</h4>
<input type="text" class="form-control" id="addressInput" name="addressInput" placeholder="Your Suburb or Postcode">
<button type="submit" class="btn btn-vc btn-default btn-lg" id="search-address-btn">Find</button>
</form>
the 2nd form is on another page which is /find that has a map search plugin
<form onsubmit="cslmap.searchLocations(); return false;" id="searchForm" action="">
<input type="text" id="addressInput" name="addressInput" placeholder="Your Suburb" size="50" value="where-i-want-value-to-be-pass">
<input type="submit" class="slp_ui_button" value="Find" id="addressSubmit">
</form>
any ideas how can i pass the value from the 1st form "addressInput" to the 2nd form? and the run the search on the 2nd form? here's the 2nd form btw
i tried searching here but what i need seems to be more complex that what i have found
or maybe how can i get the 2nd page to get the value from the url (?addressInput=North+Bega%2C+NSW%2C+2550) into the 2nd form input "addressInput" and run the submit button function when the page loads?
thanks in advance guys
I'm assuming you want to get the value from s to the second form.
replace where-i-want-value-to-be-pass
with <?php echo $_REQUEST['addressInput']; ?>
<form onsubmit="cslmap.searchLocations(); return false;" id="searchForm" action="">
<input type="text" id="addressInput" name="addressInput" placeholder="Your Suburb" size="50" value="<?php echo $_REQUEST['addressInput']; ?>">
<input type="submit" class="slp_ui_button" value="Find" id="addressSubmit">
</form>