epiz_31101759
Website: http://dragondata.42web.io/
No error message, the page just doesn’t load when I paste PHP Form code into my index.php
I’m new to php coding (that’s why I made this account). Perhaps I’m doing something wrong.
epiz_31101759
Website: http://dragondata.42web.io/
No error message, the page just doesn’t load when I paste PHP Form code into my index.php
I’m new to php coding (that’s why I made this account). Perhaps I’m doing something wrong.
My form code that caused to page to break:
/*<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
*/
/*
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
*/
<!-- COMMENT -->
, NOT /* comment */
welcome_get.php
and welcome.php
?Thank you Greenreader9 for your prompt response. I don’t understand how to paste a link to a form or " copy and paste the contents of welcome_get.phpand
welcome.php," so I’ll just paste the whole contents of index.php:
<!DOCTYPE html>
<html>
<head>
<title>MinimalHTML</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph kjhkkhb.</p>
<img src="CIMG0411.JPG" alt="Funny Picture">
<?php
echo "Hello World!";
$txt = "abcdefg";
$x = 5;
$y = 10.5;
echo $txt;
/*<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
*/
/*
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
*/
?>
</body>
</html>
That’s my code. It works, but not when I uncomment either of the form code snippets. I don’t know anything. I just copy pasted the forms code from W3 Schools.
Thank you, Yue4Long2.
Sorry, but still confused. Right now, you have an HTML form, not a PHP form (You need a backend for a PHP form). And what do you mean “It works, but not when I uncomment either of the form code snippets”?
Can you tell me the end goal (Expected behavior) here?
The problem here (in that case) is that you copy/put html form code in an PHP tag. This will of course results in an error. The PHP will run but stops at where "<form … " because it is not a valid PHP syntax.
This should at least fix that code:
<!DOCTYPE html>
<html>
<head>
<title>MinimalHTML</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph kjhkkhb.</p>
<img src="CIMG0411.JPG" alt="Funny Picture">
<?php
echo "Hello World!";
$txt = "abcdefg";
$x = 5;
$y = 10.5;
echo $txt;
?>
<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
Observe. Will that fix your problem? I actually cant understand the problem thoroughly.
you can’t make it like that. let me just point out where you are wrong
the html form you are adding inside of the php code won’t work, because inside of <?php
and ?>
tags, you are supposed to put php inside there, not the html
what you should do is
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
and for the php, just
$name = $_POST['name'];
$email = $_POST['email'];
/* your code for the form here */
the reason for this is basically, php is a dynamic programming language and it is used to make backends, not frontend.
Hello Greenreader9, thanks for bearing with me and my noob questions. I’ve been using embedded PHP inside of an HTML doc, using the <? /> to embed. (this is the way the W3 Schools tutorials do it). But I had to rename the index.html to index.php for the page to load.
What I’m trying to do is get input from the user and echo it back. Eventually I will want to store the input in a hosted database. That’s THE reason to learn PHP, right?
Thank you, Yue4Long2.
Thank you DigibytechsOrg for your helpful reply. After re-reading the W3 Schools forms tutorial, I think I see what I’m doing wrong- It seems the form is supposed to refer to a separate .php file.
Thank you, Yue4Long2.
Actually, you embed HTML in a PHP document, but close enough
PHP tags are defined as <?php
and ?>
or <?
and ?>
, NOT <?
and />
Correct. You need the PHP extension in order to run PHP code.
Can you please link that article for me? Because the code you shared is just wrong.
Try this:
<!DOCTYPE html>
<!--NOTE: Code is untested. It has no security issues, but is not guaranteed to work.-->
<html>
<head>
<title>MinimalHTML</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph kjhkkhb.</p>
<!-- You do not want to use "action",
as that means that the form is trying to send the form contents to a file that does not exist.
The code now is small, and can stay in one file-->
<form method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input name="submit" type="submit">
</form>
<!-- Collect the info from the form and echo it out -->
<?php
if($_POST['submit']){
echo "Name: "
echo $_POST['name'];
echo "<br>"
echo "Email: "
echo $_POST['email'];
}
?>
</body>
</html>
No, it’s not THE reason. PHP can be used to preform mathematical problems, harder calculations, display information, send emails, etc
I recommend that you check out this link, and learn more about PHP and how it works. Feel free to ask any clarifying questions!
Awesome, Thanks Greenreader9. I’ll try your code and I’m reading your link now. Here’s a link to the W3 Schools tutorial-
I was mis-interpreting it as 2 solutions rather than 2 files.
Thank you for your reply axtrct. It seems I was mis-interpreting the W3 Schools tutorial.
Understood.
Note that you should also validate the input on the server (Using PHP), otherwise someone could enter an HTML script tag containing JS code that could start a malicious program. For example…
<?php
if($_POST['submit']){
echo "Name: "
echo htmlspecialchars($_POST['name']);
echo "<br>"
echo "Email: "
echo htmlspecialchars($_POST['email']);
}
?>
Great, thanks Green! OK, I think I’ve got my work cut out for me…
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.