I need to make a delete button that only the admin can see. The button needs to delete an item in my database but I'm having troubles with the last part.
I used this code to create the button and to call the delete function when it's clicked
if ($_SESSION['UserID'] == 1) {
echo '<button name="featureDelete"> Delete </button>' . '<br>';
if (isset($_POST['featureDelete'])) {
$deleteFeature = $feature->Delete($row);
}
}
And this is my delete function in my class
public function Delete($row)
{
$db = new db();
$sql= "DELETE FROM features WHERE FeatureID ='".$row['FeatureID']."'";
$db->conn->query($sql);
}
So I can see the button, but when I click it nothing happens, I even tried echo'ing something, but didn't get a result. What am I missing?
You have no form. Only a button.
echo '<form method="post"><button name="featureDelete"> Delete </button></form>' . '<br>';
You have to create a form for your button :
<form action="" method="post">
<input type="submit" name="featureDelete" value="Delete" />
</form>
you have to create a form and send the variable $row.
Try:
<form method="POST"> <input type="text" value="1" name="row" /> <button type="submit" >DELETE</button></form>
And then, on the PHP, before executing the SQL
$row = $_POST['row'];
Related
I'm trying to do two different javascript actions with jquery for my php form which has two submit buttons: 'save' and 'next'. The idea is that both button submits form that saves data into db, but while 'next' goes through client-side validation and progress further, the 'save' just skips validation, returns true and the user stays on the form.
<form id="form" name="form" method="post" action="?action=my_php_form">
<input type="submit" name="save" class="save" id="save" value="save"/>
<input type="submit" name="next" id="next" class="next" value="next"/>
</form>
I already managed to succeed when user clicks either 'save' or 'next' after reload, but if user clicks 'next', launches validation and submit returns false, he cant click 'save' and ignore validation anymore. What might be the cause of this?
$(function() {
//Lets skip the whole thing if save is clicked
$('#save').click(function() {
$('#form').submit(function() {
return true;
});
});
$('#next').click(function() {
$('#form').submit(function() {
var invalid = 0;
//A lot of crazy validation, if some invalid stuff then increment increment invalid
if(invalid > 0) {
return false;
}
else {
return true;
}
});
});
});
I think I would do something like this :
$(function() {
//Lets skip the whole thing if save is clicked
//Actually, no binding is needed as the button is already a submit button
//Thx to Ocanal
$('#next').click(function() {
var invalid = 0;
//Validation process
if(invalid) {
$('#form').submit();
} else {
return false;
}
});
});
Why would you skip validation, especially if it so crazy ? Anyway the problem is here
$('#form').submit(function() { ... }
this doesn't overwrite the submit event handler, this ADDS a function to it. Therefore if you first click next then save , the function you defined for next will still be triggered when clicking on save.
While it's not clear with the notation, it's quite logic : that's what allows you to "add" action to your documentReady event from wherever you wish, not only from a central place.
You can use a different type for your non-submit button.
You'll want something like this:
<form id="form" name="form" method="post" action="?action=my_php_form">
<input type="submit" name="save" class="save" id="save" value="save"/>
<input type="button" name="next" id="next" class="next" value="next"/>
</form>
I think what's happening to you now is that both buttons are acting as your "submit" button, so the form is trying to submit, regardless of which button you're clicking, or what functions you've added to the EventListener.
First of all, Java Script is created every time when refreshing the page. It's important to understand that.
Now, if you click on the button 'Save' of type submit, your information will pack up in the form packet and sent to the server. The submit action requires reloading of the page(!).
Therefore, if you want to keep values in fields (if that's what you want) you may use PHP.
Using PHP is not complicated. I combine the code inside the <body> tag and before the <form> tag.
The code checks whether there is a value in the 'Save' field of $_POST variable, if true, we will save the received values.
And then, I present the values using variable access <? = $name ?>. That's it.
<?php
$name = "";
$credit = "";
if(isset($_POST['save'])) {
$name = $_POST['name'];
$credit = $_POST['creditCard'];
}
?>
<form id="form" name="form" method="post" action="good.php">
<input type="text" name="name" value="<?=$name ?>" />
<input type="text" name="creditCard" value="<?=$credit ?>"/>
<input type="submit" name="save" class="save" id="save" value="save"/>
<input type="submit" name="next" id="next" class="next" value="next"/>
</form>
I have a while loop generating information with a checkbox, I would like to update the database with the new "completed" value. How can I select the specific checkbox that is generated. Please help with showing me how I can grab the specific value of a checkbox and the task_name.
Thanks, Ryan
while ($row = mysql_fetch_array($query)){
$task_name = $row['task_name'] ;
$task_description = $row['task_description'];
$task_completed = $row['completed'];
$tasks .= '<div id="tasksBody">
<form action="" method="post">Completed? <input name="completed" type="checkbox" '.
($task_completed == 1?'checked="checked"':'').
' /><input type="submit" value="Update"><br /><br />
<b>'.$task_name.'</b><br /><br />'.$task_description.'<hr><br /></form></div>';
}
}
echo $tasks;
You need to name your input with something unique for the row, such as the task_name, or better, a database record ID.
Then when the user submits the form, you will use $_POST["YourTaskNameOrIDHere"] to check the value.
What you have currently calls all the check boxes the same thing.
EDIT: I'm sorry, you're isolating all of these in their own forms, I just realized that.
What you can add is an <input type="hidden" value="$task_name" name="TaskName" /> to the form, so you can look what the checkbox is corresponding to. Then, when the user submits the form, use $_POST["TaskName"] to find out the name of the task.
Add a hidden field to each of your forms containing the task_id
<form action="" method="post">
Completed?
<input name="completed" type="checkbox" <?=($task_completed == 1?'checked="checked"':'')?> value="1" />
...
<input name="task_id" value="<?=$task_id"?> type="hidden" />**strong text**
</form>
After submit:
if (isset($_POST['task_id']) { // form has been submitted
$task_id = $_POST['task_id'];
$completed = $_POST['completed'];
$sql = "UPDATE task SET task_completed=$completed WHERE task_id=$task_id LIMIT 1";
// code for updating database
// better use PDO or mysqli-* instead of old and deprecated mysql_*
}
I have Two forms in my index.php and am trying to get them to submit and POST their data seperatly
Form 1
echo '<form action="process.php?type=news" method="post">';
echo '<table cellspacing="0"><tr><th>';
echo 'Add News to News Feed';
echo '</th></tr><tr><td>';
echo 'Title:<br /><input name="newstitle" type="text" id="add" style="height:16px;width:525px;" size="80" maxlength="186" /><br />';
echo 'Main Body Text:<br /><textarea name="newsfeed" id="add" style="width:525px;height:78px;" maxlength="2000" ></textarea>';
echo '<input style="float:right;margin-top:5px;" id="button" type="submit" value="Submit" />';
echo '</td></tr></table></from>';
And Form 2
echo '<form action="process.php?type=suggest" method="post">';
echo '<table cellspacing="0"><tr><th>';
echo 'Suggest Additions to the Intranet';
echo '</th></tr><tr><td>';
echo '<textarea name="suggest" id="add" style="width:330px;height:60px;" maxlength="800" ></textarea>';
echo '<input style="float:right;margin-top:5px;" id="button" type="submit" value="Submit" />';
echo '</td></tr></table></from>';
I want these both to post and do the action after pressing the submit button, but currently the second form submit to the first forms action
How can i fix this???
EDIT: Also i am using .PHP for both the index and process page then using it to echo the forms onto the page
Also here is the process.php data
$type=$_REQUEST['type'];
$suggest=$_POST['suggest'];
$newstitle=$_POST['newstitle'];
$news=mysql_real_escape_string($_POST['newsfeed']);
if ($type == "news")
{
$sql=mysql_query("SELECT * FROM newsfeed WHERE title = ('$newstitle')");
$number_of_rows = mysql_num_rows($sql);
if ($number_of_rows > 0)
{
echo 'This Title is Already Taken';
}
else
{
$sql="INSERT INTO newsfeed (title, news) VALUES ('$newstitle','$news')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
header('Location: index.php');
exit;
}
}
elseif ($type == "suggest")
{
$sql="INSERT INTO suggestions VALUES ('$suggest')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
header('Location: index.php');
exit;
}
Could it be that you are not closing the form with </form>, but with </from>, so the code of the second form is in fact still inside the first form since the tags are never closed?
try giving the submit an on click for each form so in its submit buttons html put this:
onclick="document.forms["myform1"].submit();"
and for form 2 put
onclick="document.forms["myform2"].submit();"
Im not sure exactly what you would like to achieve, so heres my assumptions,
1. your form submits wrongly, eg, second form submits the first form's data
or
2. you would like to do both submits 1 after another.
FOR 1:
Do this.
<form id="form1" action="process.php" method="POST">
<input type="hidden" name="type" value="news">
....
</form>
Note the spelling
/form not /from
<form id="form2" action="process.php" method="POST">
<input type="hidden" name="type" value="suggest">
....
</form>
Note the spelling
/form not /from
Some pointers:
Instead of echoing out so many times, echoing out once is suffice! Remember echos slow down your script. Also I would not pass the type as a $_GET, post is always safer for forms, you can use a hidden field to add the type:
<?php
echo '
<form action="process.php" method="post">
<input type="hidden" name="type" value="news">
...
...
</form>';//Not </from>
?>
Then with the PHP part use a switch case instead of if elses, it will be easier to add features then splitting with another ifelse, plus the default block can be used for anything that dont match like the last else in an if else statement.
<?php
$type = $_POST['type'];
switch($type){
case "news":
...
break;
case "suggest":
...
break;
default:
//default if different type or no type is set
break;
}
?>
Also move over to PDO or prepared mysqli_ functions for your mySQL stuff its safer.
$result = mysql_query('SELECT * FROM teams');
while($row = mysql_fetch_array($result))
{
$team= $row['team'];
$goals= $row['goals'];
$squadsize= $row['squadsize'];
$league= getTheirLeague($team);
echo $team. "</br>";
echo "Goals Scored " . $goals. "</br>";
echo "League " . $league. "</br>" . "</br>";
<form method="POST" action="football.php">
<button type="button">Edit</button>
<button type="button">Remove</button>
</form>
}
I am trying to add a remove and edit feature to my teams, for every team i print out i have a form being printed with 2 buttons. What i am unsure of is how i can tie the button clicks up to the definitive team that the button belongs too.
As each row (team) gets its own form, simply add a hidden field with the team identifier (assuming $row['team'] for this example).
Please note that IE has terrible <button> support in forms. I'd advise using submit inputs...
<form method="POST" action="football.php">
<input type="hidden" name="team"
value="<?php echo htmlspecialchars($row['team']) ?>">
<input type="submit" name="edit" value="Edit">
<input type="submit" name="remove" value="Remove">
</form>
You can then tell which team form was submitted by checking $_POST['team'] and which button was pressed using...
if (isset($_POST['edit']) {
// edit clicked
}
if (isset($_POST['remove']) {
// remove clicked
}
I have a PHP generated form which consists of a list of items, each with a button next to them saying "Remove This" it outputs similar to below:
Item A - [Remove This]
Item B - [Remove This]
...
I wish to be able to click Remove This and it will detect which item it is, and then remove that from the database. Here's my code so far:
selectPlaces.php
<?php
include 'data.php';
mysql_connect($host, $user, $pass) or die ("Wrong Information");
mysql_select_db($db) or die("Wrong Database");
$result = mysql_query("SELECT * FROM reseller_addresses") or die ("Broken Query");
while($row = mysql_fetch_array($result)){
$placeName = stripslashes($row['b_name']);
$placeCode = stripslashes($row['b_code']);
$placeTown = stripslashes($row['b_town']);
$outputPlaces .= "<strong>$letter:</strong> $placeName, $placeTown, $placeCode <input type=\"button\" onclick=\"removePlace()\" value=\"Remove This\" /><br />";
}
mysql_close();
?>
Coupled with my admin.php
<div id="content" style="display:none">
Remove a Place<br><br>
<?php include 'selectPlaces.php'; echo $outputPlaces; ?>
</div>
I know I need to add some javascript to detect which button is clicked but I can't seem to get it working. I tried modifying the onclick="removePlace()" by perhaps passing a variable in the function removePlace(placeID) or something like that, but I'm new to JavaScript and I have no idea how to receive this in the removePlace function.
This seems easier to do without JavaScript. For each entry instead of generating just a button, generate a form that posts to a PHP script that does the deleting.
<form action="deletePlace.php?id=<?php echo $idOfThePlace?>">
<input type="submit" value="Remove This" />
</form>
$idOfThePlace would be the ID with you use to identify the data row.
You don't need JavaScript for that. Try running this example:
<?php var_dump($_POST); ?>
<form action="post.php" method="post">
<p>
<input type="submit" value="a" name="action" />
<input type="submit" value="b" name="action" />
</p>
</form>
You'll see that $_POST['action'] will depend on which button was pressed. For your example, you just need to set the value to identify the item that needs to be deleted. It might be useful to use the <button> element for that: <button name="delete" type="submit" value="12345">delete item 12345</button>. It'll show up as $_POST['delete'] with 12345 as value when submitted.