How to use $_post data from another page in a function - php

I am working on a php project, which is in wordpress, I use to send data from one page and use this data in another page, I am using $_POST to get data from that page. But When I try to get the data in function or in if block it becomes empty, but When I use it out side of function or if block, it works, (works means I get output as I want from that page in echo). How do I make that work in function or if block,
First file from which I send data
<form action="http://localhost/w/download.php" target="_blank" method="post">
<input type="hidden" name="cate" value="'.$categories[0]->name.'" /><br />
<input type="hidden" name="url" value="'.get_permalink().'" /><br />
<div align="center">
<input type="image" alt="Download" height="99" src="https://www.google.co.in/logos/doodles/2016/fathers-day-2016-us-5562299671642112-hp.jpg" width="184" />
</div>
</form>
Second file which I used to get data and make it useful for me.
<?php
if(isset($_POST["downloadfile"])) {
$urls=$_POST['url'];
echo $urls;// Does not show the out put and
//doing something
}
?>
<form method="POST">
<input type="submit" value="DOWNLOAD FILE" name="downloadfile"/>
</form>
But when I try to echo $_POST data outside of if block I got that value which I want Like:
Second file
<?php
$urls=$_POST['url'];
echo $urls; // shows me the link
if(isset($_POST["downloadfile"])) {
//doing something
}
?>
<form method="POST">
<input type="submit" value="DOWNLOAD FILE" name="downloadfile"/>
</form>
It shows me that variable $urls is empty. But when I use it out side of if block and echo it shows me output, that what I want.
Can anyone tell me How do I use this in a function or if block in second file, or I am doing some wrong way, So please tell me the right way.
The most important thing is that If any one want to give this question a down vote, please do that But Please Please Comment below Why Did you do that, I am not a genius php developer like you, I just learn from my mistake

Use if(isset($_POST["url"])) { instead of if(isset($_POST["downloadfile"])) {

Shouldn't the submit be in the first form?
Just put this line:
<input type="submit" value="DOWNLOAD FILE" name="downloadfile"/>
Right below
<input type="image" alt="Download" height="99" src="https://www.google.co.in/logos/doodles/2016/fathers-day-2016-us-5562299671642112-hp.jpg" width="184" />
One file should send all the data, and the other file should get (POST) the data. Put
<?php
if(isset($_POST["downloadfile"])) {
$urls=$_POST['url'];
//doing something
}
?>
At the top of the second file
EDIT
Okay, I did a bit of testing.. I think you're missing the php tags.
First file
<form action="http://localhost/w/download.php" target="_blank" method="post">
<input type="hidden" name="cate" value="' <?php $categories[0]->name ?>'" /><br />
<input type="hidden" name="url" value="'<?php get_permalink() ?>'" /><br />
<div align="center">
<input type="image" alt="Download" height="99" src="https://www.google.co.in/logos/doodles/2016/fathers-day-2016-us-5562299671642112-hp.jpg" width="184" />
<input type="submit" value="DOWNLOAD FILE" name="downloadfile"/>
</div>
download.php
<?php
if(isset($_POST["downloadfile"])) {
$urls=$_POST['url'];
}
echo $urls;
?>
When I run this on my own server, I get errors, but I think that is because I don't have get_permalink() defined.
Edit 2
Here is the first file
<form action="http://localhost/w/download.php" target="_blank" method="post">
<input type="hidden" name="cate" value="<?php $categories[0]->name ?>" /><br />
<input type="hidden" name="url" value="<?php get_permalink() ?>" /><br />
<div align="center">
<input type="image" value="submit" name="downloadfile" alt="Download" height="99" width="184" src="https://www.google.co.in/logos/doodles/2016/fathers-day-2016-us-5562299671642112-hp.jpg" />
</div>
And here is the second
<?php
if(isset($_POST['downloadfile_x'])) { //Name of the image, and x coordinate
$urls=$_POST['url'];
}
echo $urls;
?>

Related

method post in <a href> method post [duplicate]

This question already has answers here:
Make a link use POST instead of GET
(11 answers)
Closed 7 years ago.
Is there a way to replicate this in <a href="blah.php">?
<form action="http://localhost/php/suburb_added.php" method="post">
<b>Add a New Suburb</b>
<p>Name:
<input type="text" name="suburb" size="30" value="" />
<input type="submit" id="submit" value="Submit" name="submit" />
</p>
</form>
in suburb_added.php... i have this to capture
if (!empty($_POST['suburb']))
To a table form....
<td align="left"><a href="suburb_added.php"><?php echo $row['id'];?></td>
how to create the items below from a table? The goal is when I click the result from <?php echo $row['id'];?>, I should be able to get the value of "id" and process it in suburb_added.php using similar to if (!empty($_POST['suburb']))
<form action="http://localhost/php/suburb_added.php" method="post">
<input type="text" name="suburb" size="30" value="" />
what do you whant i don't understand?? I can help you
<?php
if(!isset($_POST['submit'])){
?>
<form action="" method="post" name="submit">
<b>Add a New Suburb</b>
<p>Name:
<input type="text" name="suburb" size="30" value="" />
<input type="submit" id="submit" value="Submit" name="submit" />
</p>
</form>
<?php
} else {
//paste here your code from http://localhost/php/suburb_added.php
echo "you doing post in this page.";
}
?>
<!--Try using header
like this:-->
if($_POST)
{
header('location:login-form.php');
}
else
{
echo "";
}
Change the PHP script so it uses $_REQUEST instead of $_POST. This variable combines the contents of $_POST and $_GET. Then you can have a link like
<?php echo $row['id'] ?>

Capture user input from Form

I'm a beginner in PHP, I'm trying to add Google PR tool in my blog. but i don't know how to make a user input based PHP url.
<?
require("PRclass.php");
$url='http://www.digitcrop.com/';
$pr = new PR();
echo "$url has Google PageRank: ".$pr->get_google_pagerank($url) ;
?>
Exactly what I want.
$url='http://www.digitcrop.com/';
Change to user input url with like below html
<p>
<input name="url[]" type="text" id="url[]" value="http://" size="80" /><br />
</p>
<p>
<input name="findpr" type="submit" value="Find Google PageRank" />
</p>
Depending on your Form method you get it via $_GET['yourinput'] or $_POST['yourinput']
You Need to Submit the form( weather GET or POST Method ) and then Putting the Input Values on correct Locations.
<?php
require("PRclass.php");
if(isset($_POST['sample']))
{
$url=urlencode($_POST['url']);
$pr = new PR();
echo "$url has Google PageRank: ".$pr->get_google_pagerank($url) ;
}
?>
<form name="sampleform" method="post" action="">
<p>
<input name="url" type="text" id="url" value="" size="80" /><br />
</p>
<p>
<input name="findpr" type="submit" value="Find Google PageRank" />
</p>
<input type="submit" name="sample" value="submit" />
</form>
To elaborate on #mfsymb's answer, you should put your input elements inside of a form element. Set the following attributes within the form element tag:
<form action='yourfile.php' method='post'>
Then, you can set your $url variable like so:
$url=$_POST["url[]"];
Read more about it here: http://www.w3schools.com/php/php_forms.asp
Try this
<?php
require("PRclass.php");
if($_POST['sample']!="")
{
$url=urlencode($_POST['url']);
$pr = new PR();
echo "$url has Google PageRank: ".$pr->get_google_pagerank($url) ;
}
?>
<form name="sampleform" method="post" action="">
<p>
<input name="url" type="text" id="url" value="" size="80" /><br />
</p>
<p>
<input name="findpr" type="submit" value="Find Google PageRank" />
</p>
<input type="submit" name="sample" value="submit" />
</form>
First of all, you don't need to use a named array for your input box (name="url[]") since a textbox will be holding a single value only.
The second thing to make note of is that you will need a <form> tag in order to let PHP know that you're submitting some information to work with.
HTML
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
<input name="url" type="text" id="url" value="http://" size="80" /><br />
</p>
<p>
<input name="findpr" type="submit" value="Find Google PageRank" />
</p>
</form>
<?php echo $_SERVER['PHP_SELF']; ?> tells the form to post the data to the same page.
PHP
// run the below code only if the form was submitted
if(isset($_POST['findpr'])) {
$url = $_POST['url']; // this is where we capture the user input
// making sure the user enters a valid URL
if(!filter_var($url, FILTER_VALIDATE_URL))
{
echo 'The entered URL is not valid. Please try again';
}
else
{
require("PRclass.php");
$pr = new PR();
echo "$url has Google PageRank: ".$pr->get_google_pagerank($url) ;
}
}

Capture the Search text and pass it in a URL when clicked on Submit

I just cant get around this simple requirement. I am new to PHP and need help.
I need to capture the value in a Search Box and then pass it in the URL which opens in a new tab when hit on Submit.
What am I missing here..it seems like I am missing a lot of things for this to work..
<?php
if (isset($_POST["submit"])){
$example = $_post['searchon'];
echo '<a target = '_blank' href=http://www.amazon.in/s/ref=nb_sb_noss_1?url=search-alias%3Daps&field-keywords=.$example.&tag=orientmarketi-21></a>';
}
?>
<form action="index.php" method="post">
<input type="search" name="searchon" id="searchon" />
<input type="submit" name ="submit" />
</form>
try this code instead of your one:
<form action="http://www.amazon.in/s/ref=nb_sb_noss_1" method="get" target="_blank">
<input type="hidden" name="url" value="search-alias=aps" />
<input type="hidden" name="tag" value="orientmarketi-21" />
<input type="text" name="field-keywords"/>
<input type="submit" />
</form>

More than one html form in a php file

I have a php and html based tool that has a form that, when submitted, outputs the data reformatted using echo commands.
I'd like to add a 2nd form to the same page that will also output using echo.
My issue is, when I submit the 2nd form the first forms output disappears. I'd like to make it so the echo output from the first form does not go away when the 2nd form is submitted so they will both be on the screen at the same time.
Is there a way I can do this?
Only one <form> block in a page can be submitted at a single time. <input> fields defined in one form will not be submitted when the other form is submitted.
e.g.
<form>
<input type="text" name="foo" />
<input type="submit" />
</form>
<form>
<input type="text" name="bar" />
<input type="submit" />
</form>
Clicking on submit will submit either a foo field, OR a bar field. Not both. If you want both fields to be submitted, then you have to either build them into a SINGLE form:
<form>
<input type="text" name="foo" />
<input type="text" name="bar" />
<input type="submit" />
</form>
or use Javascript to copy the data from one form to another.
<form method="post"> <div>Module1</div> <input type="text"
value="module1" name="module_id"> <input type="text" value="title 1"
name="title"> <input type="text" value="some text 1" name="text">
<input type="submit" name="form_1" value="submit"> </form>
<form method="post"> <div >Module2</div> <input type="text"
value="module2" name="module_id"> <input type="text" value="title 2"
name="title"> <input type="text" value="some text 2" name="text">
<input type="submit" name="form_2" value="submit"> </form>
<?php
if(isset($_POST['form_1'])){
echo '<pre>';
print_r($_POST); }
if(isset($_POST['form_2'])){
echo '<pre>';
print_r($_POST); } ?>
Yes,you can do it.
Eg :
// form1 on page a.php
<form method="post" action="a.php" name="form_one" >
<input type="text" name="form_1" value="if(isset($_POST['form_1'])) echo $_POST['form_1']; ?>" >
<input type="submit" name="submit_1" >
</form>
<?php
if(isset($_POST['submit']))
{
?>
<form method="post" action="a.php" name="form_two" >
<input type="text" name="form_2" value="if(isset($_POST['form_2'])) echo $_POST['form_2']; ?>" >
<input type="submit" name="submit_2" >
</form>
<?php
}
?>
Now when you will submit form_one you will see form_two appear and the value in form one will stay intact in form_one and one the submitting form two the value will remain.
Hope it helped :)

html submit button with php after not working

I cannot seem to find an answer to my question on the internet. I can't help but think it is a stupid mistake, however I have gone over my code at least 6 - 7 times and even took a break to look at it with fresh eyes. If someone could look it over for me, that would be super sweet!
<div id="input_alignment" style="font-size:20px">
<?php
if(!($Valid_Profile == true)){ ?>
...Form that has no issues
<?php }
else{ ?>
<form action="Redact_Profiles.php" style="font-size:19px" method="post">
<input type="hidden" value="<?php echo $Valid_Profile ?>" name="Valid_Profile" />
<input type="hidden" value="<?php echo $Username ?>" name="Username" />
<input type="hidden" value="<?php echo $Password ?>" name="Password" />
<br />
   <?php echo $Profile_Username . "<br>"; ?>
<input type="submit" style="font-size:9px" value="Edit" name="" /> <?php echo $Profile_Password . "<br>"; ?>
<br />
<br />
......<br />
<input type="submit" style="font-size:9px" value="Edit" name="" /> <?php echo $Profile_First_Name . "<br>"; ?>
<input type="submit" style="font-size:9px" value="Edit" name="" /> <?php echo $Profile_Last_Name . "<br>"; ?>
   <?php echo $Profile_EMail . "<br>"; ?>
<br />
<input type="submit" style="font-size:9px" value="Edit" name="" /> <?php echo $Profile_Location . "<br>"; ?>
<input type="submit" style="font-size:9px" value="Edit" name="" /> <?php echo $Profile_Referee_Password . "<br>"; ?>
<input type="submit" style="font-size:9px" value="Edit" name="" />
</form>
<?php }
?>
</div>
The very last two submit buttons don't work. When I take away the php containing the $Profile_Location and $Profile_Referee_Password, the submit buttons work.
Sorry, what I mean by Doesn't work means that the submit button isn't responding. The page doesn't re-load, and it acts like the submit button is disabled. The submit button looks enabled, but it is like it doesn't want to "press".
After 2 days of headache I have figured it out: When a submit button is outside the bounds of the height and width of any "div", it will not function like a button, but more like a picture. So as a good practice, when a button is not functioning properly, check to see if it is literally "outside" of the container you put it in.
This error doesn't present itself in viewing the source page, or in any error checking. However, if it does, I don't know about it. I am using a Mac, Safari 6, and CS5 Dreamweaver, if anyone is interested.

Categories