Being a total newbie in PHP, i have issues with implementing the Google reCaptcha on my website.
I oriented myself towards an easier solution which would be: if this field is empty, SEND the form.
Here is my code:
<form method="POST" action="/#contact">
<!-- Name + mail -->
<input type="text" required placeholder="Name" name="name" />
<input type="email" required placeholder="Email" name="email" id="email" value=""/>
<select name="subject" id="category">
<option value="">Subject</option>
<option value="1">1</option>
<option value="1">2</option>
<option value="1">3</option>
<option value="1">4</option>
</select>
<input id="submit" name="submit" type="submit" value="Send"/>
<input type="reset" value="Clear" class="alt" />
<div>
<label>If you're human, leave this field epmty</label>
<input type="text" name="comment">
<?php
if(empty($_POST['comment'])) {
// the field is empty
}
?>
</div>
</form>
My problem is: the form is still being sent, even there is something in the field.
Thanks in advance for your help.
There are 2 solutions to this:
Call a php file that validates the inputs. When a user hits the send button, it goes to a php page. This page retrieves the values of the filled in form and validates it. If an invalid input is found, you can call the main page (ex. index.php) with an error parameter in the url (ex. index.php?error=botfound). And in the beginning of your index.php file, you can write a check if there is an error parameter and display the error message if you want.
Let javascript validate the values of the inputs before the values are sent to a php file. To do this, you change your code from <form method="POST" action="/#contact">
to:
<form method="POST" action="/#contact" onsubmit="DoSubmit();">
With a javascript function something like:
<script>
function DoSubmit(){
if(document.getElementById('idOfInput').value != ""){
alert("Wrong input"); //Or you can actually add an error message to your page.
return false;
}
return true;
}
</script>
I think that script will make sure if the value of the input with id 'ifOfInput' is not empty, it'll give an alert and will not call the php script.
(By the way, you can also make that input invisible so it doesn't annoy normal users but bots will still fill in the input.)
Hope this helped :)
Related
I am trying to access the the textarea value which is part of one form in another form, but I am not able to do so. The code has been attached as follows.
<html>
<head></head>
<body>
<form action="board.php" method="POST">
<textarea id="text_message" name="new_message" rows="10" cols="100"></textarea>
<input type="submit" name="message" value="New Post"/>
</form>
<form action="board.php" method="GET">
<?php
echo '<table>
<tr><td><a href="board.php?reply=12345</td></tr>
</table>';
if(isset($_GET['reply'])){
$message= $_POST['new_message'];
echo $message;
}
?>
</form>
</body>
</html>
Getting an error saying Undefined index: new_message in \board.php on line 47
I assume you're trying to make a field populated with an old message when replying, but where do you intend to get the new_message? Your link (a href) does not send the new_message (also, it is missing the rest of the link information). Currently it looks like reply and new_message will never be populated at the same time, as they come from different forms. Maybe you meant to have a hidden field in the first form? e.g.
<textarea id="text_message" name="new_message" rows="10" cols="100"></textarea>
<input type="hidden" name="reply" value="12345"/>
<input type="submit" name="message" value="New Post"/>
</form>
When you click that submit button, you would have both a reply and a new_message field (although they would both be in _POST). In short, a description of a more full desired workflow would be helpful here.
I have the below for that works well, but is open for spam bots.
I want to put in a honeypot, not a captcha.
The code below works with the validation for the name, email, message,
but I can not get it to work with the honeypot.
Can anyone look at the "honeypot" code and tell me how to fix it?
I would like for the form to give an $success2 = "No Spamming allowed" that acts like the form was submitted, but does not actually submit the form.
Thanks
The Form:
<form id="contactform" action="send2.php" method="post"><div id="success"></div><div id="error"></div>
<label for="name">Name:</label><input type="text" id="name" name="name"/>
<label for="email">Email:</label><input type="text" id="email" name="email"/>
<label for="message">Message:</label><textarea id="message" name="message" rows="12" cols="20"></textarea>
<label id="robot">Are you a robot?</label><input type="text" name="robot" id="robot">
<input type="submit" value="Send your message" id="send" />
</form>
The PHP:
can be found here: http://goviewmy.com/contact/showcode/
Sorry, but i cannot get the PHP code to post in this question, so I attached a link to it.
Thanks
Honeypots work best if they have a field name that sounds legit, they should also be hidden using javascript to change the css after the page loads. (Most) bots don't have javascript enabled so they cannot process that this field should not be filled out.
I use something like this:
<div class='req'>
<label for='website'>Leave blank</label>
<input type='text' name='website'>
</div>
Hide it with jquery:
$(document).ready(function(){
$(".req").hide();
});
reject it server side if the field is filled out with something like this
if($_POST['website'] != ''){
echo "It appears you are a bot!";
}
else{
//process the rest of the form
}
I want to send a couple of form fields as a POST request to my PHP page, but I can't get it to work. Here is my code:
PHP login.php
<?php
if(!ISSET($_POST["username"]) && !ISSET($_POST["password"])) {
include "login.html";
}
else {
echo "hi";
}
?>
HTML login.html
<form action="login.php" method="post">
<label for="username">Username</label><input type="text" id="username"/>
<label for="password">Password</label>Password<input type="password" id="password"/>
<input type="submit" value="Submit"/>
</form>
Can anyone spot my mistake?
Your inputs do not have names. The id is used for client-side referencing, but it is the (non-unique) name attribute that is used to determine the key for a value when the data is submitted. A form control cannot be successful (i.e. in the form data) without a name.
You haven't included the name attribute in your html input elements. name attribute is used when passing form information to the webserver. id is primarily used for javascript based manipulation.
Username<input type="text" name="username"/>
Password<input type="password" name="password"/>
I am a bit new to Javascript. I have a html form with multiple input types but with the same names occurring multiple types. I need to validate the form before inserting the data in the form into the database. I am able to update and insert into the database without JS validation but am unable to perform the validation part using javascript because of complications with handling inputs with the same names.
Here is the form:
<form name="confirmation" method="post" action="appointment.php" onsubmit="return check()">
<label>Restaurant Name: </label>
<input name="r_name[]" type="text" value="<?php echo $row[0]?>"><br>
<label>Appointment Date: </label>
<input name="app_date[]" type="date" value="<?php echo $row[1]?>"><br>
Confirm:<select name="confirm[]">
<option value=""></option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select><br><br>
</form>
Here is the php code to update the database:
<?php
if(isset($_POST['submit'])){
$names= $_POST['r_name'];
$dates=$_POST['app_date'];
$confirm=$_POST['confirm'];
//echo $count;
for($i=0;$i<$count;$i++){
//echo "success";
$value=$confirm[$i];
if($value=="yes")
$value="y";
else
$value="n";
// echo $value;
$result= mysql_query("update appointment set confirm='".$value."' where rname='".$names[$i]."';");
}
}?>
To insert/update the data with same names in the form into the database I had to associate [] to their respective names for it was the easy way out. But I am having problem assessing it in javascript for the validation part. Could anyone suggest me how to go about doing it with a small code snippet with any of the names used for the input types used in the above form. Thanks.
If the names are the same for multiple input elements, you can use unique ids on the element if you want to target a specific element or you can use classes to target a group of similar elements.
<form>
<input name="myInput[]" id="myInput_0" class="myInput" type="text" value="">
<input name="myInput[]" id="myInput_1" class="myInput" type="text" value="">
<input name="myInput[]" id="myInput_2" class="myInput" type="text" value="">
</form>
Now you can target myInput[0] as getElementById('myInput_0') or all myInput[]s as getElementByClassName('myInput')
Try document.getElementsByName
https://developer.mozilla.org/en-US/docs/DOM/document.getElementsByName
you can use this jquery library http://www.position-relative.net/creation/formValidator/demos/demoValidators.html
You can use this validation after de excute the submit post and the librery make all the validations.
I'm trying to set up an html form that allows the user to select a field from a drop-down menu, then specify the information for that field. For example, if they select "Name" from the menu, then enter a name, it posts a specific response.
Here's how the html looks:
<form action="hello.php" method="post">
<select>
<option disabled="yes" selected="yes">Select...</option>
<option value="name">Name</option>
<option value="gender">Gender</option>
<option value="birthday>Birthday</option>
<input name="name" type="text" />
<input type="submit" value="Submit">
</form>
What I'm trying to do is have the php code change responses based on what's submitted. Here's the php code:
$name = $_POST['name'];
$gender = $_POST['gender'];
$bday = $_POST['birthday'];
With the responses:
echo "Hello, $name!";
echo "Your gender is $gender!";
echo "Happy birthday on $birthday!";
depending on what option they select. (I haven't finished the if/elseif/else statements yet because I'm not sure if they're influenced by how the information is recieved.)
Is it possible to do this? I know that my code will only ever return $name in this example, because
<input name="name"...>
But I don't know if it's possible to have the text field's information identified by the drop-down menu. I imagine it's probably possible with Javascript, but I've never used Javascript and wouldn't know how to do this.
Thanks!
EDIT: I found an answer. I ended up setting
<input type="text" name="submit" />
in my html code, and
$submit = $_POST['submit'];
in my php code. Now if I do something such as
echo $submit;
it returns whatever I entered in the html page. Thanks again!
<form action="hello.php" method="post">
<select name="myselectbox">
<option disabled="yes" selected="yes">Select...</option>
<option value="name">Name</option>
<option value="gender">Gender</option>
<option value="birthday>Birthday</option>
<input name="name" type="text" />
<input type="submit" value="Submit" />
</form>
Then in hello.php do this at the top of the file.
var_dump($_POST);
that should help you understand the relationship between a html form and the PHP Postback Form Handler (in your case hello.php)