I have created a front end page on my Wordpress site where I can submit data through a form, and it will display all the data in a table on the same page. I would like to have a delete option for each row but I am not sure which way to go.
The code I am using to display the database information works fine.
/* Displays Table and Data*/
foreach($cogs as $cogs){
echo "<tr>";
echo "<td>".$cogs->id."</td>";
echo "<td>".$cogs->date."</td>";
echo "<td>".$cogs->source."</td>";
echo "<td>".$cogs->items_purchased."</td>";
echo "<td>".$cogs->receipt_total."</td>";
echo "<td>" "</td>";
echo "</tr>";
}
In the line after "receipt_total," what code should I write? Should I include an anchor tag and link the delete button to another page? I have tried an HTML link, and a HTML button. My problem is making the button or link delete the row from the database. Someone suggested this code, but I am not sure I am using it right. My gut instinct is to place this code in my functions.php file, but I am not sure.
<?php
if(isset($_GET['id']) &&!empty($_GET['id'])){
$id = $_GET['id'];
$table = 'cogs';
$wpdb->delete( $table, array( 'id' => $id ) );
wp_redirect('URL');
}
?>
Related
Whenever I'm going to the designated page using the controller, $row values are lost.
The code below shows how I get to the new page.
while($row = $result->fetch_assoc()) {
$html .= "<tr>";
$html .= "<td><a href='?article=del&".$row["id"]."'><i class='fas fa-trash-alt'></a></i></td>";
$html .= "</tr>";
}
Normally, in my complete table, all the data is shown, including the ID and the names in the db etc. But when I get to the new page, the $row value is lost. For example the del page would be this:
<?php
function del() {
print $row;
}
I would get this error:
Notice: Undefined variable: row in C:\wamp64\www\SMS2\article\del.php on line 3
Instead, the url DOES show the $row["id"] for example:
http://localhost/SMS2/index.php?article=del&15
In this case I clicked on the row which had the 'id' value of 15.
So basically, the $row value does get loaded in the url, which is 15, but is lost in the actual page when I try to print it.
While I can't see the rest of your code, your URL is missing the definition of what 15 is. You need to have a variable in the URL. For example:
http://localhost/SMS2/index.php?article=del&id=15
Instead of
http://localhost/SMS2/index.php?article=del&15
Then in your code you would use something like $_GET['id'] to pull the value of 15 out of the URL.
Change
<a href='?article=del&".$row["id"]."'>
into:
<a href='?article=del&id=".$row["id"]."'>
and on the other page:
<?php
$id = $_GET['id'];
print $id;
?>
I have this while loop, which echos out a form onto a page.
So this page is essentially filled with all rows from the DB, (sort of like a news feed with posts)
I have a button at the end of the form, which lets users reply to this post.
How do I set the variables, to take the values from whichever post they click apply on, and take them to a new PHP document which is the application page where users can fill in another form.
while($value= mysqli_fetch_assoc($result)){
echo '<div class="form">';
echo"<div class='field-warp'>".$value['brand_uid']."</div>";
echo"<div class='field-warp'>".$value['value1']."</div>";
echo"<div class='description'>".$value['value2']."</div><br>";
echo"<div class='field-warp'>".$value['value3']."</div><br>";
echo"<button class='button button-block' name='apply' />Apply to
collaborate</button>";
echo '</div>';
}
Your feed database table probably has an id column or some other unique identifier.
So have the button be a link or turn it to a link and style it as a button. Currently your button could look like this:
echo '<button class="button button-block" name="apply" onclick="location.href=\'http://your_domain.com/feed_apply.php?id='.$row["feed_id"].'\';" />Apply to collaborate</button>';
Then in your feed_apply.php you can fetch that id like so:
$feed_id = $_GET['id'];
$feed_data = [];
// query your database to set $feed_data
Now you can pre-populate or use data elsewhere in your application form.
Why not replace the button with an a href link with a GET parameter with the post ID, then on the processing page you retrieve the post ID and its information with your new form?
I have been doing a tutorial on posting to a database with Ajax and now I have it working with 1 form field, but I would like to learn how to get it to work with 2 form fields.
The form field I have working is named content_txt and it is inserted into add_delete_record(content). The second form is named balance_txt and would also be inserted in the same table as content_txt was.
This is the block of code that I'm trying to figure out how to update with the extra form field. I have added notes in it to explain what I am using each thing for and what I want to do:
if(isset($_POST["content_txt"]) && strlen($_POST["content_txt"])>0)
{ //checks $_POST["content_txt"] is not empty, but I am not sure how to add the other field into this
$contentToSave = filter_var($_POST["content_txt"],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
//using this to strip stuff from the post, but I am not sure how I should added the other form field to this, should I create another variable line or can I add it to this.
// Insert sanitize string in record and I think I have it correct, except if I need to add a variable following content
if(mysql_query("INSERT INTO add_delete_record(`content`,`balance`) VALUES('".$contentToSave."')"))
{
//This returns the results back to the page, do I need to have a duplicate section for the additional record or will this work like a loop.
$my_id = mysql_insert_id(); //Get ID of last inserted row from MySQL
echo '<li id="item_'.$my_id.'">';
echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$my_id.'">';
echo '<img src="images/icon_del.gif" border="0" />';
echo '</a></div>';
echo $contentToSave.'</li>';
mysql_close($connecDB); //close db connection
if you use two form just named same field name "content_txt" and post it .
or when you ajax data change it name on ajax field .
For a little "webshop project" I create a table with PHP and echo"..." function. The table displays some values and in the last cells, there shall be a button which enables the user to delete the corresponding row (or better said, purchase). The data is held in a database and read out while the page loads and than displayed in the table.
I use a "purchase id" to find out which rows have to be deleted, and it works fine if I just implement the function itself. The problem is that I can't get the function working as "onclick" event for the button.
So, some code:
function delete_purchase($purchase_id){
mysql_query("DELETE FROM purchase WHERE purch_id = '$purchase_id'");};
That's the PHP function which deletes the rows, easy enough.
$result = mysql_query("SELECT purchase.purch_id, item.name, purchase.amount, purchase.purch_date, delivery.meaning, item.weight FROM purchase, item, delivery WHERE purchase.cust_id='$cust_id' AND delivery.del_id = purchase.delivered AND purchase.item_id = item.item_id");
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "<td>" . $row['weight'] * $row['amount'] . "</td>";
echo "<td>" . $row['purch_date'] . "</td>";
echo "<td>" . $row['meaning'] . "</td>";
echo "<td><button onclick=\"delete_purchase('" . $row['purch_id'] . "')\">Kill</button></td>";
echo "</tr>";
}
And this is the part which doesn't seem to work. I get the variable and some other values from the database and insert them into my table as long as there are values. Everything is displayed, even the buttons; but clicking on them doesn't do anything.
Source code of the website seems fine:
<td><button onclick="delete_purchase('138')">Kill</button></td>
Hope everything's clear, and you guys have some ideas what's wrong. If you need to know additional stuff, just ask and I'll see what I can do.
onclick="delete_purchase('138')"
calls a Javascript function called delete_purchase, which doesn't seem to exist in your code. You only have a PHP function with that name.
Since all PHP is executed on the server side, the HTML will be built long before the client ever sees the code and therefore you will never be able to call the delete_purchase PHP function from the client side.
The only two ways to get around this are:
- Create a delete_purchase JS function that then calls a PHP file through the use of AJAX.
- Don't call the onclick JS function at all and make the button a regular form submit that you then catch on the server side to delete the purchase. This however would involve a complete page refresh.
Your delete_purchase() function is defined in server-side which is not available in client side. You need to send a request to server and send the id, for example:
?action=delete&id=1
Then you can validate it on server side and call the function
<?php
if(isset($_GET['action']) && $_GET['action'] == 'delete'){
//do some staff
}
?>
you try to call a PHP-function directly from HTML (from browser)
this is impossible!
you may call it using 2 ways:
1) AJAX-call of php-script which will delete the purchase
2) redirect browser to php-script which will delete the purchase and then redirects you back
I am trying to write a simple Javascript snippet into a Codeigniter link. I am using the link to delete posts where required in my dashboard. I dont know anything about JS although am trying to learn it.
Code
$js = 'onClick = "alert("Are you sure")"';
$this->table->set_heading('Date', 'Title', 'Delete', 'Update');
foreach($records as $row){
$row->title = ucwords($row->title);
$this->table->add_row($row->date,
$row->title = ucwords($row->title),
anchor("main/delete/$row->id", $row->id, $js), //this is the link in question
anchor("main/fill_form/$row->id", $row->id)
);
}
$table = $this->table->generate();
echo $table;
My question is how to write the JS for the link ($js). I would like to use a confirm statement, (yes or no). I am totally lost with JS to prevent accidental deletions
Thank you
Here's how you might do it with the CodeIgniter anchor function :
echo anchor('delete/something', 'Delete', array('onClick' => "return confirm('Are you sure you want to delete?')"));
This displays a confirmation box when the link is clicked. If the user confirms then the link is followed. If the user cancels then no action is taken.