I'm having a problem when using file_get_contents combined with $_GET. For example, I'm trying to load the following page using file_get_contents:
https://bing.com/?q=how+to+tie+a+tie
If I were to load it like this, the page loads fine:
http://localhost/load1.php
<?
echo file_get_contents("https://bing.com/?q=how+to+tie+a+tie");
?>
However, when I load it like this, I'm having problems:
http://localhost/load2.php?url=https://bing.com/?q=how+to+tie+a+tie
<?
$enteredurl = $_GET["url"];
$page = file_get_contents($enteredurl);
echo $page;
?>
When I load using the second method, I get a blank page. Checking the page source returns nothing. When I echo $enteredurl I get "https://bing.com/?q=how to tie a tie". It seems that the "+" signs are gone.
Furthermore, loading http://localhost/load2.php?url=https://bing.com/?q=how works fine. The webpage shows up.
Anyone know what could be causing the problem?
Thanks!
UPDATE
Trying to use urlencode() to achieve this. I have a standard form with input and submit fields:
<form name="search" action="load2.php" method="post">
<input type="text" name="search" />
<input type="submit" value="Go!" />
</form>
Then to update load2.php URL:
<?
$enteredurl = $_GET["url"];
$search = urlencode($_POST["search"]);
if(!empty($search)) {
echo '<script type="text/javascript">window.location="load2.php?url=https://bing.com/?q='.$search.'";</script>';
}
?>
Somewhere here the code is broken. $enteredurl still returns the same value as before. (https://bing.com/?q=how to tie a tie)
You have to encode your parameters properly http://localhost/load2.php?url=https://bing.com/?q=how+to+tie+a+tie should be http://localhost/load2.php?urlhttps%3A%2F%2Fbing.com%2F%3Fq%3Dhow%2Bto%2Btie%2Ba%2Btie. you can use encodeURIComponent in JavaScript to do this or urlencode in php.
<?
$enteredurl = $_GET["url"];
$search = urlencode($_POST["search"]);
if(!empty($search)) {
$url = urlencode('https://bing.com/?q='.$search)
echo '<script type="text/javascript">window.location="load2.php?url='.$url.'";</script>';
}
?>
Related
I made a search form to get the profile of people whom i search the name with, the form is down here and it works
form file is named search.php and output file is result.php so literally the way the output link must be shown like
http://localhost/website/result.php?user=Name but what i'm getting is http://localhost/website/result.php
which causes not to copy the profile link and show it to to others or apparently it doesnt work when i go to the first link [result.php?user=Name],
which directs me to index.php
search.php
<form method="post" action="test.php">
<center><h3>YGG Live Player Stats</h3></center>
<h5>Enter Player name :<h5> <br/><br/><input type="text" name="search" size=50 maxlength=50><br/><br/>
<input type="Submit" name="Search" value="Search">
</form>
This is the result.php code down here, in which you could see index.php there is what it is actually taking me when i go to links like this http://localhost/website/result.php?user=Name
<?php
session_start();
if(isset($_POST['search']))
{
include "koneksi.php";
$query = $koneksi->prepare("SELECT * from `playerdata` where `user` = ?");
$query->execute(array($_POST['search']));
if($query->rowCount() > 0)
{
$data = $query->fetch();
?>
//html code here
<?php
}
else
{
go('index.php', 'Username not found!!!');
}
}
else
{
header("Location:index.php");
}
?>
How can i fix it?
You are using method="post" in the form.
If you want your url to be like http://localhost/website/result.php?user=Name then you must change method="post" to method="get" in search.php and use $_GET['search'] instead of $_POST['search'] in result.php.
For more informations: What is the difference between POST and GET?
That's pretty basic stuff so you should definitely spend some more time with tutorials.
You are sending your form with POST so values will not be visible in URL.
To change that simply use GET method and every value from form will appear in result URL.
The values won't be passed in the query string like you say, because the <form> is using the 'POST' method. The issue you're experiencing may be due to your SQL being incomplete without binding a value to the user field in the 'WHERE' clause.. I realised 'koneksi.php' must be generating your connection object, so try adding:
$koneksi->bind_param("s", $_POST['id']);
before executing the query, and remove array($_POST['search']) from the execution call.
Hello stackoverflow users. I need to pull some movie information from the Open Movie Database API. They have no docs on how to use their API so i am very confused. The site i am creating in PHP needs to pull some variables or strings from their api depending on what imdb ID i put in an form. Here is the code i got so far. This gives me: "http://www.omdbapi.com/?i=tt1092026". But i need to get the strings of their API and make them to variables so i can use them in forms later. How do i do this? Please help. Thanks! :D
<form action="?action=grab" method="post">
<input placeholder="tt1092026" type="text" name="id" id="id">
<input type="submit" class="button" value="Grab Movie">
</form>
<?php if ($_GET[action] == "grab") { ?>
<h9>
<?php
$id = $_POST["id"];
$url = "http://www.omdbapi.com/?i=$id";
echo $url;
?>
</h9>
<?php }; ?>
You need to decode the response and assign it to variables (all in PHP).
<?php
$id = $_POST["id"];
$url = file_get_contents("http://www.omdbapi.com/?i=$id");
$json = json_decode($url, true); //This will convert it to an array
$movie_title = $json['Title'];
$movie_year = $json['Year'];
//and so on...
and then when you need to echo them:
echo $movie_title;
or
echo "The movie '$movie_title' was made in $movie_year.";
I think TMDB is is a better API. http://www.themoviedb.org
They have very good documentation here: http://docs.themoviedb.apiary.io
I'm using this in my own web application and it works like a charm.
When I post my form data:
<form action="layouts.php" method="post">
<input type="text" name="bgcolor">
<input type="submit" value="Submit" align="middle">
</form>
to a php page, "layouts.php", it returns the string, as expected.:
$bgcolor = $_POST['bgcolor'];
echo $bgcolor; //returns "red"
echo gettype($bgcolor); // returns "string"
But when I include "layouts.php" in another page it returns NULL.
<?php
include("php/layouts.php");
echo $bgcolor; //
echo gettype($bgcolor); //returns "NULL"
?>
How do I pass the variable to another page?
You'll have to use a session to have variables float around in between files like that.
It's quite simple to setup. In the beginning of each PHP file, you add this code to begin a session:
session_start();
You can store variables inside of a session like this:
$_SESSION['foo'] = 'bar';
And you can reference them across pages (make sure you run session_start() on all of the pages which will use sessions).
layouts.php
<?php
session_start();
$bgcolor = $_POST['bgcolor'];
$_SESSION['bgcolor'] = $bgcolor;
?>
new.php
<?php
session_start();
echo $_SESSION['bgcolor'];
?>
Give the form two action="" and see what happens. I just tried that on a script of mine and it worked fine.
A more proper way to solve this might exist, but that is an option.
I'm absolute beginner in web technologies. I know that my question is very simple, but I don't know how to do it.
For example I have a function:
function addNumbers($firstNumber, $secondNumber)
{
echo $firstNumber + $secondNumber;
}
And I have a form:
<form action="" method="post">
<p>1-st number: <input type="text" name="number1" /></p>
<p>2-nd number: <input type="text" name="number2" /></p>
<p><input type="submit"/></p>
How can I input variables on my text fields and call my function by button pressing with arguments that I've wrote into text fields?
For example I write 5 - first textfield, 10 - second textfield, then I click button and I get the result 15 on the same page.
EDITED
I've tried to do it so:
$num1 = $POST['number1'];
$num2 = $POST['number2'];
addNumbers($num1, $num2);
But it doesn't work, the answer is 0 always.
The "function" you have is server-side. Server-side code runs before and only before data is returned to your browser (typically, displayed as a page, but also could be an ajax request).
The form you have is client-side. This form is rendered by your browser and is not "connected" to your server, but can submit data to the server for processing.
Therefore, to run the function, the following flow has to happen:
Server outputs the page with the form. No server-side processing needs to happen.
Browser loads that page and displays the form.
User types data into the form.
User presses submit button, an HTTP request is made to your server with the data.
The page handling the request (could be the same as the first request) takes the data from the request, runs your function, and outputs the result into an HTML page.
Here is a sample PHP script which does all of this:
<?php
function addNumbers($firstNumber, $secondNumber) {
return $firstNumber + $secondNumber;
}
if (isset($_POST['number1']) && isset($_POST['number2'])) {
$result = addNumbers(intval($_POST['number1']), intval($_POST['number2']));
}
?>
<html>
<body>
<?php if (isset($result)) { ?>
<h1> Result: <?php echo $result ?></h1>
<?php } ?>
<form action="" method="post">
<p>1-st number: <input type="text" name="number1" /></p>
<p>2-nd number: <input type="text" name="number2" /></p>
<p><input type="submit"/></p>
</body>
</html>
Please note:
Even though this "page" contains both PHP and HTML code, your browser never knows what the PHP code was. All it sees is the HTML output that resulted. Everything inside <?php ... ?> is executed by the server (and in this case, echo creates the only output from this execution), while everything outside the PHP tags — specifically, the HTML code — is output to the HTTP Response directly.
You'll notice that the <h1>Result:... HTML code is inside a PHP if statement. This means that this line will not be output on the first pass, because there is no $result.
Because the form action has no value, the form submits to the same page (URL) that the browser is already on.
Try This.
<?php
function addNumbers($firstNumber, $secondNumber)
{
if (isset($_POST['number1']) && isset($_POST['number2']))
{
$firstNumber = $_POST['number1'];
$secondNumber = $_POST['number2'];
$result = $firstNumber + $secondNumber;
echo $result;
}
}
?>
<form action="urphpfilename.php" method="post">
<p>1-st number: <input type="text" name="number1" /></p>
<p>2-nd number: <input type="text" name="number2" /></p>
<?php addNumbers($firstNumber, $secondNumber);?>
<p><?php echo $result; ?></p>
<p><input type="submit"/></p>
You need to gather the values from the $_POST variable and pass them into the function.
if ($_POST) {
$number_1 = (int) $_POST['number1'];
$number_2 = (int) $_POST['number2'];
echo addNumbers($number_1, $number_2);
}
Be advised, however, that you shouldn't trust user input and thus need to validate and sanitize your input.
The variables will be in the $_POST variable.
To parse it to the function you need to do this:
addNumbers($_POST['number1'],$_POST['number2']);
Be sure you check the input, users can add whatever they want in it. For example use is_numeric() function
$number1 = is_numeric($_POST['number1']) ? $_POST['number1'] : 0;
Also, don't echo inside a function, better return it:
function addNumbers($firstNumber, $secondNumber)
{
return $firstNumber + $secondNumber;
}
// check if $_POST is set
if (isset($_POST['number1']) && isset($_POST['number2']))
{
$number1 = is_numeric($_POST['number1']) ? $_POST['number1'] : 0;
$number2 = is_numeric($_POST['number2']) ? $_POST['number2'] : 0;
echo addNumbers($_POST['number1'],$_POST['number2']);
}
You are missing the underscores in
$_POST['number1']
That's all.
maybe it's a little late
but could you set a parameter in the url of the php file to post example:
In the html :
...
<form action="Controllers/set_data.php?post=login" method="post" >
...
In the php :
...
$post_select = $_GET['post'];
switch ($post_select) {
case 'setup':
set_data_setup();
break;
...
You can always use this trick. But keep in mind that if the referrer is hidden it doesn't work.
header("Location: " . $_SERVER["HTTP_REFERER"]);
Just add to your PHP page at the point where there is no more code to be executed, but is still executed.
dear all,
I need to send parameters to a URL without using form in php and get value from that page.We can easily send parameters using form like this:
<html>
<form action="http://..../abc.php" method="get">
<input name="id" type="text" />
<input name="name" type="text"/>
<input type="submit" value="press" />
</form>
</html>
But i already have value like this
<?php
$id="123";
$name="blahblah";
?>
Now i need to send values to http://..../abc.php without using form.when the 2 value send to abc.php link then it's show a value OK.Now i have to collect the "OK" msg from abc.php and print on my current page.
i need to auto execute the code.when user enter into the page those value automatically send to a the url.So i can't use form or href. because form and href need extra one click.
Is their any kind heart who can help me to solve this issue?
You can pass values via GET using a hyperlink:
<a href='abc.php?id=123&name=blahblah' />
print_r($_GET) would then give you the values, or you can use $_GET['id'] etc in abc.php
Other approaches, depending on your needs, include using AJAX to POST/GET the request asynchronously, or using include/require to pull in abc.php if it only includes specific functioanlity.eg:
$id="123";
$name="blahblah";
require('abc.php');
You can do:
$id="123";
$name="blahblah";
echo "<a href = 'http://foo.com/abc.php?id=$id&name=$name'> link </a>";
<?php
$base = 'http://example.com/abc.php';
$id="123";
$name="blahblah";
$data = array(
'id' => $id,
'name' => $name,
);
$url = $base . '?' . http_build_query($data);
header("Location: $url");
exit;