How do You Print PHP Object to Separate HTML Page - php

so I have:
$url = unserialize(base64_decode($info['story_frame']));
print $url->html;
On my php page but want to be able to format the printed code and chose where in the html it goes. Any Ideas?

You can also require the above page in the other html page.. anyways for even requesting the POST object, u have to anyways convert it to a php page. So now u have two ways of achieving it..
One requesting the POST object.
i.e
first phppage
<?php
$url = unserialize(base64_decode($info['story_frame']));
?>
<form action="urhtmlpage.php" method="post">
<input type="hidden" name="url" value="<?php echo $url ?>">
</form>
destination php page
<?php echo $_POST['url'] ?>
Requiring the other page in your new page.
first phppage
<?php
$url = unserialize(base64_decode($info['story_frame']));
?>
destination php page
<?php
require("yourphppage.php");
echo $url;
?>
But for both ways convert your HTML page into PHP page

You should probably make that other html page a php page and then request post.
Post to another page within a PHP script

Let's say you put this code in a page called "print.php".
On the code which you would want to put the output on, try the following:
<div class="formatMe">
<?php include 'print.php'; ?>
</div>
You will be able to move the div around as you please, and the content will move with it.

Related

php post to another page or the same?

I usually create another page to do my php posts like this:
page1.php:
<form action="page2.php" method="post">
...
</form>
page2.php:
<?php
$var = $_POST['...'];
?>
one friend of mine told me that I should to this in the same page:
page1.php
<?php
if (isset($_POST['...'])){
...
}
else{
?>
<form action="page1.php" method="post">
...
</form>
<?php
}
?>
My question is, which one is a better or faster method and best practise?
thank you friends!
You can do it in both the ways you have mentioned .
its not like you "Should to this in the same page"
In first part you are passing the control from page1 to page2 ...which is done by submit button
So you can directly get the values using $_POST['...'];
Now in seccond part you are passing the control to same page , Since you are calling the same page on submit .
But here need to check if Post data has been set so for that you use isset method.
Most importantly you can use second solution if you want to stay on the same page after submission
Therefore, Use of isset() method in first part is a good habbit , but in
second solution is a necessity
In my opinion Better use another page so that even the code does not look messed up.

Get url from another page

I'm very new to PHP so maybe its a very simple question.
One page1.php I want to show the url of page2.php
I tried it like this:
One page1.php:
<?php
$url1="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
?>
On page2.php:
<?php
echo $url1; ?>
Its a wordpress installation so I need url with permalinks.
maybe its a noob question but I only need to know how this works.
Thanks!
You should put them running one time, then page2.php can receive the url.
For example: in page2.php, include page1.php before echo.
So in page2.php:
<?php
include 'page1.php';
echo $url1; ?>
There are a few alternative solutions for that. I'd advice using file_get_contents function.
Example usage:
$url1 = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$page1 = file_get_contents($url1);
echo $page1;
See more details here:
http://php.net/manual/en/function.file-get-contents.php

Advanced PHP url getting

Okay, so I have two pages, home.php and page.php. On home.php, I load page.php onto it using jquery. In the url of home.php there is a GET value, u. I want to access that GET value on page.php. You can see that I tried the regular $_GET method, but this does not work. If on page.php I can just get the url of home.php with the GET variables in the url, I can get the info from there. But right now I cannot get the url of home.php, it just gets page.php. I hope that makes sense and thanks for the help!
Home.php
<div id='holder'></div>
<script>
$('#holder').load('page.php');
</script>
Page.php
<?php
$u = $_GET['u'];
echo $_SERVER['PHP_SELF']; // echo's "page.php". I need it to echo "home.php?u=test"
//some php
?>
What you can do is pass the $_GET variable to the requested page.
Do it like this:
<div id='holder'></div>
<script>
$('#holder').load('page.php<?php echo "?u=" . $_GET[" u" ]; ?>');
</script>
Stackoverflow doesn't parse it the way I want but I'm sure it works.

How do i go about echoing back to a form from a form post action?

I have a form containing a textarea for inputing text into. The form also contains a submit button. After pressing the submit button it posts the text within the textarea into my php document. Within my php document the text is added to a database. Once it has been added to the database I would like it to echo back a response telling the user that it has added the text to the database successfully.
However, if i make it echo that response back to the home page, there is nowhere declared for it to display the echoed message. Has anyone got an idea of what i should be doing in order to get this working? Many Thanks.
Normally i wouldn't use a post straight from the form and i would use ajax and then display the data within a paragraph or something on it's return, however since the form is doing the post it's self i am not sure where to then declare where the response should show up.
The bellow displays my html form code and shows it's action to post to a php file.
<div id="userban2"><form id="bannable" action="/onlineusers.php" method="post"><p> Type username to ban bellow:</p>
<textarea name="banned" id="banned" maxlength="255"></textarea><br/>
<input type="submit" value="Send" class="extrabuttons" onclick="return false; preventDefault();">
<div id="cancelban" class="extrabuttons"><p> cancel</p></div>
</form>
However when in my php file i write ....
echo "the information has been added to the database successfully";
It might send the echo back however it isn't declared to display anywhere how can i change this to make it display the response within my form?
As requested return from my php
if(isset($_POST["banned"])){
$ban_name = $_POST["banned"];
bannedd($ban_name);
}
function bannedd($ban_name) {
$query1 = mysql_query("INSERT INTO banned_users (username,firstname,lastname,email,password,ip_address,sign_up_date,last_logged_in,about,sta rr,userpref) VALUES('$usernameb','$fnameb','$lnameb','$emailb','$passwordb','$ip_addressb','$sign_up_date b','$last_logged_inb','$aboutb','$starrb','$userprefb')") or die("Could not insert your informaion");
echo "This user has successfully been banned";
}
The form posts what is written in the form due to it having the action and method of post to my php. However should i then have any return i am not sure how i declare where the returned information should then show (The echoed message).
If I understand you correctly, your form is in some index.php file and sends the data to other file - onlineusers.php, and you want to display the message in the original page?
If this is the case, the most simple way I can think of is redirect back to the original page with a URL parameter, instead of echoing.
Do this at the end of onlineusers.php:
<?php
// insert text into DB ...
header("Location: index.php?result=ok");
?>
This redirects the browser back to the original page with the form. There you check if the status variable is set:
<html>
<head></head>
<body>
<?php if(isset($_GET["result"]) && $_GET["result"]=="ok") { ?>
<p>The information has been added to the database successfully</p>
<?php } ?>
<form> ... </form>
</body>
</html>
As you can probably see, you could set other results, such as "error" this way.
If you don't like the extra string in your URL, then create a cookie after processing the form in onlineusers.php and back at the original page, check if such cookie has been set. If you need more detail on that, let me know. And if you're asking something completely different, well, never mind :)
Your form is being submitted to /onlineusers.php
This is where you would want to add your echo statement.
If you require the info on the same page you technically return to the same page with the form action being $_SERVER['PHP_SELF'].
<form id="bannable" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Then you can put in a conditional statement prior to the load of your document, and include the PHP script.
<?php
$testVar = false;
$msg = '';
if($_POST) {
include '/onlineusers.php';
//... do something e.g post to database and return true.
}
if($testVar) {
$msg = 'Successful writing to DB!';
} ?>
<html>
<body>
<?php echo $msg; ?>
</body>
</html>
This will check to see if you have any post data, if you do, then it includes the script you specify. Maybe set $testVar to true if the writing to DB is successful, and then return $msg in your HTML.

php get results on same page

I Have a php form page where users fill the data and process page to add data to database, Its working fine, But problem is I need the results from process page to be displayed back on to my main page? How to get results back to main page?
In the form's action attribute, set the path to $_SERVER['PHP_SELF'] rather than processing file. This way, form will submit to same page where you can process it.
<form action="<?php echo $_SERVER['PHP_SELF'];?>">
.....
</form>
How about
<form action="mainPage.php" ...>
Simple and your Data will be on the main page.
Use sessions. In script assign a error message to session variable and do redirect. On script.php
$_SESSION['error'] = 'Incorrect email';
index.php
echo $_SESSION['error'];
Don't forget session_start() in begin of scripts.
There's a wide variety of ways, depending on what you're talking about. You'll likely want to use session variables, though. In the processing script:
<?php
start_session();
// Do your processing here
$_SESSION['myvar'] = $finished_data;
?>
And in the main page that called it:
<?php
session_start();
if(!empty($_SESSION['myvar'])) {
$data = $_SESSION['myvar'];
}
// Use $data here as you need
?>
Looks to me like you need to include some Javascript here, then post the returned data to wherever you want. there is.. post to same pagewith...document.getElementById('yourDiv').innerHTML = 'yourReturnedData';
or with $_GET variables.
example...
yoursite.com/yourPage.php?data1=data1&data2=data2
Get returned data by ...
$var1 = $_GET['data1'];
$var2 = $_GET['data2'];
Hope this is of use

Categories