form variables not passing to php - php

I have a simple form and I'm trying to pass the form variable to php and output the value. I have tried to solve this myself with the almighty google but wasn't successful. The html form code is as follows
<html>
<head>
<title>Form</title>
</head>
<body>
<form method="post" action="test1.php">
<input type="text" name="username">
<input type="submit">
</form>
</body>
</html>
Then the php to handle the form is:
<html>
<head>
<title>Form</title>
</head>
<body>
<?php
echo "<h1>Hello " . $_POST["username"] . "</h1>";
?>
</body>
</html>
The output I'm getting no matter what I type into the html form is , Hello " . $_POST["username"] . ""; ?>
I don't know why it also outputs the ending semi colon and ending php tag also, maybe this is evidence of what's going wrong here?

PHP is
misconfigured or
not configured or
not working or
not working within HTML files.
You have to configure your webserver that it parses PHP inside HTML files, see here for example:
=> Server not parsing .html as PHP

The syntax is fine, try to write a sample code snippet like below
<?
$a = "hello word!";
echo $a;
?>
and check if php is working.

Related

PHP Form Pull From Remote XML

I have a php form that users submit their start / end dates. The form then needs to pull the results from .xml hosted on a different URL. The URL varies based on the dates entered by the user - dates become part of the URL as you can see below.
So, simply put... The user enters the dates. It calls the xml file via the URL (which changes slightly as the dates are entered into that URL) and it displays the results on a new page.
I would rather do it via PHP than AJAX if possible.
This is the frontend of my form:
<html>
<body>
<form action="test_get.php" method="get">
Start Date: <input type="text" id="start" name="start"><br>
End Date: <input type="text" id="end" name="end"> <br>
<input type="submit">
</form>
</body>
</html>
This is 'test_get.php':
<html>
<body>
<form onSubmit="return process();">
Start Date: <?php echo $_GET["start"]; ?><br>
End Date: <?php echo $_GET["end"]; ?>
</form>
</body>
<script>
function process()
{
var url="http://99.999.999.999:81/fmi/xml/fmresultset.xml?-db=Front_Desk&-lay=WebRoomQuery&-findany&-script=WebQueryPSOS&-script.param=snug," + document.getElementById("start").value + "," + document.getElementById("end").value;
location.href=url;
return false;
}
</script>
</html>
But it doesn't work. I also tried this:
<html>
<body>
<?php
$xml=simplexml_load_file("http://99.999.999.999:81/fmi/xml/fmresultset.xml?-db=Front_Desk&-lay=WebRoomQuery&-findany&-script=WebQueryPSOS&-script.param=snug," + document.getElementById("start").value + "," + document.getElementById("end").value"); or die("Error: Cannot create object");
echo $xml->field . "<br>";
echo $xml->data . "<br>";
?>
</body>
</html>
But again, no luck. Can anyone see what i'm missing?
I can also share what my .xml form looks like if needed. Any help is greatly appreciated - i've been tackling this for days! Thanks! :)
UPDATE: This is my updated code for test_get.php:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<?php
$url = '"http://69.239.118.197:81/fmi/xml/fmresultset.xml?-db=Front_Desk&-lay=WebRoomQuery&-findany&-script=WebQueryPSOS&-script.param=snug,'.$_GET['start'].','.$_GET['end'].'"';
$xml = simplexml_load_file($url);
echo $xml->field . "<br>";
echo $xml->data . "<br>";
echo var_dump($xml). "<br />";
?>
</body>
</html>
In the last block of code that you posted, you are using JavaScript outside of script tags, so this is definitely not going to work. Have you tried instead putting your $_GET variable into the query string?
Note* I pulled out the query string for more readability. It's hard for me to verify that this is working for you outside of your environment, but give it a shot.
You've got this:
$xml=simplexml_load_file("http://99.999.999.999:81/fmi/xml/fmresultset.xml?-db=Front_Desk&-lay=WebRoomQuery&-findany&-script=WebQueryPSOS&-script.param=snug," + document.getElementById("start").value + "," + document.getElementById("end").value"); or die("Error: Cannot create object");
I'm recommending this:
$url = '"http://99.999.999.999:81/fmi/xml/fmresultset.xml?-db=Front_Desk&-lay=WebRoomQuery&-findany&-script=WebQueryPSOS&-script.param=snug,'.$_GET['start'].','.$_GET['end'].'"';
$xml = simplexml_load_file($url);
*************EDIT*****************
So, after attempting to manipulate the data that simplexml_load_file returns I decided to try file_get_contents. The solution below returns the data value from the xml file in string format.
$_GET['start'] = '08/30/2017';
$_GET['end'] = '08/31/2017';
$xml = file_get_contents("http://69.239.118.197:81/fmi/xml/fmresultset.xml?-db=Front_Desk&-lay=WebRoomQuery&-findany&-script=WebQueryPSOS&-script.param=snug,".$_GET['start'].','.$_GET['end'].'"');
echo $xml;

Asp classic page to php

I have this url in classic asp:"http:/blablabla/123.asp?id=1293". My question, is there a way to pass the id number to a external php script without using the form and submit, or that is only way to do it??
My data.php script:
<?php
$xxx = $_POST["xxx"];
//... some code
$gantt->render_complex_sql("SELECT * FROM gantt_tasks WHERE something = '".$xxx."'" , "id", "start_date,duration,text,progress,parent","");
?>
My "123.asp?id=1293" page:
<html>
<head>
// .. scripts and title
</head>
<body>
<form id="form1" method="post" action="data.php">
<input type="hidden" name="xxx" value="Request.QueryString("id")" />
</form>
<div id="gantt_here" style='width:100%; height:100%;'></div>
<script type="text/javascript">
gantt.init("gantt_here");
gantt.load("data.php");
</script>
</body>
</html>
And i dont want to use the "Response.Redirect" page either.
You can do this:
UPDATE:
gantt.load('data.php?id=<%= Request.QueryString("id") %>');
But it sounds very strange using old asp files and php files in the same project.

php not functioning inside html file but works outside

I have been following the php tutorial here
CODE
Here is my html file:
<!DOCTYPE html>
<html>
<head>
<link rel=”stylesheet” type=”text/css” href=”style.css”>
<form action="postForm.php" method="post">
<TextArea name="microBlog" id="microBlog" cols="30" rows=“10"></TextArea>
<input type="submit">
</form>
</head>
<body>
<?php
require_once 'meekrodb.2.3.class.php';
DB::$user = 'root';
DB::$password = '';
DB::$dbName = 'MicroBlog';
$results = DB::query("SELECT post FROM MicroBlog");
foreach ($results as $row){
echo "<div class='microBlog'>" . $row['post'] . "</div>";
}
?>
</body>
</html>
This yields the following:
However if I copy the php code into a new postForm.php file and click "Submit" (as you can see the action is postForm.php), it works.
I get my blank screen with 3 words (from the database).
The problem is it is a brand new blank page and I do not want that.
PROBLEM
Why is it that the code works outside the html file, but not inside the html file. Why do I get ".row['post']."";} ?> when inside the html file but I get perfect output when the php exists in its own php file?
There is clearly nothing wrong with the code, so what could it be?
It really confuses me. Thanks for any answers.
Change your file extension .html into .php or .phtml. It will solve your problem.
You are writing a php code inside html file. html file doesn't evaluate php code.change the extension of file to .php instead of .html by doing so you write both html and php code inside that file.
Reason: 1. An html file does not support php scripts inside it and thus anything written will not be executed and will only be treated as an html markup.
Solution:
1. Just save the .html file as .php file and you are done!(very simple).For example if your file name is index.html, just save it as index.php and all the php scripts inside will be executed.
index.php:
<!DOCTYPE html>
<html>
<head>
<link rel=”stylesheet” type=”text/css” href=”style.css”>
<form action="postForm.php" method="post">
<textArea name="microBlog" id="microBlog" cols="30" rows=“10"></textArea>
<input type="submit">
</form>
</head>
<body>
<?php
require_once 'meekrodb.2.3.class.php';
DB::$user = 'root';
DB::$password = '';
DB::$dbName = 'MicroBlog';
$results = DB::query("SELECT post FROM MicroBlog");
foreach ($results as $row){
echo "<div class='microBlog'>" . $row['post'] . "</div>";
}
?>
</body>
</html>

How to print submitted form values?

why i cant view what i fill up in my html form. is that something wrong with my code in php code?
<html>
<head>
<title>What's your name?</title>
</head>
<body>
<h1>What's your name?</h1>
<h3>Writing a form for user input</h3>
<form method = "post" action = "User.php">
Please type your name:
<input type = "text" name = "userName" value = " "><br>
<input type = "submit">
</form>
</body>
</html>
Code:
<html>
<head>
<title>Hi User</title>
</head>
<body>
<h1>Hi User</h1>
<h3>PHP program that receives a value from "whatsName"</h3>
<?
print("<h3>Hi there , $userName </h3>");
?>
</body>
</html>
Unless you have Register Globals on (which you shouldn't so turn it off if so), the form variables won't automatically be expanded, so you need to pick them up from the $_POST array:
if($_SERVER['REQUEST_METHOD'] == 'POST'){
print("<h3>Hi there , " . htmlspecialchars($_POST['userName']) . "</h3>");
}
Maybe you have been used to badly configured servers running with register_globals turned on.
Or maybe you have moved to a version of PHP where register_globals has been removed i.e. PHP5.4 or greater.
You should address any data coming from a HTML <form> using the proper
$_POST['variableName']
or
$_GET['variableName']
With that in mind your code might look like this
print('<h3>Hi there , ' . $_POST['userName'] . '</h3>');
Note: You should really be sanity checking the values passed in this type of data, and also checking if it is actually there. Although you should have been doing that anyway even if register_globals was turned on.

PHP Pass variable up the page

I came across someone asking the question,
How can I pass a variable up the page (on the same page?).
I had a think about it but couldn't think of how to do it myself, so I was wondering if it is even possible?
So what he was trying to do was change the value of $a at the top of the page at the same time as the bottom value of $a.
Is it possible? If so how?
Thanks in advanced.
<?php
echo("Begining " . $a);
?>
<html>
<head>
<title>Test Variables</title>
</head>
<body>
<form>
<form action="<?=$PHP_SELF?>" method="POST">
<input type="TEXT" name="testf" size="10">
<input type="submit" name = "submit" value = "submit"></form><?php
if ("submit" == $submit) {
$a = $testf;
echo( "Bottom " . $a);
}
?>
</body></html>
Edit:
After seeing answers, maybe it can be done with jquery, ajax or javascript?
No, you'll need to move the if statement to the top, and any variables you calculate that need to be in the if statement also to the top.
Something like:
<?php
if ("submit" == $submit) {
$a = $testf;
}
echo("Begining " . $a);
?>
<html>
<head>
<title>Test Variables</title>
</head>
<body>
<form>
<form action="<?=$PHP_SELF?>" method="POST">
<input type="TEXT" name="testf" size="10">
<input type="submit" name = "submit" value = "submit"></form><?php
echo( "Bottom " . $a);
?>
</body></html>
It's considered a good practice to have all the logic before you start outputting the HTML anyways. Your HTML should ideally have as less logic as possible.
More info: https://stackoverflow.com/a/95027/320615 and https://stackoverflow.com/a/1088791/320615
You can probably bend over backwards to make that work somehow.
But the real answer is to handle all your business logic before you start outputting any HTML. You need to decide at the beginning of your code whether the current request is a form submission or not and set variables and HTML templates accordingly. Never mix business logic into the middle of your HTML templates.
You have to use the $_POST['testf'] and $_POST['submit'] variable.
And also check if it exists with isset : php manual isset
<?php
echo("Begining " . $a);
?>
<html>
<head>
<title>Test Variables</title>
</head>
<body>
<form>
<form action="<?=$PHP_SELF?>" method="POST">
<input type="TEXT" name="testf" size="10">
<input type="submit" name = "submit" value = "submit"></form><?php
if ("submit" == $submit) {
$a = $_POST['testf'];
echo( "Bottom " . $a);
}
?>
</body></html>
You can't do it in PHP, but you can't change HTML once it's fully loaded using javascript (and jquery to make it easier).
EDIT : ok I read your code a bit quickly the first time :
I don't understand the echo before the <html> tag, doesn't seem right. Also you want to give the value of a POST var to $a, so just :
if(isset($_POST['testf'])) {
echo $_POST['testf'];
}

Categories