Intro: I've been wandering the internet trying to find a solution to no avail. I'm a PHP noob and my html and css are marginal.
Problem:
I want to create a simple? text box on a website that can be edited and saved and displayed on the website.
If I could avoid PHP or other languages that would be fine but I think the save part is going to require some server site processing. Because I don't expect a large amount of data, storing the text boxes in a xml file or similar should be fine. (no need for a db)
I'm looking for the most simple and hopefully complete solution as I am a rookie with this stuff.
Notes:
I should be able to copy and paste the html code anywhere to create more text/comment boxes. (maybe i need to change a ID or something that's fine)
I hope the comment box has no real text/character limits and any characters or quotes or html or other language is displayed as was written.
It cam be ugly, the focus is functional and simple.
I found this was closest to what I wanted but I could not get it working at all. Maybe its a language my computer or server was not happy with. LINK
Let's start by acknowledging that you will need PHP, and unless you have a proper login system or alternatively a CAPTCHA, this project can be prone to a bot that floods a bunch of messages - I know from experience. All you need to make is an HTML form which will be used to type and submit the text to this public "bulletin board":
<form method='post' action='http://yoursite/newmessage.php' type='submit'>
<input type='text' name='text' placeholder="Type message here"/>
<input type='submit' value='Submit' />
</form>
This form will send whatever the user types in the text input to a PHP file, which will then save it. Now, for something very simple like this you can or cannot use a database.
Benefits of a database:
More secure
If two people try to add a message simultaneously, less issues there
Benefits of a text file
Much simpler to make
Since you are not storing any personal data, a text file will be fine. You can name this whatever you want on the server - it doesn't matter. This will be the PHP file:
<?php
$newText = $_REQUEST['text']; //data submitted from HTML form
$currentText = file_get_contents('data.txt'); //txt file in same folder as this PHP file_get_contents
$concatText = $currentText . '<div><p>' . $newText . '</p></div>';
file_put_contents('data.txt',$concatText);
?>
Lastly, displaying the data on the HTML page is easy. First make sure you are running this on a server that executes PHP, and it'd help to rename index.html to index.php:
<?php
$all_data = file_get_contents('data.txt');
echo $all_data;
?>
Edit I just saw that you want it password-protected. Simply create another text input in the form and in PHP verify that it matches a string.
Related
What I was trying to do, unsuccessfully, is to scan a barcode, then use the output data to send a post request to another site.
I tried using the application from "https://github.com/mebjas/html5-qrcode" in local apache server, but I dont know what I am doing wrong, it just opens a blank page.(tried finding step by step detailed guide, but did not find any. It seem I need a guide to tell me exactly where to put what, if someone knows one for a complete beginner, please tell me. Tried to follow the steps shown at github, but it seems I am missing something.)
I gave up using the github scripts and I then saved page as html from the demo app "https://scanapp.org/#reader" and it opens alright and scans the barcode on my localhost. Tried editing the html and js file, but I cannot find a way to get use of the output data.
So, using the saved page "https://scanapp.org/#reader". When the barcode is scanned, I want to send the decoded result as a post parameter in another site using the below code
<?php
$movieno = $_GET['id_of_decoded_barcode'];
?>
<form id="redirect_form" method="post" action="localhost/checkmoviestatus/find">
<input type="hidden" name="movie_no" value="<?php echo htmlentities($movieno); ?>">
</form>
<script type="text/javascript">
document.getElementById('redirect_form').submit();
</script>
If someone does not have the time to check it how this can be achieved, please to tell me what should I read or watch any tutorial.
If posting automatically as a post data is not possible, please to tell me how to place the decoded result in another place in the document. The output data has only an ID and I cannot replicate it by copying the html code. It shows only in one place at the result data container.
Using PHP and HTML I need to detect a user's IP address, if the IP address is a specific one, an alert box pops up and says "Welcome, ______!".
Can I just set it up as an if, else, or something like that?
I only have a few users (and it will stay that way), so I don't need anything elaborate.
I can choose which IP address corresponds to which username that pops up, but how do I do all of this?
And how do I get it into HTML format? I have a pretty solid understanding of alert boxes (javascript), but php, not so much. Can you please explain the steps in layman's terms?
First, your opening statement ‘I need this all in HTML format’ is not possible as you point out the requirement for PHP which is not HTML. It generally produces HTML, but it’s not HTML in itself.
To detect a users IP address: Google search php get ip address of client and you will find a lot of info including some of the pitfalls on accuracy etc.
Where you want to put HTML or JavaScript in your document use something like:
<?php
$clientIP = $_SERVER['REMOTE_ADDR'];
// now check if it’s a particular IP address. This if statement will only add the echoed html/javascript code if it is that IP address.
If ($clientIP == “192.168.0.2”) {
echo “Javascript/HTML code here to create your popup message between quotes”;
// you can use multiple echo lines which is where you write javascript of html to create your html page
}
// repeat if statement for more IP’s or you can get fancy
?>
A tip on using PHP for certain bits in a normal HTML document: Write the document as HTML and where you need to put something different because of (in this case) a specific IP address, put the PHP code there. Don’t forget to start the php code with a <?php and end it with a ?>
// is a comment line in PHP
$_SERVER http://php.net/manual/en/reserved.variables.server.php
echo is what it says, a built in PHP command that echoes what its told to. See http://php.net/manual/en/function.echo.php for details.
This is fairly basic to hopefully get you going. Start small and work your way up.
This is an odd situation and my current thought is that it doesn't work this way, but I need to some other eyes on this.
A different website I don't have control over has a form with a hidden field in it. The form action is a POST and to send it to a url on my website and I need to be able to get the value of that hidden field using javascript.
As a GET that would be included in the url and I think I would just be parsing that apart. But since it's a POST being sent to me I'm not entirely sure how to get the value of that hidden field out.
Is this doable? If so, where should I be looking to do it?
Thank you!
If your server that is receiving the sended form data uses PHP, you can get all form values using:
<?php
print_r($_POST);
?>
If the page in your server is a static html page, then you cannot get the POST data. Or you can, but then you have to make html pages to be executed as php pages (not recommended however).
You talk about that you need this value be accessible by javascript. Simply do something like:
<script>
<?php
echo 'var input_field_value="'.htmlspecialchars($_POST['name_of_input_field']).'";';
?>
</script>
The question doesn't provide information what server software is used, so I assume that is PHP.
EDIT: after Saturnix's comment I added a call to htmlspecialchars() to make it safe to execute in javascript.
Is it possible to store form data using an external PHP file that does the processing and then use echo values from the PHP result to update an HTML element on the same page without using javascript,jquery, or ajax? Do I have to return html data like this: Your post is <?php echo htmlspecialchars($_POST["email"]); ?>.<br> If so how do I do that and still keep all the other elements on the page the same way they are currently formatted. Related to this, how do I load the HTML page with some elements already bound to values extracted from the database using an external PHP file?
What is best practice for doing this kind of thing. The objective is to allow a user to input text in a textbox, click submit to have the text stored in a database, and then at the same time update an element that displays the latest sumbmission, kind of like a forum post. I know you can use ajax, javascript, jquery to do this kind of thing on the client side, but I have seen html pages with the <?php echo htmlspecialchars($_POST["email"]); ?> type syntax and wanted to experiment with that for simplicity.
"Is it possible to store form data using an external PHP file that does the processing and then use echo values from the PHP result to update an HTML element on the same page without using javascript,jquery, or ajax?"
Answer: No, you must send the data to the same page, ( action="" )
Then include the processing file at the top/beginning of the script. Please feel free to ask for more help.
IF YOU WANT TO SEND THE DATA TO THE DATABASE AND WANT TO DISPLAY THE DATA: Just retrieve the data from your database on the page you want to display it on.
So I've decided to make a website a few months ago and have then picked up basic HTML, CSS, Javascript and Jquery. Now I want to do two things with my website,
show a user-voted poll somewhere on my website that will update live as the users vote on different optinos.
A form on my website that when the user fill out and click the "submit" button, the form will automatically be forwarded to my desired e-mail address
I've been told to learn PHP because it's suppose to help my problem, but so far I have yet to see how it's going to help me solve the above two problems. Any suggestion on how I can achieve the two things above??
PS: if someone can explain what exactly is PHP, I will also be really grateful. I've been looking up what PHP is and the majority of the responses I get is "it's a server side language", which is not exactly helpful. And if PHP does help me solve the two questions above, how?
PHP standing for Hypertext Preprocessor, is a server side programming language. Taking a look at what you have currently learned (HTML, CSS and Javascript) these are all Client side languages.
The difference is that any processing done by a client side language is done within the browser straight in front of the user's eyes.
A server side language like PHP is used when we want to compile relevant data from sources like a MySQL database, before sending the information to the browser. Meaning it's far more dynamic.
You should start by going through some tutorials, do not use w3schools.com, because I don't believe they teach the best practices and it will probably do you more harm than good in the long run.
A quick google search should lead you to some decent tutorials, however I'd highly recommend http://php.net/manual/en/getting-started.php
PHP is actually a "server side language" (mostly) :)
It means that it allows your web server to be smart, i.e:
load/read data from a database
send emails
generate pages dynamically (instead of just serving static pages)
etc...
Sending mail with PHP is documented here: http://us2.php.net/manual/en/function.mail.php
Basically in your HTML you'll tell your form to be posted to a specific PHP script which is hosted on your web server, e.g:
<form action='send_mail.php'>.....</form>
And this script will retrieve the form data and handle the mail sending, e.g:
<php>
$data = $_POST; // contains the form's input values
mail("foo#mail.com", "Notification mail", "A form has been submitted with some data: ".$data["fieldName"]);
</php>
PHP is exactly what they have told you - a server side scripting language. What that means is that PHP will perform actions at the server level before rendering a page. This includes:
Loading dynamic content from a database
Posting information from a form to a database
Sending it to an email address like what you want to do
A lot of other stuff you'll learn if you stick with it.
To send an email when someone submits your form, the form needs to post to a PHP page.
Your form:
<form method="POST" action="sendmail.php">
<input type="text" name="name" />
<!-- Other inputs go here, etc. -->
<input type="submit" value="submit">
</form>
PHP to send the form (In sendmail.php)
<?php
$email = $_POST['inputName1'];
$name = $_POST['inputName2'];
$message = $_POST['inputName3'];
$phone = $_POST['inputName4'];
$yourEmail = "youremail#domain.com";
$to = $yourEmail;
$subject = "My Contact Form";
$message = $message;
$from = $email;
$headers = "From:" . $name;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
Here is a tutorial I found via Google: http://www.daniweb.com/web-development/php/threads/38784/php-sendmail-tutorial
There are probably better ones, but that should get you started.
Long story short, you need to learn PHP, and javascript.
Your other requirement (The poll) can be done many different ways, just google "Set up an online poll" for different ways. You can probably use jQuery/javascript and JSON, or XML, etc to power the polls as well as php. This tutorial may help you with that: http://net.tutsplus.com/tutorials/javascript-ajax/creating-a-dynamic-poll-with-jquery-and-php/
Good luck.
PHP is processed at the server level and the results are sent to the client's computer for display. JavaScript does its processing on the client's computer.
You can use the PHP Mail function to have the data entered in the form by the user sent to your email. Here is a link on how to use that: http://www.w3schools.com/php/php_mail.asp