This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 4 years ago.
I am new to PHP and I am trying to make a contact us form that will perform validation and will display an error if any of the fields are not entered correctly.
I tried following this tutorial W3schools php example
so this is how my form looks like
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website: <input type="text" name="website">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
but I get an error saying
Error 404 - Not Found
The document you are looking for may have been removed or re-named.
Please contact the web site owner for further assistance.
and my url changes to this
my domain name/%3C?php%20echo%20htmlspecialchars($_SERVER[
what am I doing wrong here? Also if this is not the right approach, can someone make a suggestion how can I display the error messages and success messages after validating and sending the email via PHP?
Update:
Ok, so first of all, the problem was the file extension so thank you Chris for your helpful comment. If you want, add your comment as an answer and I will select it as a correct answer.
Second. I had a problem when upon changing the extension and my href attributes, the server was still taking me to contactUS.html page instead of contactUs.php page. The problem was that my links were generated in JavaScript and for some reason, my browser was not using the updated javaScript files. I solved this by clearing my cookies.
Use double quotes arround php tag and single quotes for $_SERVER key. Like use below example then it will work for you.
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" >
Related
This question already has answers here:
Checking if form has been submitted - PHP
(9 answers)
Closed 12 months ago.
I'm novice and i want save images from a Form in php.
my code is pretty simple:
<form method="POST" enctype="multipart/form-data" action="">
<label for="nom">Nom :</label><input type="text" name="nom" id="nom">
<label for="adresse">Adresse :</label><input type="text" name="adresse" id="adresse">
<label for="tel">Numéro de télephone :</label><input type="text" name="tel" id="tel">
<label for="mail">E-mail :</label><input type="text" name="mail" id="mail">
<label for="web">Url du site web</label><input type="text" name="web" id="web">
<label for="images">Logo/image</label>
<input type="file" multiple name="images" id="images" />
<input type="submit" class="bouton" id="valider" value="valider"/>
</form>
and I just want see if my images arrive in my $_FILES.
so i have at the top of my index.php
<?php
echo var_dump($_FILES);
?>
when i press the submit button the 1st time, it's still empty, but if i press it a second time, i get it.
do someone knows why ?
thank you.
The first click is putting you on the webpage of the form as it
can't work without fields populated.
The second click is running your action with files inside it, so
it works.
Why ?
Because you are displaying your form in various webpages, you must handle your form action in a website global point of view. You are using php so try to use the layout feature for html is one thing, but do not forget to do the same for handling your POST action globally.
I'm using Wamp server to code a website on my PC, I'm following a tutorial on W3Schools for <form> and data collection with PHP (the GET method) and even though I followed the tutorial the code doesn't work.
I researched it a little here but no one seems to be able to answer my problem.
<form action="welcome_get.php">
<section class="contact bg-primary" id="contact">
<div class="container">
<h2>Abonnez vous par mail!</h2>
<input type="email" id="email" size="50" placeholder="Entrez votre adresse e-mail"></input>
</div>
<div class="mail-submit">
<input type="submit" id="submit" value="Je m'abonne"></input>
</div>
</section>
</form>
That is the HTML before I send the email address, and this is the simple page to GET the email:
<html>
<body>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>
Yet it doesn't work at all. Here is the tutorial i followed
https://www.w3schools.com/php/php_forms.asp
Update
I made a mistake. I was opening the FILE through my browser NOT accessing through Wampserver.
Now the issue is when I click on Submit button (that calls the
<form action="welcome_get.php" method="get">) it downloads the welcome_get.php.
Your form is not set method POST or GET :
<form action="welcome_get.php" method="get">
First :
add method GET
<form action="welcome_get.php" method="get">
Second :
add attributte name in input
<input type="email" name="email" id="email" size="50" placeholder="Entrez votre adresse e-mail"></input>
Final :
make sure your extension is .php not .html
I think your problem is related to your cache browser, what I advise you to use
another browser to test your PHP script or try to clear your cache browser and then it should work.
This question already has answers here:
Send email with PHP from html form on submit with the same script
(8 answers)
Closed 5 years ago.
The question is how do i make the submit button work so if they press submit, the ff will be send to my email (ex: example#gmail.com).'
If PHP is needed please post the code so i just gonna paste it on my code :D ty
<div id="forms">
<form class="form">
<p class="name">
<input type="text" name="name" id="name" placeholder="Enter Your Name" />
</p>
<p class="email">
<input type="text" name="email" id="email" placeholder="mail#example.com" />
</p>
<p class="text">
<textarea name="text" placeholder="Describe your logo" /></textarea>
</p>
<p class="submit">
<input type="submit" value="Send" />
</p>
</form>
</div>
I cannot find the answer in the existing question of this, So i decided to make another one... the existing is like 2 years ago?
I think you just need to put a mailto action on the <form> tag, i.e.:
<form class="form" action="mailto:someone#example.com">
All you need to do is change your form tag to look like this:
<form action="mailto:address#example.com">
This will send unformatted data to your email address without needing PHP. If you would rather format it in a specific way, then you can use the PHP mail function.
I also removed class="form". Typically this is not needed, but it depends on your reason. If you are using class for CSS you can just reference the element form rather than the class name .form.
I have a free form search that I moved from my main content to my header. Once I moved the form into the header the text inside of it stopped showing. The placeholder works but when you begin typing it is completely blank. The search and the functionality work perfectly, I just cannot see what I am typing. Any ideas?
<form class="search" action="/all-results/" method="get">
<fieldset>
<span class="text"><input name="searchProducts" value="All" type="hidden">
<input type="hidden" name="search_selection" value="all">
<input name="byString" id="s" type="text" value="" placeholder="<?php echo __('Search','Avada'); ?>" /></span>
</fieldset>
<form name="input" action="/all-results/" method="get">
<input type="submit" id="search_button" value="Search">
</form>
</form>
Your HTML is invalid. You can not nest a <form> element inside another <form>. Messing up forms and tables are two of the best ways to produces peculiar browser behaviour, so my guess is that this is your problem.
If you correct your HTML (validate it!) and the problem persists, then post a minimal HTML page with CSS so that there is something for us to debug.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to determine if user selected a file for file upload?
I would like a simple PHP form validation. My form has two inputs: A link and a file to upload. The form should be submitted only if the link field is filled and the user selected a file to upload. If one of them is or both are false, an alert box should be displayed (like here when you click Upload without selecting a file).
This is my form:
<form action="upload_file.php" enctype="multipart/form-data" method="post">
<p>Link:<br>
<input type="text" name="link" size="50">
</p>
<p>Image:<br>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input type="file" name="file" id="file" size="40">
</p>
<div>
<input type="submit" name="upload" value="Upload">
</div>
</form>
I am new to PHP and I have been browsing tutorials for hours now, many of them was of no help or shows the server side validation.
I understand I should start with this:
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
to validate the fields before the form is submitted. I have some basic knowledge of PHP but I cannot figure out how to do this easy or not so easy task.
php is server side. What you need is a client side checker (js, jq ..)
You can perform the check only with php, but on your server, then return to the client the error message or the task complete message. If you want the check to be done client side, you need a client side language to handle it.
PHP is indeed a server-side application and thus cannot perform client-side validation.
If you really want to do client-side validation, you'll probably have to use javascript. Have a look at the jQuery validation plugin. Here is a big demo page showing you some of the possibilities: http://jquery.bassistance.de/validate/demo/
For more help and information, also have a look at the jQuery.com/plugins/validation page.
(Note the WARNING at the end of this post)
Example:
(the javascript)
<script>
$(document).ready(function(){
$("#commentForm").validate();
});
</script>
(the form)
<form class="cmxform" id="commentForm" method="get" action="">
<fieldset>
<legend>A simple comment form with submit validation and default messages</legend>
<p>
<label for="cname">Name</label>
<em>*</em><input id="cname" name="name" size="25" class="required" minlength="2" />
</p>
<p>
<label for="cemail">E-Mail</label>
<em>*</em><input id="cemail" name="email" size="25" class="required email" />
</p>
<p>
<label for="curl">URL</label>
<em> </em><input id="curl" name="url" size="25" class="url" value="" />
</p>
<p>
<label for="ccomment">Your comment</label>
<em>*</em><textarea id="ccomment" name="comment" cols="22" class="required"></textarea>
</p>
<p>
<input class="submit" type="submit" value="Submit"/>
</p>
</fieldset>
</form>
WARNING
Please keep in mind that javascript, being client-side, runs on the client's computer. Don't rely on javascript validation alone. Anyone can disable javascript and insert 'wrong' values in your form.
In case of form it's best to do client side validation using some javascript library like jQuery. Check out this jQuery tool for form validation.
For server side validation, check out this link. It may help you.
Validation should be done on server side ALWAYS. You can add client minor validation also as a confort for the user with javascript.
Here some basic code to show a message if fields empty using minor validation:
<input type="submit" name="upload" value="Upload" onclick="var ref = document.getElementById('file'); if ( ref.length == 0 ){ alert( 'select file' ); return false; } ref = document.getElementById( 'textfieldid' ); if ( ref.value.length == 0 ){ alert( 'fill link, better use a regexp to match a url but this another store' ); return false; }">
For client side validation of "File upload" field, refer following plug-in :
http://adamsanderson.github.com/jQuery-File-Validator/
It is useful. I have tried it.