Capture user input from Form - php

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) ;
}
}

Related

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

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

submitting result to the end of the url instead of adding an index.php

Id basically like the below submission to place text at the end of the url
example of what i want
http://example.com/(text) -- without the () obviously
example of what i don't want -- http://www.example.com/index.php?firstname=text
<form action="(end of current url)">
<fieldset>
search name
<br>
<input type="text" name="search" value="name">
<br>
<input type="submit" value="Submit"></fieldset>
</form>
id like to fix this via html or php either will do aslong as it submits the request to that :)
thank you in advance.
Using the POST method instead of GET.
<form action="" method="POST">
<fieldset>
search name
<br>
<input type="text" name="search" value="name">
<br>
<input type="submit" value="Submit"></fieldset>
</form>
In short you do the following:
<?php
// Get posted text
$text = strtolower(mysql_real_escape_string($_POST['text']));
// Do some cleanup here
// Redirect to page
if ($text != ''){
header( 'Location: http://www.example.com/' . $text );
}
// HTML output below (not before)
?>
<form method="post" action="">
<fieldset>
Search name<br />
<input type="text" name="text" /><br />
<input type="submit" value="Submit" />
</fieldset>
</form>

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 :)

How to access the form's 'name' variable from PHP

I'm trying to create a BMI calculator. This should allow people to use either metric or imperial measurements.
I realise that I could use hidden tags to solve my problem, but this has bugged me before so I thought I'd ask: I can use $_POST['variableName'] to find the submitted variableName field-value; but...I don't know, or see, how to verify which form was used to submit the variables.
My code's below (though I'm not sure it's strictly relevant to the question):
<?php
$bmiSubmitted = $_POST['bmiSubmitted'];
if (isset($bmiSubmitted)) {
$height = $_POST['height'];
$weight = $_POST['weight'];
$bmi = floor($weight/($height*$height));
?>
<ul id="bmi">
<li>Weight (in kilograms) is: <span><?php echo "$weight"; ?></span></li>
<li>Height (in metres) is: <span><?php echo "$height"; ?></span></li>
<li>Body mass index (BMI) is: <span><?php echo "$bmi"; ?></span></li>
</ul>
<?php
}
else {
?>
<div id="formSelector">
<ul>
<li>Metric</li>
<li>Imperial</li>
</ul>
<form name="met" id="metric" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="form/multipart">
<fieldset>
<label for="weight">Weight (<abbr title="Kilograms">kg</abbr>):</label>
<input type="text" name="weight" id="weight" />
<label for="height">Height (<abbr title="metres">m</abbr>):</label>
<input type="text" name="height" id="height" />
<input type="hidden" name="bmiSubmitted" id="bmiSubmitted" value="1" />
</fieldset>
<fieldset>
<input type="reset" id="reset" value="Clear" />
<input type="submit" id="submit" value="Submit" />
</fieldset>
</form>
<form name="imp" id="imperial" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="form/multipart">
<fieldset>
<label for="weight">Weight (<abbr title="Pounds">lbs</abbr>):</label>
<input type="text" name="weight" id="weight" />
<label for="height">Height (Inches):</label>
<input type="text" name="height" id="height" /
<input type="hidden" name="bmiSubmitted" id="bmiSubmitted" value="1" />
</fieldset>
<fieldset>
<input type="reset" id="reset" value="Clear" />
<input type="submit" id="submit" value="Submit" />
</fieldset>
</form>
<?php
}
?>
I verified that it worked (though without validation at the moment -I didn't want to crowd my question too much) with metric; I've added the form but not the processing for the imperial yet.
To identify the submitted form, you can use:
A hidden input field.
The name or value of the submit button.
The name of the form is not sent to the server as part of the POST data.
You can use code as follows:
<form name="myform" method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="frmname" value=""/>
</form>
You can do it like this:
<input type="text" name="myform[login]">
<input type="password" name="myform[password]">
Check the posted values
if (isset($_POST['myform'])) {
$values = $_POST['myform'];
// $login = $values['login'];
// ...
}
The form name is not submitted. You should just add a hidden field to each form and call it a day.
In the form submitting button (id method of form is post):
<input type="submit" value="save" name="commentData">
In the PHP file:
if (isset($_POST['commentData'])){
// Code
}
For some reason, the name of the submit button is not passed to the superglobal $_POST when submitted with Ajax/jQuery.
Use a unique value on the submit button for each form like so
File index.html
<form method="post" action="bat/email.php">
<input type="text" name="firstName" placeholder="First name" required>
<input type="text" name="lastName" placeholder="Last name" required>
<button name="submit" type="submit" value="contact">Send Message</button>
</form>
<form method="post" action="bat/email.php">
<input type="text" name="firstName" placeholder="First name" required>
<input type="text" name="lastName" placeholder="Last name" required>
<button name="submit" type="submit" value="support">Send Message</button>
</form>
File email.php
<?php
if (isset($_POST["submit"])) {
switch ($_POST["submit"]) {
case "contact":
break;
case "support":
break;
default:
break;
}
}
?>
As petervandijck.com pointed out, this code may be susceptible to XSS attacks if you have it behind some kind of log-in system or have it embedded in other code.
To prevent an XSS attack, where you have written:
<?php echo "$weight"; ?>
You should write instead:
<?php echo htmlentities($weight); ?>
Which could even be better written as:
<?=htmlentities($weight); ?>
You can use GET in the form's action parameter, which I use whenever I make a login/register combined page.
For example: action="loginregister.php?whichform=loginform"
I had a similar problem which brought me to this question. I reviewed all the preceding answers, but ultimately I ending up figuring out my own solution:
<form name="ctc_form" id="ctc_form" action='' method='get'>
<input type="hidden" name="form_nm" id="form_nm">
<button type="submit" name="submit" id="submit" onclick="document.getElementById('form_nm').value=this.closest('form').name;">Submit</button>
</form>
It seamlessly and efficiently accomplishes the following:
Passes the form name attribute via a hidden input field, without using the fallible value attribute of the submit button.
Works with both GET and POST methods.
Requires no additional, independent JavaScript.
You could just give a name to the submit button and do what needs to be done based on that. I have several forms on a page and do just that. Pass the button name and then if button name = button name do something.
Only the names of the form fields are submitted, but the name of the form itself is not. But you can set a hidden field with the name in it.

Categories