I am writing a PHP code in Adobe Dreamweaver. My code is as shown below. I am expecting my code to output two boxes, into which I write something. And when I click on the submit button, I expect to see the two separate things that I entered into the box to be concatenated. But I'm not getting a concatenation. In fact, NOTHING happens when I click on submit. I am confused. Is this code not something I should be using Dreamweaver for? I am relatively new to PHP and I do not understand all the bits. My suspicion is that Dreamweaver does not recognize "twoFieldForm.php". Can someone please tell me how I can overcome this issue?
<!DOCTYPE html>
<html>
<head> </head>
<body>
<h1>Form Filler</h1>
<?php
function joined($s1, $s2) {
return array($s1.$s2, strlen($s1.$s2));
}
$first = $_GET[fieldOne];
$second = $_GET[fieldTwo];
$answer = joined($first, $second);
echo "The concatenated string is \"{$answer[0]}\"
and its length is \"{$answer[1]}\";
?>
<form action = "twoFieldForm.php" method = "get">
Field 1 : <input type = "text" name = "fieldOne"/>
Field 2 : <input type = "text" name = "fieldTwo"/>
<input type = "submit">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head> </head>
<body>
<h1>Form Filler</h1>
<?php
function joined($s1, $s2) {
return array($s1.$s2, strlen($s1.$s2));
}
$first = $_GET['fieldOne'];
$second = $_GET['fieldTwo'];
$answer = joined($first, $second);
echo "The concatenated string is \"{$answer[0]}\"
and its length is \"{$answer[1]}\"";
?>
<form action="" method="GET">
Field 1 : <input type="text" name="fieldOne"/>
Field 2 : <input type="text" name="fieldTwo"/>
<input type="submit">
</form>
</body>
</html>
Explanation: You have to add quotes in $_GET['']. Remember indention please, always when I see people using adobe dreamweaver, their code is horrible... If you are refering to the same file, you don't have to use an action="somewhat.php". Just leave it empty. Aswell you have missed a " after your echo statement.
This will work now. Please start using a good IDE then you won't have those basic mistakes because the IDE will show you your mistakes already...
Try add quotes:
$first = $_GET['fieldOne'];
$second = $_GET['fieldTwo'];
$answer=$_GET['fieldOne'].$_GET['fieldTwo'];
echo "concatenated string is:".$answer;
try this...
Related
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;
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.
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'];
}
I'm getting the following error with $_POST['str']:
Notice: Undefined index: str in C:\Program Files\EasyPHP-5.3.8.0\www\strrev.php on line 12
I spent too much time to find an answer for this but no luck!
Please take a look at the code and let me know what's wrong with it?
<html>
<head>
<title></title>
</head>
<body>
<?php
if (trim($_POST['str'])) {
$str = $_POST['str'];
$len = strlen($str);
for($i=($len-1); $i>=0;$i--) {
echo $str[$i];
}
} else {
?>
<form method="post" action="">
<input type="text" name="str" />
<input type="button" name="submit" value="Reverse" />
</form>
<?php
}
?>
</body>
</html>
It shows the text field and Reverse button beside the error.
And also when I push the button nothing will happens.
Change if (trim($_POST['str'])) to if (!empty($_POST['str']))
Your if statement is trying to trim an array index that does not exist, hence the error. You must check if your index exists first.
In case anyone is wondering - I used empty instead of isset as the OP, by using trim, implied (or at least, I inferred from this) that a properly set $_POST['str'] containing an empty string was unacceptable. This won't fix the case where $_POST['str'] contains a bunch of spaces, however.
You're not checking if a POST has actually occured:
<html>
...
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST yada yada yada) {
...
}
}
?>
<form action="" method="POST">
...
</form>
...
</html>
$str is not necessarily an array. In order to loop through it, it needs to be an array.
It's just copying that value from $_POST['str'], so when you do the post either
Make sure it's an array with
if (is_array($str))
I'm trying to write a program where the basic idea is I ask the user for input in a textarea, and then the text gets stored into a word file. Here is the code I'm trying to use:
<html>
<head>
<title>Simple Guestbook</title>
</head>
<body>
<h1>Simple Guestbook Comment Creator</h1>
<br>
<form method = "post"
action = "mysite.php">
<textarea name = "text"
rows = "10"
cols = "20">Write Here</textarea>
<input type = "submit"
value = "Submit Comment">
</form>
<?
if($_POST['text'] !== NULL){
$comment = $_POST['text'];
$file = fopen("texttest.txt", "a");
fputs($file, "<br>\n$comment");
fclose($file);
}
?>
</body>
</html>
I can't seem to get this to work properly. I was also thinking about somehow making the form action store the text and then reload the site, but I haven't gotten that to work (the original file is mysite.php, so the action is to just reload the page).
If anyone has any better ideas of an algorithm to use/different syntax to use, please let me know, as I just started learning basic PHP syntax.
Thanks
Check the following:
Does php have the permission to write files in that directory?
Is that php file called "myfile.php"?
Anyway, when something does not work and you want to know what's causing the arror, place error_reporting(-1); at the beginning of your php - it will output any error or warning, including the ones trown by fopen().
Also, you might want to check whether the variable has been correctly submitted: echo $comment right after you assign it.
Something like this might work.
You might want to do more with the values they are entering and all, but this will basically do what you are asking.
You will also want to make sure that you have the correct path of the file you are trying to write to and that that file has the correct permissions to allow it to be written to:
<html>
<head>
<title>Simple Guestbook</title>
</head>
<body>
<h1>Simple Guestbook Comment Creator</h1><br>
<?php
if (isset($_POST['submit'])) {
if (strlen(trim($_POST['comment']))) {
$file = fopen("texttest.txt", "a");
fputs($file, "$_POST['comment'])\n");
fclose($file);
}
} else {
?>
<form method = "post" action = "<?php echo($_SERVER['PHP_SELF']); ?>">
<label>Leave your comment
<textarea name="comment" rows="10" cols="20"></textarea>
</label>
<input type="submit" name="submit" value="Submit Comment" />
</form>
<?php
}
?>
</body>
Also, since you are returning to the same page you may want to put some kind of message letting the person know that they succeeded in entering something into your address book.