So the goal is to take the input from a form append it to the end of the URL and then return the HTML from that page.
I am not entirely sure how to take the forms value (in this case $2) and attach it to the URL.
<html>
<head>
<title>Metar Test</title>
</head>
<body>
<form action="" method="POST">
<p>IACO Code: <input type="text" name="$2" value=""></p>
<input type="submit" name="submit" value="Submit">
</form>
<?php $html=file_get_contents("http://www.metar.mysite.net/metar?id=$2")?>
<?php echo $_POST ["$html"];?>
</body>
</html>
On submit any input from a form with method="POST" will be stored in the $_POST global. You can access it from there and append it to your URL string.
<html>
<head>
<title>Metar Test</title>
</head>
<body>
<form action="" method="POST">
<p>IACO Code: <input type="text" name="iacoCode" value=""></p>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST["iacoCode"])) {
$html = file_get_contents("http://www.metar.mysite.net/metar?id=" . $_POST["iacoCode"]);
echo $html;
}
?>
</body>
</html>
Using the IF statement to check if it is set will prevent it from loading the URL with no variable.
The way we extract data via php from a form is like below
<html>
<head>
<title>Metar Test</title>
</head>
<body>
<form action="" method="POST">
<p>IACO Code: <input type="text" name="$2" value=""></p>
<input type="submit" name="submit" value="Submit">
</form>
<?php $html=file_get_contents("http://www.metar.mysite.net/metar?id=$_POST['$2']")?>
<?php echo $_POST ["$html"];?>
Related
index.php
<html>
<head>
<script type="text/javascript">
function submitForms()
{
document.forms["form-1"].submit();
document.forms["form-2"].submit();
}
</script>
</head>
<body>
<form method="POST" action="form.php" id='form-1'>
<input type="text" name="txt1" />
</form>
<form method="POST" action="form.php" id='form-2'>
<input type="text" name="txt2" />
</form>
<input type="button" value="Click Me!" onclick="submitForms();" />
</body>
</html>
form.php
<?php
echo $_POST['txt1'];
echo $_POST['txt2'];
?>
Above is my code and when i submit both forms then both text-fields with their value it does not shoe me both text-field values.It only shoe me second text-field value.Please help me quickly.
I think because you try to get the params after sumbit two forms. You have sent the two forms at once and the second has stepped to the first, so the result is the return of the second form.
I think this will be better:
<html>
<head>
</head>
<body>
<form method="POST" action="form.php">
<input type="text" name="txt1" />
<input type="text" name="txt2" />
<input type="submit" value="Click Me!" />
</form>
</body>
</html>
<?php
echo $_POST['txt1'];
echo $_POST['txt2'];
?>
Sorry for my english
I am trying to get user input from a text box and then echo it using php. Here is my code, and it is not seeming to work.
<html>
<body>
<?php
echo $_POST['value'];
?>
<form method="post" action="">
<input type="text" name="value">
<input type="submit">
</form>
</body>
</html>
<html>
<body>
<?php
if(!empty($_POST['value']))
{
echo filter_var($_POST['value'], FILTER_SANITIZE_STRING);}
?>
<form method="post" action="">
<input type="text" name="value">
<input type="submit">
</form>
</body>
</html>
First check if form posted.
<!doctype html>
<html>
<head>
<body>
<form action="question.php" method="post">
<input type="hidden" name="session" value="12" />
</form>
</body>
</html>
<?php
header('Location: question.php');
?>
question.php File...
<?php
$rr = $_POST['session'];
echo $rr;
?>
it should print 12 right?
But i get this error "Notice: Undefined index: session in C:\wamp\www\Project\question.php on line 3".
what is the problem here?
thanks...
This will echo your value once you click the submit button.
question.php File
<?php
$rr = $_POST['session'];
echo $rr;
?>
HTML current page
<!doctype html>
<html>
<head>
<body>
<form id="myForm" action="question.php" method="post">
<input type="hidden" name="session" value="12" />
<input type="submit" value="submit" />
</form>
</body>
</html>
// this will auto submit your form using javaScript
<script type="text/javascript">
document.getElementById("myForm").submit();
</script>
But what if i want to submit the from automatically without a submit button:
jsfiddle
<form action="question.php" method="post">
<input type="hidden" name="session" value="12" />
<input type="submit" value="submit" id="sub"/>
</form>
jQuery:
$(function(){
$('#sub').click();
});
wow, pretty complicated... all you needed to do was add
<?php
session_start();
// session_start has to be the first line
// of your php and before any HTML code
?>
<html>
<body>
</body>
</html>
<!doctype html>
<html>
<head>
<body>
<form action="question.php" method="post">
<input type="hidden" name="session" value="12" />
<input type="submit" name="send" value="send">
</form>
</body>
</html>
question.php:
<?php
# prints all post elements
print_r($_POST); // for method="post"
echo $_POST['session']; # only the hidden input named "session"
Here's my code in a short way. This code can't access post and I don't know why.
if ($_POST) {
echo 'post a girdim';
}
?>
<html>
<head>
</head>
<body>
<form action="" method="post">
<input type="submit">
</form>
</body>
You don't have any elements in your form with a name. Therefore no values are submitted to the server, the $_POST array is empty and if ($_POST) is false.
If you don't want any named elements, you can check whether the request method in $_SERVER['REQUEST_METHOD'] is "POST".
set input name like:
<input name="submit" type="submit" />
Give your input tags names.
if (isset($_POST["submit"])) {
echo 'post a girdim';
}
?>
<html>
<head>
</head>
<body>
<form action="" method="post">
<input type="submit" name="submit">
</form>
</body>
you should follow this
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
and further
you can give name for input
for example
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" name="submit">
</form>
and then you must check this
<?php
extract($_POST);
if (isset($_POST['submit'])) {
echo $_POST['submit'];
}
?>
I am new to PHP (in-fact I am learning it). I am trying to get the value of textbox defined in HTML and print it on HTML page through PHP code. I am able to send the value of textbox to another page using form post and getting it using $_POST['name'] on other page.
I am not able to print the value of textbox on same page.
Here is my code:
<html>
<head>
<?php
function myfunction()
{
printf($_POST['fname']);
}
?>
</head>
<body>
Name: <input type="text" name="fname" />
<input type="button" onClick="myfunction()" value="Click" />
</body>
</html>
First of all it's not required to write php function within <head> tags. Second,
you can't POST data without <form> tags. try this
<?php
if(isset($_POST['submit']) && $_POST['submit']=='Submit'){
$name=$_POST['fname'];
echo $name;
}
else {
?>
<html>
<head>
</head>
<body>
<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">
Name: <input type="text" name="fname" />
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
<?php } ?>
you can not call PHP function by button's click event...
if you want to print on same page then you can do by below code..
<html>
<?php
if(count($_POST)>0){
echo $_POST['fname'];
}
?>
<body>
<form method='post'>
Name: <input type="text" name="fname" />
<input type="submit" value="Click" />
</form>
</body>
</html>
hope you will get what you want by my answer..