XAMPP PHP error Notice: "Undefined variable" from HTML to PHP - javascript

I am newbie learning PHP, and I think my problem is that I can't pass a variable value from HTML to PHP:
I have one page, called form.php. Here it is its code, along with its HTML code as well:
<html>
<head>
<title>form</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<form action="page2.php" method=post>
My name is: <br>
<input type="text" name="yourname">
<p> Please leave your message here <br>
<input type="text" name="message">
<p>
<input type="submit" name="submit" value="Please accept my data!">
</form>
</body>
page2.php has this chunk instead in it:
<html>
<head>
<title>Hi!</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p>
Hi! <?php print $yourname; ?>
<p>
Thank you for your message <b> <?php print $message; ?> !?! </b>
</body>
The error I get is the following:
Notice: Undefined variable: yourname in C:\xampp\htdocs\Test-Antonio\page2.php on line 7
Notice: Undefined variable: message in C:\xampp\htdocs\Test-Antonio\page2.php on line 9
So it seems that I am not able to pass varibles values from HTML to PHP. Is it right? What I am supposed to do to make it run?
Thank you in advance to anyone who can help me on this!!
Best regards,
Antonio.

Page 1 should be:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>page 1</title>
</head>
<body>
<form action="page2.php" method="post">
<label>My name is: </label><br/>
<input type="text" name="yourname"> <br/>
<label> Please leave your message here:</label><br/>
<input type="text" name="message"><br/>
<input type="submit" name="submit" value="Please accept my data!">
</form>
</body>
</html>
Page 2 should be :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>page 2</title>
</head>
<body>
<p>Hi! <?php echo $_POST['yourname']; ?><p>
<p>Thank you for your message <b> <?php echo $_POST['message']; ?> !?! </p>
</body>
</html>

Your syntax is wrong. You should echo $yourname with
<?php echo $yourname ?>
OR
Also make sure you've assigned the $yourname variable. You can do that with
$yourname = "your name";
Or if you are working with a post mechanism:
$yourname = $_POST['yourname'];

<?php echo $_POST ["yourname']; ?>
Will resolve your error. Because you have not assigned a variable for $yourname PHP doesn't know what it's doing so will throw an error. I'd also recommend looking at the manual for the $_POST global
http://php.net/manual/en/reserved.variables.post.php

You can get the variables from a form using $_POST['variablesName']. So in this case , use $_POST['yourname'] other than $yourname.

use $_POST to get the data passed via POST request
Hi! <?php echo $_POST["yourname"]; ?>
Your html form has method="post" attribute so it will pass all your input via POST request and can be accessible in PHP in $_POST[<input name>] variable.
If your form has no method attribute defined or has method="get" then it will be submitted via GET request and can be accessible in PHP via $_GET[<input name>].
Read this: http://www.w3schools.com/php/php_forms.asp
Learn about basic form handling in PHP and HTML please. You might want to learn ajax via javascript to tweak your forms. Have a read about ajax here: https://developer.mozilla.org/en/docs/AJAX

Related

Accessing PHP file through HTML form

I am working on my website which having different tabs (home, contact, etc.). Now in one of these tabs I'm using a login form, in this form I'm just validating one fixed user (Not Using database), this will understand from verification.php code.
Below is part of .html file code that calling verification.php file
<div id="loginn" class="loginForm">
<form action="verfication.php" method="post">
<div class="ll">
<img src="image/avatar2.png">
</div>
<label for="name"><b>Username</b></label><br>
<input type="text" name="uname" required><br>
<label for="psw"><b>Password</b></label><br>
<input type="password" name="psw" required><br>
<button type="submit">Login</button>
</form>
</div>
Below is my verification.php file, which is saved by me in www directory, where all the html, css, js files are stored
<!DOCTTYPE html>
<html>
<body>
<?php
if(isset($_POST['uname']) and isset($_POST['psw']))
{
$name=$_POST['uname'];
$pass=$_POST['psw'];
if ( $name == "rrrr" and $pass="ravina#6543" )
{
?>
<script type="text/javascript">document.getElementById('veri').style.display="block";</script>
<script type="text/javascript">document.getElementById('loginn').style.display="none";</script>
<?php}
}
?>
</body>
</html>
Now the problem is, when I click on the login button, there is nothing happening on the html part. I mean, my PHP file is not getting access from my HTML part, so please can anyone help me to solve these problem?
I think there's an error in the syntax: first of all there is an additional "T" in the <!DOCTTYPE html> and secondly there's a curly bracket after you started the php script: <?php}, just adding a space will fix your problem.
There's also a spelling mistake in your html code regarding the name of the php file verification.php.
<!DOCTYPE html>
<html>
<body>
<?php
if(isset($_POST['uname']) and isset($_POST['psw']))
{
$name=$_POST['uname'];
$pass=$_POST['psw'];
if ( $name == "rrrr" and $pass="ravina#6543" )
{
?>
<script type="text/javascript">document.getElementById('veri').style.display="block";</script>
<script type="text/javascript">document.getElementById('loginn').style.display="none";</script>
<?php }
}
?>
</body>
</html>
in your verification.php, you can check success/not with using only echo
<!DOCTYPE html>
<html>
<body>
<?php
if(isset($_POST['uname']) and isset($_POST['psw']))
{
$name=$_POST['uname'];
$pass=$_POST['psw'];
if ( $name == "rrrr" and $pass="ravina#6543" )
{
echo "success~~";
} else {
echo "failed!!";
}
}
?>
</body>
</html>
This is not how to use PHP, HTML and javascript. You should start by learning how HTTP works, then HTML, then JavaScript and finally PHP.
First of all you should correctly write the name of php file in your html form i.e. verification.php
verification.php file remove one T from DOCTTYPE html and give one space between php and { in ?php{ this line.

Php $_POST returns empty when using javascript to call php function

I am still studying php and java script.
I am creating a simple contact form and set the form action to the same page using $_Server[php_self]
What I want to do is when someone submit to my form, it will show a message including the name that was submitted on the same page. replace the contact form with the message.
I also tried pointing action to a different php page. and it still did not work.
Does Javascript work like that? or I have to use different code or language to do that.
Here is my code
<!DOCTYPE html>
<html>
<head>
<?php
include 'action.php';
?>
<title> My profle</title>
<meta name="robots" content="noindex">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="contact">
<form class="form" class="contact" id="contact-form" action="action.php" method="POST">
Name: <br>
<input type="text" class="field" name="name"><br>
Number:<br>
<input type="text" class="field" name="number"><br>
Email:<br>
<input type="email" class="field" name="email:>"<br>
<br>
<input type="submit" class="submit" name="submit" value="submit"
onclick ="document.getElementById('contact-form').innerHTML='<?php thankyou();?>'">
</form>
</div>
</body>
</html>
Then here is the action.php
<?php
function thankyou(){
$name = $_POST['name'];
echo "Thank you"." $name ! Your message has been submitted.";
}
?>
You have a couple of different problems here.
The first is a lack of understanding about the timing of when PHP and JS run.
The second is that DOM changes are lost when a new page is loaded.
This is what is happening:
The browser requests the page
The PHP runs ($_POST does not have a name key)
The browser gets the page
You click the submit button
The JavaScript runs, and set the innerHTML (remember that the PHP ran back at step 2)
The form submits causing the browser to navigate to a new page
The PHP runs again
The browser gets the new page … and the DOM changes make to the previous page are lost
Further reading: What is the difference between client-side and server-side programming?.
I'm open to hearing better ways to do this in the comments, as this is off the top of my head and I know there are better solutions... but here's what I would do in your situation:
<!DOCTYPE html>
<html>
<head>
<title> My profle</title>
<meta name="robots" content="noindex">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="contact">
<form class="form" class="contact" id="contact-form" action="" method="POST">
<?php
if(isset($_POST)){
//process data however (I'm assuming) you need to - even if just posting to another script at this point...
echo 'Thank you '.$_POST['name'].' ! Your message has been submitted';
}
else{
echo 'Name: <br>
<input type="text" class="field" name="name"><br>
Number:<br>
<input type="text" class="field" name="number"><br>
Email:<br>
<input type="email" class="field" name="email"><br>
<br>
<input type="submit" class="submit" name="submit" value="submit">';
}
?>
</form>
</div>
</body>
</html>

How would you make a request when you created a form?

I am trying to hack a page that requires a "username" and "password". I have created a form and I think it looks good;
<form action="http://hackmefff.co/login">
<input type="hidden" name = "username" value="hacker">
<input type="hidden" name = "password" value="b678jk">
</form>
People have told me that my form looks good and that I need to create a request in javascript. I don't know how to create one because I just started learning javascript yesterday. I have searched on how to create a request of submit, and all I see is functions that look like they are making a form, which I already have, so am confused. How would I send a request to log me into a website, assuming that url exists, and both name and password are correct.
EDIT:
I have formatted the code below because I don't want the page to show any text at all even though am logged in, it should just be blank. But a test page shows that I didn't get logged in;
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head>
<title>Test page for your custom form</title>
<script type="text/javascript">
function submitform()
{
document.yourform.submit();
}
</script>
</head>
<body>
<form name="yourform" action="http://hackmefff.co/login">
<input type="hidden" name = "username" value="hacker">
<input type="hidden" name = "password" value="b678jk">
</form>
<input type='hidden' name='query' />
</body>
</html>
<html>
<head>
<title>Test page for your custom form</title>
<script type="text/javascript">
function submitform()
{
document.yourform.submit();
}
</script>
</head>
<body>
<form name="yourform" action="http://hackmefff.co/login" method="post">
<input type="hidden" name = "username" value="hacker">
<input type="hidden" name = "password" value="b678jk">
</form>
Search: <input type='text' name='query' />
Login
</body>
</html>
Note that I have modified your form slightly to include a name so that we can reference it from the JavaScript function.
This answer of course assumes that you have to use JavaScript.
You don't need Javascript for that, just add a submit-button to your form:
<input type="submit" value="Submit">

PHP Code Error fopen not opening and writing

When I run this PHP code it should store the comment but it doesn't write anything in comments.txt as it was supposed to do. Please find the error.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome</title>
</head>
<body>
<?php
setcookie("username","admin", time()+3600);
setcookie("password","xss", time()+3600);
if($_POST['content']!=null){
$fp= fopen('comments.txt','a');
fwrite($fp,$_POST['content'], "</hr>");
fclose($fp);
}
echo nl2br(file_get_contents('comments.txt'));
?>
<h3>Post Your HTML Code here</h3>
<form action="index.php" method="post">
<textarea name="content" rows="3" cols="100"></textarea>
<br/>
<input type="submit" value="Post" />
</form>
</body>
</html>
You can see a demo of this on my website here.
have you checked your file has right permissions for apache to write it?
you can change permissions with chmod, an example:
// allow any operation for any user
chmod("comments.txt", 0777);
if($_POST['content']!=null){
The preferred (or preached) way to handle what you're after with this line is:
if(isset($_POST['content'])) {
For the actual error, it's because you're using the wrong arguments for fwrite(). From the PHP documentation, the arguments are:
int fwrite ( resource $handle , string $string [, int $length ] )
Drop the "</hr>" from the end and it'll fix it, like so:
fwrite($fp,$_POST['content']);

using a php variable in a JS funcion

i previously asked this question but the example i provided did not provide my real problem.
I want to get the input from a form using a php variable and use it in a javascript function. I do not use javascript directly because the content of the variable in question is obtained from a mysql database and i do not think that there is a way for me to access the database using javascript.
below its the code that i have so far:
<!DOCTYPE html>
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
function initialize() {
var slon1 = '<?php echo (json_encode($siteLon1)); ?>';
self.alert(slon1);
//some code goes here.
}
</script>
</head>
<body>
<div align="center">
<form method="get">
<label>Choose Site</label>
<select name='ps' id="ps" data-inline="true">
<?php
$use = $fgmembersite->getsites($fgmembersite->UserFullName());
$fields_num = mysql_num_fields($use);
while($row = mysql_fetch_row($use))
{
echo "<option value='".$row[0]."'>".$row[0]."</option>";
}
?>
</select>
<input type="submit" name="submit" value="View">
</form>
</div>
<div id="map-canvas"></div>
</body>
</html>
Suprisingly enough, the result displayed on self.alert(slon1) is null; be it when i just load the page or when choose from the drop down box and click on View. Please someone help me on this.
Thanks.
JSON encoder should quote variable, so using single quotes manually would trigger JavaScript parse error. Check JS console. This should generate valid JS code:
var slon1 = <?php echo (json_encode($siteLon1)); ?>;

Categories

Resources