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

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>

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

Fetch value of a input type and store in variable

I have following input type
<input id="crp-project-title" class="crp-project-title" type="text" placeholder="Enter project title" value="EXTERIOR" name="project.title">
Its value is value="EXTERIOR". I want to fetch the value and store in a variable. I am not getting any idea regarding this. I am trying to modify plugin (career portfolio) according to my project and its part of that.
Your html form:
<form method="POST" action="process.php">
<input type="text" name="foo">
<input type="submit">
</form>
Your Php form processing page (process.php):
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$foo = filter_input(INPUT_POST, 'foo', FILTER_SANITIZE_STRING);
}
?>
This is front end code, if you are trying to move front end data to server side it must pass through the server. i.e. $_POST...
now you can either do this with AJAX / PHP or PHP / HTML only
<?php
if(isset($_POST[project.title])
$var = $_POST[project.title]; //its now in a varible
?>
<form method="POST" action="thispage.php">
<input id="crp-project-title" class="crp-project-title" type="text" placeholder="Enter project title" value="EXTERIOR" name="project.title">
<input type=submit value="PRESS ME TO SUBMIT VALUE TO VAR">
</form>

Is POST method used to send user defined inputs only

Well, I have 2 php files. The first one user form.php has an html form to collect user inputs and send those data to other php file mysql_insert.php via POST method.
The problem is that along with 6 user inputs, I also have to transfer one more variable count(as discussed in code below) which is not input from user.
I tired following approach:
user form.php--->
<?php
$count=file_get_contents("cnt.txt","r");
$count=$count+1;
echo"Welcome!<br/>You are student number $count.<br/>";
?>
<html>
<p>Fill in the following information to save your marksheet to the database:</p><br/>
<form action="mysql_insert.php" method="POST">
Name:<input type="text" name="name" value=""/><br/><br/>
Marks(out of 100)<br/>
Subject 1:<input type="text" name="sub1" value=""/><br/>
Subject 2:<input type="text" name="sub2" value=""/><br/>
Subject 3:<input type="text" name="sub3" value=""/><br/>
Subject 4:<input type="text" name="sub4" value=""/><br/>
Subject 5:<input type="text" name="sub5" value=""/><br/><br/>
<p name="count" value="$count"></p>
<input type="submit" value="Submit"/>
</form>
</html>
I simply named an empty P element as count and set its value as $count to send it along with input variables(correct me if there is something wrong here, I am very novice to php).
And on the receiving end I used following code:
mysql_insert.php--->
<?php
require("connect.php");
$name=$_POST['name'];
$s1=$_POST['sub1'];
$s2=$_POST['sub2'];
$s3=$_POST['sub3'];
$s4=$_POST['sub4'];
$s5=$_POST['sub5'];
$count=$_POST['count'];
.
.
.
?>
Now on the line $count=$_POST['count']; the browser is throwing the error:
Notice: Undefined index: count in C:\xampp\htdocs\Vikas-117-PHP\level 3\mysql_insert.php on line 10
It seems the count is not being posted to this file.
Please guide me where I am wrong.
P.S.: I can of course use the file_get_contents() in the mysql_insert.php and get the count value directly in this file, but I am considering that way as my last option.
So please help if the non user-input variable can be posted via forms???
Thanks a million!!!
you have to put your $count variable in a hidden field in your form.. The data which are put in form inputs only get posted in form submit..
<input type="hidden" name="count" value="<?php echo $count ?>">
<p name="count" value="$count"></p>
you can not use directly any html tag for post data only use html form fields for posting data.
for use html tag values post you need to use js/ajax/php
<p name="count"><?php echo $count;?></p>
or better you use
<textarea name="count"><?php echo $count;?></textarea>
or for data not showing use hidden field
and get by name this p value or use class or id for get and post data using ajax
<p name="count" value="$count"></p>
This is not an input element so you cant post directly. You can however use jquery to get the value and POST using js.
You need to have the value inside input type text or hidden if you want to do the way you are doing now.
Change this line : <p name="count" value="$count"></p>
to the : <input type="hidden" name="count" value="<?=$count?>" />
You can use hidden elements like the following
use
<input type="hidden" value="<?php echo $count; ?>" name="count"/>
instead of
<p name="count" value="$count"></p>
Instead of
<p name="count" value="$count"></p>
Use
<input type="hidden" name="count" value="<?php echo $count;?>">
this line will not work : <p name="count" value="$count"></p>
you can use <input type="hidden" name="count" value="<?php echo $count; ?>" />

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 to call php variable input text from another Php page

My question is how do I call input value of user selected which has show by php code from another php.
Normal simple way we get the input this way
$calendar_id_val = $_GET['calendar_id'];
and now it is not working:
For example Show.php, I have one form which show the values from Database and show the result with php variable with While Loop.
<input name="calendar_id" value="<?php echo $calendar_id;?>">
and when user is submit that form I will carry these user selected value and perform insert.php
While you are doing echo in the input use echo $calendar_id_val
You should use form like,
<form action="insert.php" method="get">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>"/>
<input type="submit" name="submit_id" value="Insert"/>
</form>
Insert.php
echo $calendar_id_val = $_GET['calendar_id'];
Does this answer your question?
<form action="insert.php" method="GET">
<input name="calendar_id" value="<?php echo $calendar_id;?>">
</form>
If you want to redirect the user back to show.php, add this to the end of your insert.php script
header('Location: show.php');
exit();
I suggest $_POST var like this:
<form action="insert.php" method="POST">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>" />
<input type="submit" name="submit" value="Submit" />
</form>
insert.php file:
echo $calendar_id = $_POST['calendar_id'];

Categories