So I'm trying to get my webpage to save some text to a server-side file. It's kinda like a guestbook, but I want it in a text file so I can review it before I put it on the site. I've been looking around, and from the looks of it, you can't do it with javascript because of 'security hazards'. I've seen things about php, but php files never seem to work for me. Is there a way to do this in plain .html and if not how do I make a php file do this, and be very specific with the steps required to make said php file. And I'm using apache if that helps at all. Thanks for your help.
This will take one PHP file and is not secure. But, it is what you want. The user can very easily sign the guest book. Additionally, it will display previous guest entries on the page. Yay!
guestBook.php
<?php
session_start();
if (!empty($_POST['name'])) {
if (!isset($_SESSION['posts'])) {
$_SESSION['posts'] = 0;
} else if ($_SESSION['posts'] >= 5) {
echo "<div style='color:red'>Failed to post - max posts exceded</div>";
} else {
$_SESSION['posts'] += 1;
file_put_contents("guest.txt",htmlspecialchars($_POST['name']) . "<br />",FILE_APPEND);
}
}
echo file_get_contents("guest.txt");
?>
<form method="post" action="">
<input name="name" type="text" value="John Kieth" />
<input type="submit" />
</form>
P.S. You literally can't do this through regular HTML/JavaScript.
Related
I want to download text from HTML form to .txt file on my PC. I have tried many ways but nothing works well:
require(fs) doesn't work with the browser, as far as I understand
require(browserify-fs) saves file in VIRTUAL MEMORY (as far as I understand), so it useless for me
last way I found - using PHP script(I found it in net), but it doesn't work, idk why. I have tried to fix it - nothing, I know nothing about PHP
program.php
<?PHP
$title = $_POST["channel0Title"];
$gain = $_POST["channel0Gain"];
$offset = $_POST["channel0Offset"];
$file = fopen('1.txt', 'w');
ftruncate($file, 0);
$content = $title. PHP_EOL .$gain. PHP_EOL .$offset;
fwrite($file , $content);
fclose($file );
die(header("Location: ".$_SERVER["HTTP_REFERER"]));
?>
main.html
<form action="public\scripts\program.php" method="post">
Channel 8 Title:<br><input type="text" name="channel0Title" value="Channel 7"><br>
Gain:<br><input type="text" name="channel0Gain" value="4.000"><br>
Offset:<br><input type="text" name="channel0Offset" value= "6.000"><br>
<input type="submit" id ="submitButton" value="Submit">
</form>
I have a error Cannot POST /public/scripts/program.php
Could you help me please why such a script doesn't work
Or maybe give advice on what language or framework should I use to simplify data transfer between client and server
Thank you in advance!
P.S. I use Node.js, so load program.php as a static file
I know this question has been asked before, but I'm trying to diagnose why this isn't working. I want to write form data into a .txt file using a post request. I don't know much PHP at all, as this is a quick program I'm patching together. Here's the code:
Javascript:
function submitdata() {
document.querySelector("#val").innerHTML = input.value + ": " + input1.value;
document.querySelector("#submitform").submit(); }
HTML:
<form style="display: none;" method="POST" name="myform" id="submitform">
<input id="val" name="val">
</form>
PHP:
<?php
if(isset($_POST['val']))
{
$data=$_POST['val'];
$fp = fopen('data.txt', 'a+');
fwrite($fp, $data);
fclose($fp);
}
?>
How does your browser know where to send the form data?
You need to specify the php file name in form action attribute.
Edit- added relevant point from comment below.
I have pointed out the obvious error based on what you have provided, but it might not be the only one. Other error is you are using innerHTML on an input element. So this may not set the value for #val (some browsers may set the value, some may not).
2 weeks ago I taught myself html, css and javascript in my spare time. I know the basics of all three of these languages. I just need a little help with the submit button and server side programming.
Here's my problem:
I made a website (html & css) that randomly generates two things to rate (for example, girls). Under these, there is a radio button for left or right. Then under that there is a submit button. When you enter left or right and hit submit, there is no rating algorithm, so it just goes to the next file (which repeats this). I want to know how to put in a rating algorithm to keep score (like elo or a rating/10). How should I approach this? I thought of a couple of ways, but I don't know if I can pull it off in JS or I'll have to learn php.
For JS, the only idea I have is maybe making and if/else if. For example:
If girl1 and girl2 are available and girl1 is chosen:
Run this algorithm.
Else if girl1 and girl2 are available and girl2 is chosen:
Run this algorithm.
If I do this, will the variables change and be saved when anybody uses the site, or are there limits?
I will also be using a web hosting service, I don't have my own server.
Thanks a ton!
This is the form that you would use for selection. Lets say a file named abc.php
Write a bit of html in abc.php
<form method="POST">
<input type="radio" name="person" value="1" />
<input type="radio" name="person" value="2" />
<input type="submit" name="submit" />
</form>
This is the server side scripting
And a bit of php in abc.php
<?php
if(isset($_POST['submit']))
{
$person = $_POST['person'];
echo $person;
}
?>
This $person has now the value either 1 or 2. Since you have gotten the value now you can use it. for instance
<?php
if(isset($_POST['submit']))
{
$person = $_POST['person'];
echo $person;
ratePerson($person);
}
function ratePerson($person)
{
//some logic of your algorithm
}
?>
I trying to store CSV data to database using onclick function. Unfortunately, I am using php code inside javascript function which is not efficient enough. Therefore, I hope that I can get any suggest or solution to improve efficiency of my project by using javascript instead of php to store CSV data into database.
This is javascript with php code :
<script>
function storeQueEmail(){
<?php
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
$record['contact_first'] = $data[0];
$record['contact_last'] = $data[1];
$record['contact_email'] = $data[2];
$record['subject'] = $_REQUEST['subject'];
$record['message'] = $_REQUEST['message'];
$record['status'] = 0;
$oAdminEmail->insertQueEmail($record);
}
} while ($data = fgetcsv($handle,1000,",","'"));
?>
}
</script>
This is HTML code :
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Import a CSV File with PHP & MySQL</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1" >
Subject : <br/>
<input type="text" name="subject" id="subject" required/> <br/>
Choose your upload type: <br />
<input name="csv" type="file" id="csv" accept=".csv" required/> <br/>
Content : <br/>
<textarea name="message" cols="50" rows="10" required></textarea><br/>
<input type="submit" name="submit" value="Submit" onclick="storeQueEmail()"/>
</form>
</body>
</html>
what are you trying to do there? I think you are speaking about a PHP in
HTML document. So your PHP code inside your function is interpreted on server. As your PHP code has no output your Javascript function is simply empty. Thus as you have registered your Javascript function as onclick handler just nothing would happen.
Remember that standard Javascript code is interpreted on Client (that means in browser) though you PHP code is interpreted on server. As mentioned in the comments above it is possible to use an AJAX or POST/GET - Request to send data to your server and then write it to your DB or file etc.
Another way to do this directly with Javascript is Node.js - serverside Javascript that is able to write to your Database like PHP can do.
The easiest way for you to do it with your HTML code you presented above is to fill in your
action - attribute
in your
form tag
For example:
<form action="proccessData.php" ...>
...
</form>
Don't forget to remove your onclick - attribute inside form's submit input field.
If you know press your submit button your entire form and its content will be send to http://www.xyz.de/proccessData.php. Inside that file you can work with your form data: In your case two text fields and one file upload field. As you may know you could get to the content of text input field send via post via:
$_POST['<name of field>']
To get your uploaded file and proccess it you could use PHP's global
$_FILE[] - Array
Just refer to PHP's manual on php.net or some other online documentation. There's pretty much to find on the web.
I just give you a helpful link to php.net on how to handle file uploads in a correct and therefor secure manner: http://us2.php.net/manual/en/features.file-upload.php
Once you have read your uploaded file via PHP's global $_FILE[] - array just proccess that file via fgetcsv in your proccessData.php and write it line for line to your database.
Hope that helps! It is really not that hard :)
After looking around on a Google without any success, i feel posting here may be a good idea as I have used this site to answer previous questions.
Anyways, I am currently working on an HTML5 canvas game using; PHP, MYSQL, Html5, and JavaScript.
I have MYSQL databases setup and an PHP page displaying player high-scores, and usernames.
My question is how would I go about displaying the high-scores inside the canvas once the game is over.
As well as saving the high score when the game ends. I've looked on W3SCHOOLS site about AJAX but I'm still unsure of what codes to use inside the JavaScript file.
These are my php/script codes. or at-least the ones that are relevant:
// Here's the savescore.php file
<?php
include 'connect.php';
$user_score = ($_POST['user_score']);
$user_name = ($_POST['user_name']);
if(mysql_query("INSERT INTO users VALUES('$user_name','$user_score')"))
echo "Score Successfully Saved";
else
echo "Score Saving Failed";
?>
// Here's some of the index.php file
<link rel="stylesheet" href="css.css">
</HEAD>
<body>
<div id="menu">
<a class="item" href="/index.php">Home</a>
<?php
include 'connect.php';
session_start();
if($_SESSION['signed_in'])
{
echo 'Hello ' . $_SESSION['user_name'] . '. Not you? Sign out';
include 'chat.php';
}
else
{
echo 'Sign in or create an account.';
}
?>
</div>
<BODY>
<canvas id="canvasGAMEOVER" width="800" height="599"> </canvas>
<script src="game.js"> </script>
// here's whats inside inside game.js... well the part I want to be able to save score
var score = 0;
function drawGAMEOVER() {
}
I have used google and looked at tutorials for AJAX, I found I have been able to connect to the server using AJAX only using:
<form action="savescore.php">
user_name: <input type="text" name="user_name"><br>
user_score: <input type="text" name="user_score"><br>
<input type="submit" value="Submit">
</form>
inside the index.php page, but I am not sure if its possible to grab the 'user_name' they logged in with (displayed on the index.php page) as well as this.score (displayed inside the javascript file.)
Could anyone tell me how this is possible... if not maybe a better way of doing this?
Any help/reply is much appreciated thanks in advance.
If you are using jquery you can use $.get to get all highscores.
http://api.jquery.com/jQuery.get/