Need clarity on basics of PHP [duplicate] - php

This question already has answers here:
Can someone tell me the difference between $_REQUEST and the other 2 method($_GET,$_POST) for php [duplicate]
(1 answer)
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
I have gone through some tutorials in W3Schools and others on HTML and PHP but I need a little clarity on how stuff works. I am trying to transport a value via URL using PHP from page1 to page2. Although I have a fair bit of idea on how to do it using HTML
Page1:
<form method="post" action="page2.php">
FIRST NAME <input type="text" name="fname"><br>
LAST NAME <input type="text" name="lname"><br>
<input type="submit" name="sub" value="sub">
</form>
Page2:
<?php
$fname=$_POST['fname'];
$lname=$_POST['lname'];
echo $fname."-".$lname;
?>
I want to use only PHP to transport values, I was planning to use a tag without using a form but I am sure it is not the right way because I won't be able to fetch values on page 2. Plz someone help me out clear these basics.
<?php
$name="hello world";
echo '<input type="button name="sub" value="submit">';
?>

You're going about it backwards. POST data goes in the body of the request, not in the URL. GET data goes in the URL.
If you want to have the data in the URL, you would change your form tag to:
<form method="get" action="page2.php">
and the page2.php code to:
<?php
$fname = $_GET['fname'];
$lname = $_GET['lname'];
echo $fname . "-" . $lname;
?>
Please note that this has a number of vulnerabilities (such as XSS) and is not how you should do these things.

You have to use 'GET'.so your data pass With Append into URL.
Page 1
<form method="GET" action="page2.php">
FIRST NAME <input type="text" name="fname"><br>
LAST NAME <input type="text" name="lname"><br>
<input type="submit" name="sub" value="sub">
</form>
Page 2
<?php $fname=$_GET['fname']; $lname=$_GET['lname']; echo $fname."-".$lname; ?>

Related

How to send a get form wthout losing get parameters

I have a form located at a url containing get parameters,my form is also using this method.When the form is submitted it rewrites the previos get parameters.
Is there a simple way to rewrite only my form parameters?
I have in mind a Javascript solution ,however I want to know if there is a simpler way?Using HTML/PHP perhaps?
As far as I know, u u are not interested in using JS, then using form's hidden element is only way u have like this-
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<input type="hidden" name="country" value="Norway">
<input type="submit" value="Submit">
</form>
<p>Notice that the hidden field above is not shown to a user.</p>
The question is how u can use it with PHP, right?
The solution is here-
//In PHP
if( isset($_GET['fromPerson']) )
{
echo $fromPerson;
}
So combined HTML and PHP code will be like this (assuming a get element from prevous page is named fromPerson)-
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<?php
if( isset($_GET['fromPerson']) )
{
echo '<input type="hidden" name="country" value=".$_POST['fromPerson'].">';
}
?>
<input type="submit" value="Submit">
</form>
Lets say you get a parameter p1 from a get request, it should look like this:
http://server.com/?p1=123
In your form, you can add hidden fields that would have the same effect when you submit, like this:
<form method="GET">
<input type="hidden" value="<?php echo $_GET["p1"]; ?>" name="p1">
</form>
That way you can resend the variables as many times as you need.
I'm not sure I understand your question... Can you post your code?
I assume you mean something like this?
in index.php
<input type="hidden" name="id" value="<?php echo $id; ?>" />
in return.php
Edit

Need to pass a variable in anchor tag

I am very new to php, I am having a problem passing data from textbox to a php variable to use it in an anchor tag. Below is the code i am using.
<form id="searchform" action="fetchvalues.php" method="get">
<input name="q" id="q" type="text" />
<input name="searchbutton" id="go" type="submit" value="" />
</form>
I want to pass value from searchbutton to anchor tag
Hello"
If you are using jquery in your project and want to do this on the front end, you can do:
<a id="someLink" href="http://www.example.com/?q=var" target="_blank">Hello"</a>
$('#someLink').attr('href', "http://www.example.com/?q=" + $('q').val());
With php you'd only be able to set the entered value of q on a post. (meaning when someone submits the form)
i.e.
Hello"
IF you need to populate the link href without a page refresh, you'll need to use javascript, if you want it to be populated after a form post, you can use php.
Be aware though that the link would need to be on the page set in your forms action attribute to populate the link
You should be aware that you take precautions when echoing out form submissions, however the level of questions suggests you've got more to learn before that. (No offense intended)
<form id="searchform" action="fetchvalues.php" method="get">
<input name="q" id="q" type="text" />
<input name="searchbutton" id="go" type="submit" value="" />
</form>
On your fetchvalues.php page
<?php
$val = $_GET['q'];
?>
then
Hello
You can do like this
<?php
if(isset($_REQUEST['searchbutton']))
{
?>
Hello
<?php
}
?>

undefined index 'username' in /var/www/html/test/foo.php on line 2 [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
Recently started learning php and am trying to get the get method to work.
This is the html form I am testing on:
<!DOCTYPE html>
<html>
<body>
<form action="foo.php" method="get">
Name: <input type="text" name="username" /><br />
Email: <input type="text" name="email" /><br />
<input type="submit" name="submit" value="Submit me!" />
</form>
</body>
</html>
I researched and found that you should use isset() to check if the $_GET array contains the value. So I tried(this is foo.php):
<?php
if(isset($_GET['username']))
{
echo $_GET['username'];
}
else
{
echo "username not passed <br />";
}
if(isset($_GET['email']))
{
echo $_GET['email'];
}
else
{
echo "email not passed";
}
?>
It seems something is wrong with trying to pass the values via the get method, I tried the exact same thing with post, replacing method="get" to method="post" and changing $_GET to $_POST in the php file. This worked perfectly with the post method. I know you shouldn't pass sensitive information like email etc via the get method but i am just trying to test it out.
This is the output I get after I submit the form:
username not passed
email not passed
What happens when you try this method?
if(array_key_exists('username', $_GET))
echo $_GET['username'];

What is the difference between POST and GET in HTML/PHP [duplicate]

This question already has answers here:
When should I use GET or POST method? What's the difference between them?
(15 answers)
What is the difference between POST and GET? [duplicate]
(7 answers)
Closed 9 years ago.
I am coding a PHP script, but I can't really seem to get it to work. I am testing out the basics, but I don't really understand what GET and POST means, what's the difference? All the definitions I've seen on the web make not much sense to me, what I've coded so far (but since I don't understand POST and GET, I don't know how to get it to work:
<form name="mail_sub" method="get">
Name: <input type="text" name="theirname"> <br />
Email: <input type="text" name="theirpass"> <br />
<input type="submit" value="Join!" style="width:200px">
</form>
<?php
if (isset($_POST['mail_sub']))
{
echo $_POST['theirname'];
}
?>
$_POST isn't working for you because you have the form method set to get.
<form name="mail_sub" method="post">
There's plenty of better info online as to the difference between post and get so I won't go into that but this will fix the issue for you.
Change your PHP as well.
if ( isset( $_POST['theirname'] ) ) {
echo $_POST['theirname'];
}
To answer the question:
GET and POST are one of the many request types in the standards of internet.
The difference is that GET can't post data, parameters will be appended to the url (url-parameters) which has it's limitations. POST does post parameters/data.
The standard is:
GET for getting data.
POST for creating data.
PUT for updating data.
DELETE for removing data.
replace
isset($_POST['mail_sub'])
by
isset($_POST['theirname'])
The main difference between GET and POST requests is that in GET requests all parameter are part of the url and the user sees the parameters. In POST requests the url is not modified and all form parameter are hidden from the user. If you have no file uploads or very long field parameter use GET. Use POST when going in production.
Post is like "sending" the data to the page without giving the user having any interaction with it, when Get shows the parameters in the URL and making it public for the user. Get is more often used on "unsecure" type of datatransactions like for example a searchform and POST is used when you want to make more secure things like a login form.
Using a Get will give you like
index.php?parameter=hello&parameter2=world
In your example you should use either POST in the method attribute in the formtag or $_GET in the php section
So either
<form name="mail_sub" method="post">
Name: <input type="text" name="theirname"> <br />
Email: <input type="text" name="theirpass"> <br />
<input type="submit" value="Join!" style="width:200px">
</form>
<?php
if (isset($_POST['theirname']))
{
echo $_POST['theirname'];
}
?>
or
<form name="mail_sub" method="get">
Name: <input type="text" name="theirname"> <br />
Email: <input type="text" name="theirpass"> <br />
<input type="submit" value="Join!" style="width:200px">
</form>
<?php
if (isset($_GET['theirname']))
{
echo $_GET['theirname'];
}
?>
GET - If form method is GET then on submitting the form, Form will be submitted to
xyz.com/submit.php?theirname=john&theirpass=password // CHECK HERE - form elements are in URL so not so secure
and can be checked by PHP
as
if(isset($_GET['their']))
{
$name=$_GET['their']; //name will return John
}
POST - If form method is POST then on submitting the form, Form will be submitted to
xyz.com/submit.php // CHECK HERE form elements are not in URL
and can be checked by PHP
as
if(isset($_POST['their']))
{
$name=$_POST['their']; //name will return John
}
you used method as GET in form,so your code should be like this
<?php echo $_SERVER['PHP_SELF'];?>//the form and action will be same page
<form name="mail_sub" method="get" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="theirname"> <br />
Email: <input type="text" name="theirpass"> <br />
<input type="submit" value="Join!" style="width:200px">
</form>
<?php
echo $_GET['theirname'];
?>
Replace Your Code:
<form name="mail_sub" method="post">
Name: <input type="text" name="theirname"> <br />
Email: <input type="text" name="theirpass"> <br />
<input type="submit" name="submit" value="Join!" style="width:200px">
</form>
<?php
if (isset($_POST['submit']))
{
echo $_POST['theirname'];
}
?>

How do I retrieve a PHP variable in my HTML code?

I call a PHP script from my HTML form submit.
Then I process the values retrieved from this HTML form in the PHP script and now I want to display these values in another HTML form that I am calling from this PHP script.
How do I retrieve this variable?
Note: I tried echo but I think I actually want to display this value in the form (HTML) and not break it again in a PHP tag.
I'm not sure what you mean by "not breaking it again with a PHP tag". HTML on its own cannot access PHP variables. This is because PHP is a server-side language and HTML is a client side language. But here is the simplest way to print php variables retrieved from a form.
<form>
<p>Your name: <?php echo $_POST['name'] ?></p>
</form>
This is assuming that there was a form that submitted a field called 'name' using POST.
You can process the name in a php script at the top of the file and then simply echo it when you're printing the html. This way, you won't have too much php code mixed in with the HTML (which makes it look cleaner).
Once you got the values in the PHP script, are you calling a new script? If so, you might wanna save the values in $_SESSION["varible_name"]. If not, you just have to echo it.
It depends on how you are accessing your form data, either through $_POST or through $_GET. For simplicity, I'll assume your using $_GET and modify this example for more clarity.
So lets say you have a form hosted on welcome.php:
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
Now the results will be returned back to the same page, so you want to modify the page to say:
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" value="<?php echo $_GET["fname"]; ?>"/>
Age: <input type="text" name="age" value="<?php echo $_GET["age"]; ?>" />
<input type="submit" />
</form>
Though you'll notice that we're using the same page, and we can only have one version of the page, so we want to render if upon the condition that our variable has been set.
if (isset($_GET["fname"])){
//code to print second form
}
else{
//code to print first form
}
Or, in another way (using the ternary operator):
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" value="<?php echo ((isset($_GET["fname"]))?$_GET["fname"]:""); ?>"/>
Age: <input type="text" name="age" value="<?php echo ((isset($_GET["age"]))?$_GET["age"]:""); ?>" />
<input type="submit" />
</form>

Categories