Redirect if page is blank - php

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; ?>

Related

Delete button not working.. Error: Object not found

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>

How to redirect following submit button code to same page and if possible without reloading

I have created a code that run in a popup of WordPress website, the purpose of this code is to validate entered data available in mysql db table if available it displays relative code else it executes other code.
I have tried
action="<?php echo $_SERVER['PHP_SELF']; ?>"
and
headlocation(.......)
but none worked, it is submitting code and redirecting to index.php to display results.
<style><?php include 'search-zip.css'; ?>
</style>
<?php
include 'search-config.php';
$conn = OpenCon();
?>
<h3 class="formhead">Available in</h3>
<h2 class="formhead">Melbourne Inner Suburbs</h2>
<form class="form-wrapper" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="$POSTCODE" id="search" size="25" maxlength="35" value="<?php echo $_REQUEST['$POSTCODE'] ?>" />
<input type="submit"
name="submit" value="Search" id="submit"></form>
<p class="form-text">Please search your postal code for service availability</p>
<?
if (isset($_POST['submit'])) {
{
$zipcode=$_REQUEST['$POSTCODE'];
$sql = "SELECT * FROM service_location WHERE zipcode = $zipcode";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
?>
<style>.formhead{display:none;} .form-wrapper{display:none;} .form-text{display:none;}</style>
<div class="available">
<img class="zipimage" src="https://www.hastygrocer.com.au/wp-content/uploads/2019/07/hasty-grocer-available-popup-image.png" alt="Happy Minion" >
<h2 style="text-align: center;">Experience Melbourne's</h2>
<h2 style="text-align: center;"><i style="color: #3aafa9; font-family: Faster One script=all rev=2; font-size: 60px;">Fast</i> & <b style="color: green; font-family: Aladin; font-size: 60px;">Fresh </b></h2>
<h2 style="text-align: center;">Grocery Delivery</h2>
<span class="pum-close popmake-close "><button class="zipbutton" type="button">Shop</button></span></div>
<?
} else {
?>
<style>.formhead{display:none;} </style>
<div class="available">
<h3 style="text-align: center;">Get Notified When Available in <b style=" text-align: center;font-weight: 800; font-family: arial; color: #3aafa9;"><?php echo "$zipcode"?></b></h3>
<div class="zipform"><?php echo do_shortcode( '[contact-form-7 id="613" title="PopUp"]' ); ?></div>
</div>
<?
}
}}
?>
I created this code to redirect to same page.
but this is redirecting to index.php to display result.
please help me in fixing this using any php or ajax that may fix this code.
sorry I am not an expert developer I got code from internet and created the following code.
Thanks in advance
See this part of code: action="<?php echo $_SERVER['PHP_SELF']; ?>"
The submit will be performed to the link passed from the $_SERVER array, but PHP manual says:
$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these.
Try to make an <? echo $_SERVER['PHP_SELF']; ?> and see what is printed. You will probably get the index.php location.
Try to set the action to the page that contains the form.

How to POST a href link with isset

Please help me, I wanted to do a post method using an anchor tag.
Here is the sample code:
<form action="essay2.php" id="quiz" name="quiz" method="post">
<ul>
<li>
2
</li>
</ul>
<input type="text" name="test1">
</form>
And the PHP code:
<?php
session_start();
if (isset($_POST['submit-test'])) {
$_SESSION['test1'] = $_POST['test1'];
header("Location: essay2.php"]);
}
?>
To elaborate on my comment above...
You can actually style a <button type="submit" name="submit-test">2</button> element to look exactly like a plain text link
consider this approach
button[name="submit-test"] {
background: transparent;
border: 0 none;
color: blue;
text-decoration: underline;
font: inherit;
cursor: pointer;
}
<form action="essay2.php" id="quiz" name="quiz" method="post">
<ul>
<li>
<button name="submit-test" type="submit">2</button>
</li>
</ul>
<input type="text" name="test1">
</form>
Results in the following sent when you click the "2"
You can use javascript with the onclick method on your link.
click to send
You also need to put a id to your form tag (id="myform" in this case).
This is how I would do:
<form action="essay2.php" id="quiz" name="quiz" method="post">
<ul>
<li>
2
</li>
</ul>
<input type="text" name="test1">
<input type="hidden" name="submit-test">
</form>
Use the tag inside the like this :
<button type="submit" name="submit-test"><a href="essay2.php"> 2 </button>

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/

How to let the user choose a page number?

I've added information to some boxes, which are alligned with the 'float' attribute.
The content of all those boxes together is too big to fit in the webpage, so I want to put those boxes on different pages, and to let the user choose which page he wants to view.
E.g: Choose page: 1 2 3... (last page)
The contents of these 'pages' must be in divisions.
For reference, see my css code:
.clear {
clear: both;
}
#over-ons .imagebox,
#over-ons .contentbox
{
background-color: #C4C4A6;
float: left;
margin: 0 10px 40px 0;
padding: 10px;
border: 2px solid black;
}
#over-ons .imagebox {
width: 100px;
text-align: center;
}
#over-ons .contentbox {
width: 200px;
}
Also, see my HTML code of the 'boxes' I mentioned:
<div class="imagebox"><img src="media/images/overons/bas.png" /></div>
<div class="contentbox">
Function: blablablabla<br />
Name: blablabla<br />
City: Breda<br />
Education: HBO Informatica<br />
</div>
<div class="clear"></div>
How do I create the ability to let the user view 3 different boxes, depending on the page number he wants to see?
Edit:
My current solution looks like this, only nothing happens when I click 'page 2':
<?php
//Vraag pagina op
$page = 1;
if (isset($_POST['page']) && is_numeric($_POST['page']))
$page = $_POST['page'];
?>
<? if ($page == 1) { ?>
<div class="imagebox"><img src="media/images/overons/pieter.png" /></div>
<div class="contentbox">
Functie: Bedrijfsleider<br />
Naam: Pieter<br />
Stad: Deurne<br />
Opleiding: HBO Informatica<br />
</div>
<div class="clear"></div>
<div class="imagebox"><img src="media/images/overons/patrick.png" /></div>
<div class="contentbox">
Functie: Verkoper<br />
Naam: Patrick<br />
Stad: Roosendaal<br />
Opleiding: HBO Bedrijfskundige Informatica<br />
</div>
<div class="clear"></div>
<div class="imagebox"><img src="media/images/overons/rick.png" /></div>
<div class="contentbox">
Functie: Ontwikkelaar<br />
Naam: Rick Gommers<br />
Stad: Oosterhout<br />
Opleiding: HBO Informatica<br />
</div>
<div class="clear"></div>
<? }
else if ($page == 2) { ?>
<div class="imagebox"><img src="media/images/overons/bas.png" /></div>
<div class="contentbox">
Functie: Salesmanager<br />
Naam: Bas<br />
Stad: Breda<br />
Opleiding: HBO Informatica<br />
</div>
<div class="clear"></div>
<div class="imagebox"><img src="media/images/overons/robbert.png" /></div>
<div class="contentbox">
Functie: Administrateur<br />
Naam: Robbert Tuerlings<br />
Stad: Goirle<br />
Opleiding: HBO Bedrijfskundige Informatica<br />
</div>
<div class="clear"></div>
<div class="imagebox"><img src="media/images/overons/timothee.png" /></div>
<div class="contentbox">
Functie: Implementatie Technicus<br />
Naam: Timothee<br />
Stad: Breda<br />
Opleiding: HBO Informatica<br />
</div>
<div class="clear"></div>
<? }
else { ?>
<div class="contentbox">
You didn't select a proper page!
</div>
<? } ?>
Imagine your current page is called index.php. So first of all you want to create the links to your pages in your HTML as the following:
Page 1
Page 2
Page 3
And for a clear code you can simply put the content of your "boxes" in an array and then output the corresponding entry for the page:
<?php
$content = array{ "Page 1 data", "Page 2 data", "Page 3 data" };
//Fetch the page
$page = 1;
if (isset($_GET['page']) && is_numeric($_GET['page']))
$page = $_GET['page'];
?>
<div class="contentbox">
<? echo $content[($page - 1)]; ?>
</div>
Another solution (less easy to read and to maintain, but easier if your content is really complex) you can simply fetch the page number and in a regular if else display the box they requested.
<?php
//Fetch the page
$page = 1;
if (isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 0)
$page = $_GET['page'];
?>
<? if ($page == 1) { ?>
<div class="contentbox">
Your first box for page 1
</div>
<? }
else if ($page == 2) { ?>
<div class="contentbox">
Your second box for page 2
</div>
<? }
else if ($page == 3) { ?>
<div class="contentbox">
Your third box for page 3
</div>
<? }
else { ?>
<div class="contentbox">
You didn't select a proper page!
</div>
<? } ?>
But ye, there are 1001 ways to do this. It really depends on your needs and the way you want to do it. The more you hardcode, the uglier the code usually gets.. ;-)
One way is to have all divisions on the page and make only the one visible which user has clicked to...To do so you can make all 3 divs and by default make them hidden and by javascript trigget action...
Another way is by PHP's output buffering to accomplish this:
ob_start(); // begin collecting output
include 'myfile.php';
$result = ob_get_clean(); // retrieve output from myfile.php, stop buffering
$result will then contain the text..
The first method is preety easy one most people use it..But as you specificaly said you have created all separate pages for all 3 div's I had to add the php method..

Categories