I know there are lots others question like this and i found 1.000 answer on the web but none of those work with my code :(
So can someone help me with how to keep email value after submit?
<form name="login-registration" onSubmit="return validateForm()" method="post" action="" >
<label>Email</label>
<input type="email" name="emailinput" id="emailinput" value ="" />
<p id="emptyEmail" class="hidden">Email field is required</p>
<p id="invalidEmail" class="hidden">Email you insert is invalid!</p>
<label>Your password</label>
<input type="password" name="pswinput" id="pswinput" value=""/>
<p id="pswMinMax" class="hidden">Password should be from 4 to 8 caracters</p>
<p id="pswLettNum" class="hidden">Password should be letters and numbers. No special caracters are allow</p>
<label>Repeat password</label>
<input type="password" name="pswrepeatinput" id="pswrepeatinput" value="" onblur="isValidPswRep()"/>
<p id="pswR" class="hidden">Your passwords is different</p>
<input type="checkbox" id="policy" name="policy" value="policy" /> <label>I agree</label>
<p id="checkN" class="hidden">You must agree to our term</p>
<input type="submit" name="submit" value="Login" />
</form>
I try to put some code like:
<input type="email" name="emailinput" id="emailinput" value = "<?php echo htmlspecialchars($_GET['lastname']); ?>" />
But that php line is just displayed inside the field input.
try using $_POST['lastname'] instead of $_GET['lastname']
1)If i am not wrong,i don't see any field with name="lastname" in your code above.
2)Use $_POST because you are posting your form data with method="post".
Assuming that your file have .php as extension and php is installed on your server i would like you to notice that you have an error because you used a POST form while when you apply value to your input field you are trying to use $_GET Further more you did not assign lastnameto any input field, so use emailinput as you apply to this field name="emailinput". You should change htmlspecialchars($_GET['emailinput']); to be htmlspecialchars($_POST['emailinput']);
So your code would look like this
<input type="email" name="emailinput" id="emailinput" value = "<?php echo htmlspecialchars($_POST['emailinput']); ?>" />
This should print your variable inside your input field
There are at least 2 problems there.
The fact that the php is displayed inline suggests that either you have wrong file extension (like the comments suggested), either file is not included in your "know how to read by php" (for lack of a better way to say it, my English is not perfect) directory.
You echo a get when you sent a post (as other answer suggested)
Also... WHY DOES YOUR QUESTION HAVE JS TAG (sorry for caps, wanted to be sure it sticks out)?
Related
I have a form for asking information about courses , every course has it page, but the information page is one for all.
The form should be something like that:
<form action="#" method="POST">
<label for="name">Name</label>
<input name="name" type="text">
<label for="email">Email</label>
<input name="email" type="email">
<input type="hidden" id="code" value="<?php echo $course_code; ?>">
<input id="submit" type="submit" value="Invia" />
</form>
I wish to change the var $course code according to the referrer page. (With a $_GET var)
I tried "Shortcode Exec PHP" plugin to execute php in wp pages, but doesnt work.
When you POST the form, the variable won't be set in $_GET but in $_POST. It's either one or the other, so if you want to read the $_GET var, you must also use GET on the form, like this:
<form action="#" method="GET">
<label for="name">Name</label>
...
(this is what Fred commented on, but I couldn't expand upon that comment due to my low rep)
I was wrong to use "Shortcode Exec PHP" plugin.
I set a shortcode:
$course_name = $_GET['cn'];
$courses= array("courses1","courses2","couses3");
if (in_array($course_name, $courses)) {
echo $course_name:
}
and the in the wordpress page can be used the name of the shortcode
[couse_name]
Now its work!
You can just use $_REQUEST so it doesn't matter if its a POST or a GET from the form. But I wouldn't use GET from a form unless it was a search or something where the user could bookmark the url and see the result. Mostly use POST for all other instances.
HTML form...
<form method="post">
<label>Name<br>
<input type="text" name="name">
</label>
...
<input type="submit" value="Invia">
</form>
PHP page that handles the form...
<?php
// $_REQUEST will contain POST, GET & COOKIE
echo $_REQUEST['name'];
?>
Well, I have 2 php files. The first one user form.php has an html form to collect user inputs and send those data to other php file mysql_insert.php via POST method.
The problem is that along with 6 user inputs, I also have to transfer one more variable count(as discussed in code below) which is not input from user.
I tired following approach:
user form.php--->
<?php
$count=file_get_contents("cnt.txt","r");
$count=$count+1;
echo"Welcome!<br/>You are student number $count.<br/>";
?>
<html>
<p>Fill in the following information to save your marksheet to the database:</p><br/>
<form action="mysql_insert.php" method="POST">
Name:<input type="text" name="name" value=""/><br/><br/>
Marks(out of 100)<br/>
Subject 1:<input type="text" name="sub1" value=""/><br/>
Subject 2:<input type="text" name="sub2" value=""/><br/>
Subject 3:<input type="text" name="sub3" value=""/><br/>
Subject 4:<input type="text" name="sub4" value=""/><br/>
Subject 5:<input type="text" name="sub5" value=""/><br/><br/>
<p name="count" value="$count"></p>
<input type="submit" value="Submit"/>
</form>
</html>
I simply named an empty P element as count and set its value as $count to send it along with input variables(correct me if there is something wrong here, I am very novice to php).
And on the receiving end I used following code:
mysql_insert.php--->
<?php
require("connect.php");
$name=$_POST['name'];
$s1=$_POST['sub1'];
$s2=$_POST['sub2'];
$s3=$_POST['sub3'];
$s4=$_POST['sub4'];
$s5=$_POST['sub5'];
$count=$_POST['count'];
.
.
.
?>
Now on the line $count=$_POST['count']; the browser is throwing the error:
Notice: Undefined index: count in C:\xampp\htdocs\Vikas-117-PHP\level 3\mysql_insert.php on line 10
It seems the count is not being posted to this file.
Please guide me where I am wrong.
P.S.: I can of course use the file_get_contents() in the mysql_insert.php and get the count value directly in this file, but I am considering that way as my last option.
So please help if the non user-input variable can be posted via forms???
Thanks a million!!!
you have to put your $count variable in a hidden field in your form.. The data which are put in form inputs only get posted in form submit..
<input type="hidden" name="count" value="<?php echo $count ?>">
<p name="count" value="$count"></p>
you can not use directly any html tag for post data only use html form fields for posting data.
for use html tag values post you need to use js/ajax/php
<p name="count"><?php echo $count;?></p>
or better you use
<textarea name="count"><?php echo $count;?></textarea>
or for data not showing use hidden field
and get by name this p value or use class or id for get and post data using ajax
<p name="count" value="$count"></p>
This is not an input element so you cant post directly. You can however use jquery to get the value and POST using js.
You need to have the value inside input type text or hidden if you want to do the way you are doing now.
Change this line : <p name="count" value="$count"></p>
to the : <input type="hidden" name="count" value="<?=$count?>" />
You can use hidden elements like the following
use
<input type="hidden" value="<?php echo $count; ?>" name="count"/>
instead of
<p name="count" value="$count"></p>
Instead of
<p name="count" value="$count"></p>
Use
<input type="hidden" name="count" value="<?php echo $count;?>">
this line will not work : <p name="count" value="$count"></p>
you can use <input type="hidden" name="count" value="<?php echo $count; ?>" />
This doesn't get posted if it's hidden like this, how do i get around it?
<input name="Pid" style="display:none"/>
and PHP.
$Pid = preg_replace ('#[^0-9 ]#i', '', $_POST['Pid']);
Kindly use it as:
<input name="Pid" type="hidden" />
You can use hidden field simply setting type as hidden
<input type="hidden" value="" name="pid"/>
If you really need your input type to remain, like text or checkbox, use a <div style='display:none;'></div> tag to hide your input object.
Sometimes, this is required if your submit code does not include checks like
if (isset($_POST['Your_Object_name'])) {...}
Sometimes you don't want to show them, but neither do you want your code to issue error messages.
Your code will then be as follows
<div style='display:none;'>
<input name="Pid" type="text" />
</div>
Et voila
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
}
<form action="test.php" method="post">
Name: <input type="text" name="fname" />
<input type="hidden" name="fname" value="test" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
How can I read the values of both the fields named fname?
On my action file(test.php) under $_POST, I am getting only hidden field value.
Is there any PHP setting through which I can read both values?
I believe you want to name the fields as:
Name: <input type="text" name="fname[]" />
<input type="hidden" name="fname[]" value="test" />
to make PHP understand them as an Array.
In case someone wants to do this and doesn't want to change the name of the form elements, or can't, there is still one way it can be done - you can parse the $_SERVER['QUERY_STRING'] or http_get_request_body() value directly.
It would be something like
$vals=explode('&',http_get_request_body());
$mypost=Array();
foreach ($vals as $val) {
list($key,$v)=explode('=',$val,2);
$v=urldecode($v);
$key=urldecode($key);
if ($mypost[$key]) $mypost[$key][]=$v;
else $mypost[$key]=Array($v);
}
This way $mypost ends up containing everything posted as an array of things that had that name (if there was just one thing with a given name, it will be an array with only one element, accessed with $mypost['element_name'][0]).
For doing the same with query strings, replace http_get_request_body() with $_SERVER['QUERY_STRING']
If you want to pass two form inputs with the same name, you need to make them an array. For example:
<input type="text" name="fname[]" />
<input type="hidden" name="fname[]" value="test" />
You can then access it using the following:
$_POST['fname'][0]
$_POST['fname'][1]
You might want to rethink whether you really need to use the same name though.
Solutions are
1) Try using different name for textbox and hidden value
2) Use an array as mentioned above for field name
3) Its not possible as the values will be overwritten if the names are same