Multiply pages PHP - Problem - php

I have a problem when creating a script. I have an index.php file, which controls all other pages. Though, I have a problem using the $_GET[''] with $_POST[''] variable in the pages.
I have a page, called adpanel.php Inside that page, I have use the $_GET function like this:
if($_GET['newad'] == "create"):
//In this, I only want the content showed when the above statement is true.
endif;
The above code, does work. I do know how to show the content, if the $_GET is true. Although, inside the $_GET I have a $_POST function, which will submit through jquery, and return the data back to the adpanel.php page.
I have a problem, that it returns the full page in the status div. Example:
http://awesomescreenshot.com/0f8i95ba0
Below is my index, that controls the pages:
case 'a': // Advertisement Panel
if($_GET['newad']){
if($_POST){
include($settings['pagepath'].'adpanel.php&newad=create');
}
include($settings['pagepath'].'adpanel.php&newad=create');
}
if($_GET['manage']){
getHeader();
include($settings['pagepath'].'manageAds.php');
getFooter();
}else{
getHeader();
include($settings['pagepath'].'adpanel.php');
getFooter();
break;
}
How do I fix this issue?

Try this:
case 'a': // Advertisement Panel
if($_GET['newad']){
if($_POST){
include($settings['pagepath'].'adpanel.php&newad=create');
}
include($settings['pagepath'].'adpanel.php&newad=create');
}
if($_GET['manage']){
getHeader();
include($settings['pagepath'].'manageAds.php');
getFooter();
}else{
getHeader();
include($settings['pagepath'].'adpanel.php');
getFooter();
//break;<--remove break
}
break; //<-- break after if/else so it breaks on case and does not continue

Try using
if(isset($_GET['manage'])){

Related

post method not working on condition

I have created a page on which a form posts its value. So I add some lines to get those values like
$parameter = mysqli_real_escape_string($con,$_POST['Parameter']);
But when I open that page without that form it shows Notice that
Undefined index parameter in page on line.
So I want to make something like if I post the values then only specific area will work.
Otherwise remaining area will work just like if condition.
For ex.
if(post)
{}
else
{}
How can I do this?
First you would need to check if the values are set properly..You can do it with the if condition which would be like
if(isset($_POST) && array_key_exists('name_of_your_submit_input',$_POST)){
// do the things after form processing
}else{
//things you want to do after form breaks.
}
Use isset() to check
if(isset($_POST['Parameter'])){
//desired tasks
//$parameter = mysqli_real_escape_string($con,$_POST['Parameter']);
}else{
//other task
}
You can use isset() function. In your case if should be like
if(isset($_POST['param']))
{
//Do something
}
else
{
//Do something else
}

Switching between posts (Goes through all posts?) PHP

Hello I have kind of a step-voting system, so when you click on continue, it goes to the next step.
I do not want to use sessions, therefore I am using POST to go to the next step and queries to call items.
if (isset($_POST['continue']))
{
if ($_POST['vote_page'] == 'site1')
{
$_POST['vote_page'] = 'site2';
}
if ($_POST['vote_page'] == 'site2')
{
$_POST['vote_page'] = 'site3';
}
}
if clicked continue and at site 1, then go to site 2, etc.
switch ($_POST['vote_page'])
{
case 'site1':
echo 'site1!';
break;
case 'site2':
echo 'site 2!!';
break;
case 'site3':
echo 'site 3!!!!!';
break;
}
But for some reason it loops through all of these posts, because the PHP is on the top of the script I think.
Problem:
From site1, it went to site3, ignoring site2...
How can I prevent this? or is there any other way to manage step-voting without sessions?
You can use cookies or hidden input tags to transmit data and you can use $_SERVER["PHP_SELF"] to know your current location, I see you're French so I'm going to say 'Bon chance!'

php: dynamic link appends content to existing page rather than opening new one

I have a dynamic link which fetches invoice detail based on invoice ID.
<a href='<?php echo $_SERVER['PHP_SELF']; ?>/retrieve?class=InvoiceLineItems&id=<?php echo $invoice['invoice_id']; ?>'><?php echo $invoice['invoice_number']; ?></a> <?php echo $invoice['customer_name'] ?> <?php echo $invoice['invoice_date'] ?>
It calls this function
public function retrieve($class,
$id = NULL)
{
switch ($class) {
case 'Invoice':
$invoices = $this->invoice->getInvoices();
include 'view/invoiceList.php';
break;
case 'InvoiceLineItems':
$partnerInfo = $this->partnerInfo->getPartnerInfo($id);
$invoiceLineItems = $this->invoiceLineItems->getInvoiceLineItems($id);
include 'view/invoice.php';
break;
}
}
However, the include statement found in case 'InvoiceLineItems:' appends the content of invoice.php to the bottom of the existing page rather than replacing it altogether. I've tried adding a target to the anchor, but that didn't work. How do I get the link to open the new page?
UPDATE: based on #sixeightzero suggestion, here is the call to retrieve();
if (isset($_REQUEST['id'])) {
// A request ID value indicates arrival here through link.
$this->retrieve('InvoiceLineItems',
$_REQUEST['id']);
}
Also, I tried using a header redirect.
ob_start();
header('Location: /view/invoice.php', 302);
ob_end_flush();
exit();
It redirects, but I lose access to my array variables from
$invoiceLineItems = $this->invoiceLineItems->getInvoiceLineItems($id);
So, I get errors like
Notice: Undefined variable: partnerInfo in C:\xampp\htdocs\bp\view\invoice.php on line 25
and
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\bp\view\invoice.php on line 25
put exit(); at the end of the function to stop executing code after calling it.
or better still use invoice.php to display the invoices instead of the current page.
1) Since, you are using super global *$_SERVER['PHP_SELF']*, the
anchor will always direct to the same page.
2) Now, I assume, you call the function retrieve() at the end of
the page which in turn, after you hit the link, appends
'view/invoice.php' to the page, so you see the appended content at the
end.
Now, you want to get the link to open the new page, there are two ways:
1) You redirect the page to the page view/invoice.php and there you
call the function retrieve and include target="self" in the anchor tag.
Or
2) You do what you are doing but include target="blank" in the
anchor tag.
In order to get the link to open as fresh content, do as follows:
1) Add javascript to the page. In the Html Head section add the
following lines:
<script type="text/javascript">
function hideMainContentDiv(){
document.getElementById("mainContent").setAttribute("style", "display:none");
}
</script>
2) Add the onclick attribute to the anchor tag as:
<a onclick="hideMainContentDiv()">
3) Now, enclose the content of the page in and give it
id="mainContent" as:
<div id="mainContent"> <!-- All your content --> </div>
4) At the end of the page when you have enclosed your content, add
another div and call your function retrieve as:
<div id="newContent">
<?php retrieve($class,$id); ?>
</div>
What this will do is:
When you click the link, it will call the javascript and hide all your existing page content in the div whose id="mainContent".
When your function retrieve() will be called, it will include 'view/invoice.php' which will show your new content.
Your old content has already been hidden by javascript call.
I hope this resolves your query.
The answer turned out to be sessions.
case 'InvoiceLineItems':
$partnerInfo = $this->partnerInfo->getPartnerInfo($id);
$invoiceLineItems = $this->invoiceLineItems->getInvoiceLineItems($id);
$_SESSION['partnerInfo'] = $partnerInfo;
$_SESSION['invoiceLineItems'] = $invoiceLineItems;
ob_start();
header('Location: /view/invoice.php');
ob_end_flush();
exit();
break;
I added the array data to two session variables in the controller. Then, in the view, I replaced the corresponding variable names -- $partnerInfo and $invoiceLineItems -- with their session equivalents $_SESSION['partnerInfo'] and $_SESSION['invoiceLineItems'].

PHP - Navigation using a switch statement

I feel like the following script file should work for navigation on my site, but when I click around on the links nothing loads up and nothing loads by default. How do I fix it?
<html>
<head><title>Your Title</title></head>
<body>
Navigation:
News
Whatever1
<br /><br />
<?php
$id = $_GET;
switch($id)
{
default:
include('home.html');
break;
case "what1":include('whatever1');
break;
case "what2":include('whatever2');
}
?>
</body>
</html>
What are you $_GETing? Also $_GET returns an associative array, and the switch statement takes a variable. You need to specify what it is you get by giving it an id like $_GET['id'].
You need to go in to the $_GET variable and pull out the exact field you want:
$_GET['id']
You don't store the actual GET variable in $id,
$id = $_GET;
should be
$id = $_GET['id'];

Running a function by typing in URL

I have searched all over the internet to run a function from URL and found out it is not possible.
I am asking if there is another way to do this. I will explain below what I am trying to achieve.
On my website I have several functions that when a hyperlink is clicked on that cracks.php page, the main content changes accordingly. Now I want to direct people from a single URL to that cracks.php page with the function already called.
Example - www.example.com/cracks.php?AC ( which will call a function named AC and the content changes before the page loads)
Ive found this method below, but couldnt get it to work.
if(document.location.search == '?AC')
{
AC();
}
Sorry for the messy code on the website. Thanks for reading, any help would be appreciated.
You can call www.example.com/cracks.php?do=AC and then get do with $doMethod = $_GET['do'];. What you then do is, use a switch function or a few ifs to check and execute when e.g. $doMethod equals AC.
Like this:
$doMethod = $_GET['do'];
switch($doMethod)
{
case "AC":
//some random stuff to do
break;
case "BD":
//some random stuff to do
break;
case "XY":
//some random stuff to do
break;
default:
break;
}
That depends if you need to do that dynamically or you can do it hard coded. Because that hard coded is too simple (with if's and switches), what you have to do is:
$functionsList = Array('func1', 'func2');
function func1(){
echo '1';
}
function func2(){
echo '2';
}
if (function_exists($_GET['f']) and in_array($_GET['f'], $functionsList)){
call_user_func($_GET['f']);
}
Then call your_file_name.php?f=func1 and your_file_name.php?f=func2 and you'll see different outputs.
With the help of Mark Koopman I managed to use his Javascript method and it worked like I wanted.
So heres the method in Javascript:
<html>
<body>
<script>
function handleOnload()
{
if(location.search == "?AC")
alert("the query string is " + location.search);
}
window.onload=handleOnload;
</script>
</body>
</html>

Categories