HTML5 & PHP Contact Form - php

I am assigned with creating an HTML page that accepts user input and then verifying the user input on a php script (upon clicking submit on the HTML page). Thus far, I am only trying to verify the first name variable of the HTML page onto the PHP page.
My issue is that the PHP page is not displaying the HTML variables, 'fname' I've looked around for a good solution but I am unable to find anything yet
I am using Aptana Studio 3 and I am running XAMPP. I have posted my HTML and PHP code below for you to view. Any help would be greatly appreciated :)
<!DOCTYPE html>
<html>
<head>
<title>
Assignment Four, HMTL/PHP Form
</title>
<meta charset = "UTF-8">
</head>
<body>
<!--Begin the body code-->
<h1>Assignment Four: HTML/PHP Form</h1>
<p>Watch the following video, write a brief review and then click submit!</p>
<!--Post the Scholartica Video-->
<iframe width="560" height="315" src="//www.youtube.com/embed/d_2_8n86jA8" frameborder="0"
allowfullscreen></iframe>
<h3>Complete the following information:</h3>
<form method = "post" action = "form.php">
<p><label>First Name</label>
<input type = "text" name = "fname"></p>
<p><label>Last Name</label>
<input type = "text" name = "lname"></p>
<p><label>Email:</label>
<input type = "text" name = "email"></p>
<p><label>Phone:</label>
<input type = "text" name = "phone"
placeholder = "(123) 456-7890"></p>
<h3>Write a brief review about the video here</h3>
<p><textarea rows = "4" cols="50" name = "review">What did you think?
</textarea></p>
<h3>Rate the quality of the video</h3>
<select name = "Rating">
<option>Poor</option>
<option>Average</option>
<option>Good</option>
<option>Excellent</option>
</select>
<p><input type = "submit" name ="submit" value ="Register"></p>
</form>
</body>
</html>
My PHP form looks like this
<!DOCTYPE html>
<html>
<body>
Hi <?php echo $_POST["fname"];?>
Thank you for finishing the survey
Your responses have been recorded.
</body>
</html>

I ran the code and it seems to work.
Entering name = "Bob" and clicking submit returns page with
Hi Bob Thank you for finishing the survey Your responses have been recorded.
Make sure that:
Your second page that displays the result is called "form.php"
And also have both files in the same directory

Related

Echo textarea's value with PHP

I want to echo the textarea value with PHP, so I create a simple form with HTML, and inside it I include textarea element with name of b64_place and then input to submit the values.
I check if b64_place is set, and if it is I echo the value of the textarea. But my program doesn't even get into the condition block, I try debugging and it is just not doing nothing.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<form action="index.php" method="GET">
<textarea name="b64_place" form="encode">Enter text here:</textarea>
<input type="submit" value="Encode">
</form>
<?php
if (isset($_GET['b64_place'])) {
$base64e_text = htmlspecialchars($_GET['b64_place']);
echo $base64e_text;
}
?>
</body>
</html>
Your textarea contains an attribute form This attribute is used to define the id of the form this input is attached to. So, when you submit the form, the textarea isn't bound with that form and the datas aren't send
You can either add an id to the form :
<!-- check this ----------------------v---------v -->
<form action="index.php" method="GET" id="encode">
<textarea name="b64_place" form="encode">Enter text here:</textarea>
<input type="submit" value="Encode">
</form>
or simply remove the form="encode"
Edit based on suggestion from senior SO members,
The reason i recommend you to change the method to POST is because of the length limit of the GET method. At some point you may want to encode very large data and it may get trimmed of because of URL length limit. But with POST you don't have to worry about this restriction.
Steps to solve your issue.
If your Form and your PHP code is in the same file changethe action="index.php" to action="" and change the method="GET" to method="POST"
In text area use placeholder to tell the user what to input instead of writing it between the tags.
change $_GET to $_POST everywhere in your code.
You can copy the following code into the index.php and it will work fine.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<form action="" method="POST">
<textarea name="b64_place" placeholder="Enter text here:"></textarea>
<input type="submit" value="Encode">
</form>
<?php
if (isset($_POST['b64_place'])) {
$base64e_text = htmlspecialchars($_POST['b64_place']);
echo $base64e_text;
}
?>
</body>
</html>

Just starting and struggling

I am trying to code a basic website as starter test for me and am struggling. It sort of a blog
It has a form and a textarea, the user types in the textarea, and it passes the value to another php page. Where it gets printed as a test.
But it does not get printed. Some help to get me started would be useful. Thanks in advance
<form method="Post" action="testit.php" name = "myform">
<textarea name="blogtext" rows="15" cols="50">
</textarea>
<p><input type="submit" value="Submit Blog"><p>
</form>
and the php file is
<html>
<body>
Welcome to you
<?php
$name = $_POST["blogtext"];
print $name
?>
</body>
</html

Get sum with php

I have following codes
<html>
<head>
<title>Javascript function </title>
<style type="text/css">
.box
{
width:400px;
background-color:#F0F8FF;
}
h4
{
color:#09F
}
</style>
<script type="text/javascript">
function hello(){
var xx=eval(form1.text1.value);
var yy=eval(form1.text2.value);
form1.text3.value=xx+yy
}
</script>
</head>
<body onLoad="form1.text1.focus()">
<center>
<div class="box">
<h1 style="color:#2c80d3 ">Javascript Function</h1>
<table border="0" cellspacing="1" cellpadding="1" width="25%">
<form name="form1" action="textboxes.php" method="Post">
<tr><td> First</d><td width="20px"><input type="text" name="text1" value=""></td></tr>
<tr><td> Second</d><td><input type="text" name="text2" value="" onBlur="hello()"></td></tr>
<tr><td> Result</d><td><input type="text" name="text3" value="" disabled=""></td></tr>
</form>
</table>
<h4>Enter any digit in text1 and text2 and see result in text3</h4>
<h4>Is it possible to do above with php without submitting FORM?</h4>
</div>
</center>
</body>
</html>
No problem, It works fine.
I used java script to sum two numbers.
Is it possible to add two numbers with php without using any submit button?
If yes then please guide me.
http://i41.tinypic.com/2rfev7m.jpg
If you want to do it without a submit button and in php, you can use an AJAX request in javascript to the server, which will compute to value and then return it to the client.
here is the working sample.
First of all we need to create a html file named add.html.
here is the code of the add.html file…
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Addition</title>
</head>
<body>
<form action="result.php" method="post">
Enter first Integer: <input type="text" name="first" size="5" /><br/>
Enter second Integer: <input type="text" name="sec" size="5" /><br/>
<input type="submit" name="submit" value="Add" />
</form>
</body>
</html>
place it in the pseudo server folder. If your pseudo server is WampServer, then the path of the file will be
C:/wamp/www/add.html
now open your favorite browser and to the address bar type
localhost/add.html
You can see the add.html page. add with number
You can see two text box where you can input the numbers and a submit button. You can input two numbers into the input boxes and press the submit button (The Add button). But nothing will happen. Because the php will do the adding job.
Lets create the php file. You can name it result.php. As I have already declared in the add.html form action is the result.php.
<form action="result.php" method="post">
if you give different name of the php file then please change the form action php name in add.html.
Here is the php code of result.php…
<?php //Starting of php
$first = $_POST['first']; //Getting Value of first integer from add.html
$sec = $_POST['sec']; //Getting Value of Second integer from add.html
$res = $first + $sec; //Adding the two values and placing the added result to 'res' variable
echo 'Added Result:';
echo $first." + ".$sec." = ".$res; //Showing the result to the screen
//Ending of php
?>
save this result.php file in the server path where you have already placed the add.html file.
Now it is the time to test. Open your favorite browser and in the address bar type…
localhost/add.html
enter two numbers and hit the Add button. you will see that the browser will direct you to the result.php page where you can see the added result.
hope this will help you.

generate code using form button and then display generated code on page

I have two file code_generator.html. which takes input as a image url and landing url when i click on submit button it calls code_created.php
<html>
<head>
</head>
<body>
<form action ="code_created.php" method ="post">
Image : <input type ="text" name ="image">
Landing Url : <input type ="text" name="landingurl">
<input type= "submit">
</form>
</body>
</html>
I want to show generated code as below on web page
<div id='banner-img' style='display:none;text-align:center' onclick='landing()'><img style='text-align:center' id='bannerImage'/>
<img id='t'/>
<img id='trackeridImg' style='display:none;width:0px;height:0px'/>
</div>
<script type="text/javascript">
function showAd() {
document.getElementById('banner-img').style.display = 'block';
document.getElementById('bannerImage').setAttribute('src',IMAGE URL SUBMITTED FROM HTML FILE);
</script>
problem is that webpage is not showing that code generated code ,webpage is rendering that code , I want to only show that generated code.
1-how to do it using html and php
2-is my approach is right
You can use tag to show HTML entities You need to encode all
Your HTML entities like < => < like way.
Also you can show a text area in which all those HTML code need to echo, it will not execute your code simply it will print it.

comunication between pages in php

HI,
I wrote this code in php.
<head>
<title>listent</title>
</head>
<body>
<form action="untitled 3.php">
<input type = "text" name = "user">
<br>
<textarea name = "address" rows = "10" cols = "40">
</textarea>
<br>
<input type = "submit" value = "heat it">
<br>
<select name="combobox" multiple[]>
<option>mehdi
<option>nine
</select>
</form>
</body>
</html>
now when i click on submit button untitled 3.php is run.
in untitled 3.php i wrote
<?php
print "welcome $user";
?>
but it has error.
Notice: Undefined variable: user in C:\xampp\htdocs\me\Untitled 3.php on line 4
welcome
what is problem?how can i solve it?
Form values don't just magically appear as variables anymore - at least not in any decently modern and properly configured PHP installation. You need to do $_GET["user"] to access the value which is sent by the form (into the URL - you might want to read about the difference between GET and POST)
And please, please use more descriptive names for your files...
PHP Globals wont survive the new page.
In you case you must use the POST variables sent by your form.
So in untitled3.php you should have
echo "welcome ".$_POST['user'];
PS : I would avoid spaces in PHP filenames.
First you should specify a Form submission method in your first page:
<form action="untitled 3.php" method="post">
Then you have access to all posted values in the $_POST array in untitled 3.php:
$user = $_POST['user'];

Categories