PHP Code Error fopen not opening and writing - javascript

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']);

Related

I need help understanding how to use a javascript function in an onclick event in a php file

I created a javascript file, made a function, and then linked my html file with it in the head. It worked. When I copy and pasted the exact same thing into a php file, the function suddenly doesn't work. I need a way to make the function work in the php file.
This is the javascript function:
function showField(){
if(document.getElementById("downtown").checked && document.getElementById("pricerange").value == "100"){
document.getElementById("newField").removeAttribute("hidden");
}
}
This is the php where i try to call the function:
<input type="button" name = "search" value="Search" id="search" onclick="showField();">
This is in my head where I linked the javascript file:
<script src = "a3q5.js"></script>
Seeing that I don't know what the rest of your code is, I would just make sure you have the normal HTML stuff:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<input type="button" name = "search" value="Search" id="search" onclick="showField();">
<script src = "a3q5.js"></script>
</body>
</html>
Hope this helps!
The simple way would be here, Put this to your PHP. It worked for me
<?php
// your code
echo '<script src = "a3q5.js"></script>';
// your code
?>

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.

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

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

Not see data on the textarea

not see data on the textarea
creatDocument.php
<html>
<head>
<script type="text/javascript" src="../jquery-1.8.1.js"></script>
<script language="javascript">
function insertFormulationToTextErea(string){
document.getElementById("argumentId").value = string;
}
</script>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<textarea name="argument" id="argumentId" rows="10" cols="50"></textarea>
<?php
if(isset($_REQUEST['formulation']))
$formulation = $_REQUEST['formulation'];
else $formulation = "No data receive";
$formulation = base64_decode($formulation);
echo "<script>insertFormulationToTextErea('$formulation')
</script>";
echo $formulation;
?>
</body>
</html>
$formulation is the string parameter that received from another php page with POST method.
When I use with echo I see the string value, when I try to insert $formulation to textarea, the textarea is empty.
When I use with GET method it work fine, but when $formulation string too long, the server reported that Request-URI Too Large.
Anybody know this problem or any solution that I can use?
What you're doing looks a bit complex, try simply this:
<textarea name="argument" id="argumentId" rows="10" cols="50" value="<?php echo $formulation; ?>"></textarea>
This puts your string directly into the textarea value, without trying to use it as a parameter for your function.
AND reason why your you get nothing from $formulation when you use it as a paramter for your function is because your code should be like so:
echo "<script>insertFormulationToTextErea(".$formulation.")</script>";
Though I highly advise you to avoid coding like that..

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