Delete button not working.. Error: Object not found - php

I am making an ecommerce website. So far, all are okay except where I want to delete items in the cart based on my cart database. The cart comprises of cart_id and menu_id.
Whenever I click the button (Delete), there is an error shown
"Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.52 (Unix) OpenSSL/1.1.1m PHP/8.1.2 mod_perl/2.0.11 Perl/v5.32.1"
I can't figure out what was my mistake. Hope you can help me. Thanks
<div class="container-2">
<?php
include('cart.php');
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (isset($_POST['delete_submit'])) {
// call method addToCart
$Cart->deleteCart($_POST['menu_id']);
}
}
?>
<div class="row">
<div class="col h2 ">
<img src="https://logos-world.net/wp-content/uploads/2020/09/Starbucks-Emblem.png" class="Logo" alt="Logo" />
</div>
</div>
<div class="row" style="min-height: 20vh; border-top: 1px solid #dee2e6;">
<div class="col" style="text-align: left;">
<!-- Display Item From Database -->
<h3>Order here</h3>
<div class="row" style="padding: 1rem; background-color: #FFFFFF; border-radius: 10px; border: 1px solid #dfdfdf;">
<?php
foreach ($menu->getData('cart') as $item) :
$cart = $menu->getProduct($item['menu_id']);
$subTotal[] = array_map(function ($item) {
?>
<div class="col">
<div>
<img src="<?php echo $item['menu_image'] ?? "./menu-img/Warm Drinks/Brewed Coffee/Caffe ministo.jpeg"; ?>" style="width: 80%; border-radius: 25px;">
</div>
<div style="padding: 0.5rem;">
<h3>₱ <?php echo $item['menu_price'] ?? "0.00"; ?></h3>
</div>
</div>
<div class="col" style="margin: auto;">
<h5><?php echo $item['menu_name'] ?? "Unknown"; ?></h6><br>
</div>
<div class="col text-center" style="padding-top: 8%;">
<form action="post">
<input value="<?php echo $item['menu_id'] ?? 0; ?>" name="menu_id">
<?php
echo '<button type="submit" name="delete_submit" class="btn-default btn-lg"><p >Delete</p></button>';
?>
</form>
</div>
<!-- !cart item -->
<?php
return $item['menu_price'];
}, $cart);
endforeach;
?>
</div>
</div>
</div>
function:
public function deleteCart($menu_id = null, $table = "cart")
{
if ($menu_id != null) {
$result = $this->db->con->query("DELETE FROM {$table} WHERE menu_id={$menu_id}");
if ($result) {
header("Location:" . $_SERVER['PHP_SELF']);
}
return $result;
}
}

You have set the Form Element's action attribute to post. This attribute actually specifies the URL that the post data should be sent to. So instead of POSTing to the current URL, it's trying to send the data as a GET request (the default method) to http://yourdomain/post. What you need to do is set the method attribute instead and omit the action attribute.
<form method="post">
<button> Delete </button>
</form>

Related

How to order divs side by side and centered when get datas from database in php and css

How to order divs side by side and centered when get datas from database in php and css ?
php and html codes
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="css/normalize.css">
</head>
<body>
<?php include ("connect.php");
$data = $db->query("select * from videos order by id desc");
$data ->execute();
while ($row=$data->fetch(PDO::FETCH_ASSOC)) {?>
<div class="cards">
<div class="card">
<img src="<?php echo $row['thumbnail'];?>" class="card-img" />
<div class="card-title">
<h3><?php echo $row['v_name'];?></h3>
<p><?php echo $row['v_models'];?></p>
</div>
</div>
</div>
<?php }?>
</body>
</html>
css style codes
body {
background-color:#282828;
}
a{
text-decoration: none;
}
.cards{
text-align: center;
font-size: 0;
}
.card{
margin: 0 10px;
width: 250px;
text-align: left;
display: inline-block;
font-size: medium;
}
.card-img{
width: 250px;
margin-bottom: -5px;
}
.card-title{
background-color: #FFFCF5;
width: 240px;
padding:5px;
margin-bottom: 15px;
color: #282828;
}
.card-title>p>a{
color: #00BFA5;
}
my page looks like this:
but I want to those pictures order side by side
I couldn't that. How could I do?
Thank you
You got .cards inside you while loop, take it out:
<body>
<div class="cards">
<?php include ("connect.php");
$data = $db->query("select * from videos order by id desc");
$data ->execute();
while ($row=$data->fetch(PDO::FETCH_ASSOC)) {?>
<div class="card">
<img src="<?php echo $row['thumbnail'];?>" class="card-img" />
<div class="card-title">
<h3><?php echo $row['v_name'];?></h3>
<p><?php echo $row['v_models'];?></p>
</div>
</div>
<?php }?>
</div>
</body>
The reason your code doesnt work, is because .card is inline-block which will place multiple card nexto eachother, but because you look the .cards (note the S) as well, you wrap each item in a div. A div is a block element, which means it'll take up one whole row, the next element will fall below it.
For each row from database you parse, you will have this following output:
`
<div class="cards">
<div class="card">
<img src="<?php echo $row['thumbnail'];?>" class="card-img" />
<div class="card-title">
<h3><?php echo $row['v_name'];?></h3>
<p><?php echo $row['v_models'];?></p>
</div>
</div>
</div>
`
So, all you have to do is to have the cotainer outside the while loop, like this:
`
<div class="cards">
<?php include ("connect.php");
$data = $db->query("select * from videos order by id desc");
$data ->execute();
while ($row=$data->fetch(PDO::FETCH_ASSOC)) {?>
<div class="card">
<img src="<?php echo $row['thumbnail'];?>" class="card-img" />
<div class="card-title">
<h3><?php echo $row['v_name'];?></h3>
<p><?php echo $row['v_models'];?></p>
</div>
</div>
<?php }?>
</div>
`
I know it's not exactly what you want, but try :
cards { display : flex ; flex-direction : row ; justify-content : space-between }
/ ! \ Doesn't work on IE8 and previous

wrap post in div, and the comment in same div. reproduce reddit nested comment css

I have been trying to mimick the nested comment system that reddit use. Now i have a app that does in a way look like what it was intended to look. But, if you have a look at the reddit style:
I haven't been able to reproduce the lines that attach the previous answer.
My thought is that is nested divs somehow.
currenlty I'm outputting the db data lik this:
<?php if(!empty(getMessage())): ?>
<div class="container">
<h1>The nested comment exampel!</h1>
<?php foreach (getMessage() as $value){ ?>
<div class="level_<?= $value->level; ?> comment" id="<?= $value->id; ?>">
<?php if($value->visible == 0) : ?>
<?= $value->date ?><br>
Deleted
<?php elseif($value->visible == 1): ?>
<?= $value->date ?> X <?= 'id: '. $value->id . ', sort order: ' .$value->sort_order . ', level: '.$value->level ;?><br>
<?= $value->comment_text ?>
<form action="index.php" method="post">
<input type="hidden" name="id" value="<?= $value->id ?>" />
<input type="hidden" name="parent" value="<?= $value->reply_to ?>" />
Add comment: <input type="text" name="svar">
<input type="submit">
</form>
<?php endif; ?>
</div>
<?php } endif; ?>
</div>
And i have solved the left margin with a simpel (not so clean) solution with
<style>
div.level_ {
margin-left: 00px;
}
div.level_1 {
margin-left: 20px;
}
div.level_2 {
margin-left: 40px;
}
div.level_3 {
margin-left: 60px;
}
div.level_4 {
margin-left: 80px;
}
div.level_5 {
margin-left: 100px;
}
div.level_6 {
margin-left: 120px;
}
div.level_7 {
margin-left: 140px;
}
div.level_8 {
margin-left: 160px;
}
div.level_9 {
margin-left: 180px;
}
div.level_10 {
margin-left: 200px;
}
</style>
I have these columns in the database
id,comment_text, reply_to, sort_order, level, date, ip, visible
And my "clone" or what you can call it simply reads the parent id (reply to) and updates all rows if a comment is addad before. if a comment is addad to the first comment/post, the sort_order is simply +1.
But, how can i output a "comment" inside the post. and a answer
comment inside its parent comment.
Currently it looks like this
EDIT
To make it clear i have prepared some mock up data.
Here is the result I'm looking for: http://jsfiddle.net/wv9j9ueo/
With dis data http://pastie.org/10400764 notice the level column. Shouldn't it be possible to foreach the column levelfrom 0 and then keep going until there is nothing left (notice that the can be two or more of the same value).
If you put divs into the parents your code looks prettiest:
<div class="comment">
<p>The comment</p>
<div class="comment">
<p>The comment</p>
<div class="comment"> <p>The comment</p></div>
</div>
</div>
Css
.comment {
margin-left: 15px;
border-left : 2px dotted blue;
padding : 0 10px;
}
PHP
<?php if(!empty(getMessage())): ?>
<div class="container">
<h1>The nested comment exampel!</h1>
<?php
$i=0; //initialize flag
foreach (getMessage() as $value){ ?>
<div class="comment" id="<?= $value->id; ?>">
//you don't need level class
<?php if($value->visible == 0) : ?>
<?= $value->date ?><br>
Deleted
<?php elseif($value->visible == 1): ?>
<?= $value->date ?> X <?= 'id: '. $value->id . ', sort order: ' .$value->sort_order . ', level: '.$value->level ;?><br>
<?= $value->comment_text ?>
<form action="index.php" method="post">
<input type="hidden" name="id" value="<?= $value->id ?>" />
<input type="hidden" name="parent" value="<?= $value->reply_to ?>" />
Add comment: <input type="text" name="svar">
<input type="submit">
</form>
<?php endif; ?>
<?php
$i++; //sum flag
}
for($x=0; $x<=$i; $x++) { //paint div closers
echo '</div>' ;
}
?>
<? endif; ?>
</div>
See it working
http://jsfiddle.net/jc5104st/1/

Redirect if page is blank

I'm working on some sort of search engine that adds on to a URL to complete the request. using header(Location:http://www.WEBSITENAME.com/'. $search); Although sometimes this will send me to a blank page. Was wondering if there's any sort of code that will redirect me to another page if that error happens. I want the else to be what it redirects to if the page is blank. Thanks.
My code:
search.php
<?php
$search = $_POST["search"];
if(isset($_POST['search'])){
header('Location:http://cydia.saurik.com/package/'.$search);
}else{
?>
<?php
echo "
<head>
<style>
#header{
color:black;
}
.header{
color:black;
font-size:25px;
}
#header_minor{
margin-top:20px;
}
.header_minor{
font-size:18px;
}
a[class='header_minor']{
color:black;
}
body{
text-align:center;
margin-top:14%;
}
#idea{
margin-top:20px;
}
.hidden{
display:none;
visibility:hidden;
}
</style>
</head>
";
?>
<div id="header">
<span class="header">
Sorry, this tweak was not found...
</span>
</div>
<div id="header_minor">
<span class="header_minor">
Would you like to suggest it? or Return home
</span>
</div>
<div id="idea">
<form method="POST" action="idea.php" name="idea">
<input type="text" name="idea" value="<?php echo $_POST['search']; ?>"/>
<input type="text" name="hidden" class="hidden"/>
<input type="submit" value="Submit"/>
</form>
</div>
<?php
exit();
}
?>
It could have been better if you can call an api on that site so that you can interact easier with the external site.
I would do something like this:
PHP:
<?php
if(isset($_POST['search'])) {
$search_text = $_POST['search_field'];
$location_url = "http://cydia.saurik.com/package/{$search_text}/";
$content = #file_get_contents($location_url); // use this function to query on site
if($content) {
// redirect if it has a result
header("Location: {$location_url}");
} else {
// will go here if query on external site is empty (blank)
// create your own empty result page that says your search yielded no results (your own page, redirect the user)
// sample
header("Location: http://www.yoursite.com/index.php?noresults=1");
}
}
?>
HTML:
<form method="POST" action="index.php">
<input type="text" name="search_field" class="" id="" /><br/>
<input type="submit" name="search" class="" id="" value="Search" />
</form>
<?php if(isset($_GET['noresults'])): ?>
<div class="yourdesign" style="display: block; width: 300px; border: 1px solid black;";>
<h1>No results found</h1>
</div>
<?php endif; ?>

Display diffrent content in a DIV base on which link you click

I have a demo website I have been trying out some capabilities on. What I am trying to do is only display the DIVs with green or blue based on which link you click into the page.
Here is the example main page i use right now, however it requires me to build 3 separate pages to display results; all.php, green.php, and blue.php. I would like to have only one page and hide or show DIVs as needed
<?php
// which page should be shown now
$page = (isset($_GET['page']) && $_GET['page'] != '') ? $_GET['page'] : 'home';
// only the pages listed here can be accessed
// any other pages will result in error
$allowedPages = array('home',
'all',
'blue',
'green',
)
;
if (!in_array($page, $allowedPages) || !file_exists($page . '.php')) {
$page = 'notfound';
}
// set prettier title
//$title .= ($page != 'home') ? ' - ' . ucfirst($page) : '';
// start output buffering so headers can be used in sub pages
ob_start();
?> <html>
<head>
<title>tester</title>
<link rel="stylesheet" href="nav.css" type="text/css" media="all" />
<script type="text/javascript" src="sucker.js"></script>
</head>
<body>
<ul id="nav">
<li>
All Color
</li>
<li>
<a href="index.php?page=green">Greew/a>
<ul>
<li>
Blue
</li>
</ul>
</li>
</ul>
<div class="clear"></div>
<?php
include($page . '.php');
?>
</body>
</html>
This is the content of all.php
<div style="min-height: 245px; border: 2px solid transparent;" class="itemWrapper">
<a class="itemLink" href="http://www.demo.com/products/blue1.jpg">
<img src="images/products/hold.jpg" class="itemImage" style="width:150px;;height:150px;;"/></a>
<h3 class="itemTitle"> Blue Item 1</a></h3>
<p class="itemPrice">$5.00</p>
<img style="display: none;" class="quickViewBtn" src="images/products/quick_view.png" />
</div>
<div style="min-height: 245px; border: 2px solid transparent;" class="itemWrapper">
<a class="itemLink" href="http://www.demo.com/products/blue2.jpg">
<img src="images/products/hold.jpg" class="itemImage" style="width:150px;;height:150px;;"/></a>
<h3 class="itemTitle"> Blue Item 2</a></h3>
<p class="itemPrice">$3.00</p>
<img style="display: none;" class="quickViewBtn" src="images/products/quick_view.png" /></div>
<div style="min-height: 245px;" class="itemWrapper">
<a class="itemLink" href="http://www.demo.com/products/blue3.jpg">
<img src="images/products/hold.jpg" class="itemImage" style="width:150px;;height:150px;;"/></a>
<h3 class="itemTitle"> Blue Item 3</a></h3>
<p class="itemPrice">$4.00</p>
<img style="display: none;" class="quickViewBtn" src="images/products/quick_view.png" alt= "Quick View" /></div>
<div style="min-height: 245px;" class="itemWrapper last">
<a class="itemLink" href="http://www.demo.com/products/green1.jpg">
<img src="images/products/hold.jpg" class="itemImage" style="width:150px;;height:150px;;"/></a>
<h3 class="itemTitle"> Green Item 1</a></h3>
<p class="itemPrice">$1.00</p>
<img style="display: none;" class="quickViewBtn" src="images/products/quick_view.png" /></div>
<div style="min-height: 245px;" class="itemWrapper last">
<a class="itemLink" href="http://www.demo.com/products/green2.jpg">
<img src="images/products/hold.jpg" class="itemImage" style="width:150px;;height:150px;;"/></a>
<h3 class="itemTitle"> Green Item 2</a></h3>
<p class="itemPrice">$2.00</p>
<img style="display: none;" class="quickViewBtn" src="images/products/quick_view.png" /></div>
</div>
Any help you can provide would be great!
jQuery/JavaScript is the way to go here if you want it to be seamless, but since you're saying this is just a sort of "test" site, I assume you are doing it more to experiment with PHP and have fun. So, what you need to do is put all three pages into one file, then wrap all three pages inside an if...then statement. Effectively:
<?php
if (!isset($_POST['page'])) {
//let the user make a choice
} else if ($_POST['page'] == 'green') {
?>
<!--HTML for the "green" pages goes here!-->
<?php
} else if ($_POST['page'] == 'blue') {
?>
<!--HTML for the "blue" pages goes here!-->
<?php
} else {
print("Invalid page selection!"); //Keep in mind, we want to "error check" to make sure the user actually selected a page
}
?>
If you had many more "pages", you should use a switch statement. If this were a really big application you could use an include() statement (but make sure you check that the file exists!) and just include multiple styled files inside your application.
You could load it all, and have them in different classes, and load them using jQuery. I made an example - check it out here.
$(document).ready(function() {
$(".green").hide();
$(".blue").hide();
Something like that - and then showing them onclick of the buttons.
Just write something like this into your body tag
<body class="
<?php
... if isset... if in_array
switch ($page)
{
case "green":
echo "body_green";
break;
case "blue":
echo "body_blue";
break;
default:
echo "";
}
?>
">
And in your CSS
.green, .blue {
display:none;
}
.body_green .green {
display: block;
}
...

How do I get user input from a form?

I have the current PHP:
$thisarray[] = "#000";
SetProfileField('usercss', $USER->id, implode(',',$thisarray));
Which writes #000 to the users profile field labelled usercss.
However I would like the user to be able to set the value from a simple form.
Can anyone suggest some code I might use?
++ Hope to extend this to include a jQuery color picker so the user does not need to know the hex. E.g. http://acko.net/dev/farbtastic
from the example on http://acko.net/dev/farbtastic:
<form action="yourFile.php" method="post">
<div style="margin-bottom: 1em;" id="picker">
<div class="farbtastic">
<div class="color" style="background-color: rgb(0, 127, 255);"></div>
<div class="wheel"></div>
<div class="overlay"></div>
<div class="h-marker marker" style="left: 55px; top: 170px;"></div>
<div class="sl-marker marker" style="left: 82px; top: 127px;"></div>
</div>
</div>
<div class="form-item">
<label for="color">Color:</label>
<input type="text" style="width: 195px; background-color: rgb(18, 52, 86); color: rgb(255, 255, 255);" value="#123456" name="color" id="color">
</div>
</form>
and your php:
if (!empty($_POST['color']) && preg_match('/^#[a-fA-f0-9]{6}/', $_POST['color']) != 0 ) {
$thisarray[] = $_POST['color'];
SetProfileField('usercss', $USER->id, implode(',',$thisarray));
}
If you don't know PHP I suggest reading some tutorials.
If you do, then the following should set you on the right path:
HTML:
<input name="usercss" value="" type="text" />
PHP:
if (isset($_POST['usercss'])) {
$usercss = filterUserInputPlease($_POST['usercss']);
$thisarray[] = $usercss;
SetProfileField('usercss', $USER->id, implode(',',$thisarray));
}

Categories