Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need your help with some form/php code:
2Fields first for name 2nd for fam.name
If the "User" fill both fields its echo what he filled in
If the "User" dont fill any field he get form back with an error message
Same if he just fill only 1 of the 2 fields
My question is if the "User" just fill 1 field and send it (submit) i want that he gets the error message like now but also save his input (the date he filled in the field) so he only has to fill the other field and not both again.
I tried it allready with session_start(); and stuff but failed.
<?php
$errormessage="";
//check if 1 of 2 fields is empty
if(empty($_POST['vorname'])){
$errormessage=$errormessage." First field ,";
}
if(empty($_POST['nachname'])){
$errormessage=$errormessage." Second field ,";
}
//Submit starts
if (isset($_POST['go'])) {
if (!empty($_POST['vorname'])&& !empty($_POST['nachname'])){
echo "First field : ".$_POST["vorname"]." ";
echo " 2nd field: ".$_POST["nachname"];
}//end empty
//ELse if inputs empty
if (!empty($errormessage)) { ?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="text" name="vorname" />
<input type="text" name="nachname" />
<input type="submit" name="go" />
<?php echo "error: ".$errormessage; ?>
</form>
<?php } //ende if errormessage
}//ende if isset go
else {//start side ?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="text" name="vorname" />
<input type="text" name="nachname" />
<input type="submit" name="go" />
</form>
<?php} //end else?>
If user specified some info only for one field and send it, why don't you paste correct filled data from $_POST var into value attribute of one of yours inputs:
<input type="text" name="vorname" value="<?php echo $correct_data ?>"/>
and show additional error message, like you do?
I mean, you can save old $_POST['vorname'] and $_POST['nachname'] into two extra vars, if one of the $_POST fields is empty, one of this var is empty, then add this vars in value attrubutes of your inputs in html form:
$errormessage = "";
$old_vorname = empty($_POST['vorname']) ? "" : $_POST['vorname'];
$old_nachname = empty($_POST['nachname']) ? "" : $_POST['nachname'];
....
//ELse if inputs empty
if (!empty($errormessage)) { ?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="text" name="vorname" value="<?php echo $old_vorname; ?>"/>
<input type="text" name="nachname" value="<?php echo $old_nachname; ?>"/>
<input type="submit" name="go" />
<?php echo "error: ".$errormessage; ?>
</form>
Value attribute of html input element contains it actual content, you can see on webpage or edit.
Related
I am having an issue trying to get my form with a hidden input field to be recognized when I check if it isset. Not sure if it is because I am running the form in a loop or what. I do know when I click submit for any of the fields from the loop the value of the hidden field is registering properly, it is just somehow the 'form1' is not registering as isset. Any ideas?
<?php
if(isset($_POST['form1']))
{
echo $_POST['cid'];
} else {echo "nope!";}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" name="form1">
// gets array
$listchars = $user->getCharacterList($_SESSION['user_id']);
// loops through array
foreach($listchars as $chardata){
echo $chardata['name']; ?>
<input type="hidden" name="cid" value="<?php echo $chardata['cid'];?>">
<input type="submit" name="submit" value="submit">
<?php } ?>
</form>
The form name is not submitted
Use
<input type="submit" name="form_name">
or
<input type="hidden" name="form_name">
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 want make some php test in my form and i've followed a tutorial but i don't get the result i wanted. I want to make sure that the borne's value isn't null. Then send the value to another page "exo.php" to use it.
The problem is when i add the php code inside input's value for example it's not cosindring it as php code but as a string so it prints the php code.
this is my code :
<?php
if (isset($_GET["submit"])) {
$borneErr="";
$borne="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["borne"])) {
$borneErr = "Missing";
}
else {
$borne = $_POST["borne"];
}
}
}
?>
<form class="" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<label> Borne : <input type="text" name="borne" value="<?php echo htmlspecialchars($borne);?>">
<span class="error">* <?php echo $borneErr;?></span> </label>
<button type="submit" value="OK"> Send !</button>
</form>
this is the result i get in this image below :
Your code raises notice
Undefined variable: borne
on this line:
<label> Borne : <input type="text" name="borne" value="<?php echo htmlspecialchars($borne);?>">
And also this notice
Undefined variable: borneErr
on this line:
<span class="error">* <?php echo $borneErr;?></span> </label>
You can fix that by defining the variable outside of the condition.
The form has a method="POST" attribute.
But you're checking the condition against GET data:
if (isset($_GET["submit"])) {
Also, you're checking existence of a field submit that is not included in the form data, since it's a <button>. You can either change it to <input> or change your PHP condition to check the borne field.
<input type="submit" value="Send !">
or
if (isset($_POST["borne"])) {
The check against $_SERVER["REQUEST_METHOD"] is now redundant so you can get rid of it.
The code could be simplified and polished even more but I'll leave it so it's easier see those errors fixed.
Working code:
<?php
$borneErr = "";
$borne = "";
if (isset($_POST["borne"])) {
if (empty($_POST['borne'])) {
$borneErr = "Missing";
} else {
$borne = htmlspecialchars($_POST['borne']);
}
}
?>
<form class="" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<label> Borne : <input type="text" name="borne" value="<?php echo $borne; ?>">
<span class="error">* <?php echo $borneErr;?></span> </label>
<input type="submit" value="Send !">
</form>
just add value inside quotation mark "HERE"
<input type="text" name="borne" value="<?php echo htmlspecialchars($borne);?>">
You can simply choose one here
<input type="text" name="borne" value="<?=$_SERVER["PHP_SELF"]?>"><Br>
OR
<input type="text" name="borne" value=""> /* leave it blank cause if blank i will submit automatically on the same page. */
please correct me here.
i have created multi page form where i want to pass data from each pages to final pages and then submit those on email. first one is apply.php, there are many input fields, but i have listed some of those, here when some enters passport number in passport field, i want this to be passed in everypage of the form and print this at couple of places on each page. here getting some issues when passing some of these fields.
this is first page ( apply.php )
<?php
// Start the session
session_start();
?>
<form name="search_form" method="post" onSubmit="return chk();" action="apply2.php">
<input name="passportno" id="passportno" type="text" maxlength="20" placeholder="Enter Passport No." size="43" >
<input name="birthdate" type="date" class="textBoxDashed" size="43" id="birthdate" datepicker="true" datepicker_min="01/01/1900" datepicker_max="21/11/2017" maxlength="10" datepicker_format="DD/MM/YYYY" isdatepicker="true" value="">
<input name="button1" type="submit" value="Continue">
this is apply2.php . here there is some issues, i am not able to find, as you can see below codes, i am able to print date of birth but not able to print passport no ( input from form1 ). Please correct where i am wrong here.
<?php
session_start();
$msg="";
////include("connect.php");
if (isset($_POST['button1']))
{
extract($_POST);
$code=strtolower($_POST['captcha_code']);
$sess=strtolower($_SESSION["code"]);
if ($sess==$code)
{
$appid=time().rand();
$result=mysqli_query($con,"select *from registration where email='$email'");
if (mysqli_fetch_row($result)>0)
{
?>
<script>
alert("This email is already exist");
</script>
<?php
}
else
{
$query="insert into registration values('$appid','$passportno','$birthdate','$email')";
if (mysqli_query($con,$query))
$msg="Data saved Successfully.Please note down the Temporary Application ID $appid";
else
echo "not inserted".mysqli_error($con);
if (!isset($_SESSION["appid"]))
{
$_SESSION["appid"]=$appid;
}
}
}
else
{
?>
<?php
}
}
?>
<form name="OnlineForm" method="post" onsubmit="return OnSubmitForm();" action="apply3.php">
<input name="applid" id="applid" value="<?php echo $_SESSION["appid"];?>">
<input type="hidden" name="birthdate" value="<?php echo $birthdate;?>"><b><?php echo $birthdate;?>
<input name="passportno" type="text" class="textBoxDashed" id="passportno" value="" size="43" maxlength="14" value="<?php echo $passportno;?>">
input name="sc" type="submit" class="btn btn-primary" id="continue" value="Save and Continue" onclick="document.pressed=this.name">
Don't use extract. Also do some checking to see if the data is set. As for not getting the the data try $_POST['passportno'] and if you want to pull the values and put them back into the input boxes simply use <?php echo isset($_POST['passportno'])?$_POST['passportno']:'' ?> to return nothing if it is not defined.
Also you need to do add some protection to your inputs.
You can add protection by using $passportno = mysqli_real_escape_string($con, $passportno);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm new to PHP and I just want a very simple insecure(for testing) login form.
I wrote this code: but I do not get what I want, can you correct what I did wrong or what should I add, correct ...
<html>
<body>
<form>
Username: <input type="text" name="name" value="<?php $name ?>" ><br>
Password: <input type"password" name="pass" value="<?php $pass ?>" ><br>
<input type="submit">
</form>
<?php
if ($name == "root" && $pass == "laptop") {
header("Location: /index.html");
exit;
}
?>
Your PHP writes HTML which is then sent to the browser for rendering - once it has completed sending to the browser it exits. Hence your first mistake appears to be that you haven't understood the seperation of what happens at the server and what happens at the client.
Your next mistake is that your PHP operates on variables which happen to have the same names as form elements - but form vales are not automatically converted into PHP variables.
Your third error is that even when you've fixed the 2 previous problems your script won't work: any HTTP headers must be emitted before writing to the html body - so your redirect isn't going to work. Consider instead:
<?php
if ('root' == $_POST['name'] && 'laptop' == $pass) {
header("Location: /index.html");
exit;
}
?>
<html>
<body>
<form method='POST'>
<?php print $_POST['name'] ? "Try again...." : "login..."; ?>
<br />
Username: <input type="text" name="name"
value="<?php print $_POST['name']; ?>" ><br />
Password: <input type"password" name="pass"
value="<?php print $_POST['pass'] ?>" ><br />
<input type="submit">
</form>
</body>
</html>
The php code you inserted into the form has no function, it does nothing. I edited your form, so that it sends data to the same page. You can then do:
$name = $_POST['name'];
$pass = $_POST['pass'];
Your old code comes after that.
You can put it in the same file index.php
<?php
if (isset($_POST['submit']))
{
echo 'you submitted name='.$_POST['name'].' And password='.$_POST['pass'].'<br /><br />';
}
echo '<form method="post">
Name <input name="name" type="text" /><br />
Pass <input name="pass" type="password" /><br />
<input name"submit" type="submit" />
</form>';
?>
Make two files - one with html and the other one with php function.
in html add to form action and method
<html>
<body>
<form action="php_file_name.php" method="post">
Username: <input type="text" name="name" value="" ><br>
Password: <input type"password" name="pass" value="" ><br>
<input type="submit">
</form>
and put in php_file_name.php this php code that you wrote, knowing that you're using POST method, that is:
<?php
if ($_POST[name] == "root" && $_POST[pass] == "laptop") {
header("Location: /index.html");
exit;
}
It should work then. Of course you can choose other name of php_file_name.php but with changing it accordingly
I am creating a faq panel for there can be multiple answers for question and i want to take the answer id .because i am storing comment by answer id
the problem is that how to sent the $answer_id to the comment_submit_process.php and how to recognize the answer ?
$selected_ques= mysql_prep($_GET['ques']);
$query = "SELECT * FROM formanswer where question_id = {$selected_ques}";
$ans= mysql_query($query);
if($ans){
while($answer = mysql_fetch_array($ans))
//here is the form
<form id="add-comment" action="comment_submit_process.php" >
<textarea class="comment-submit-textarea" cols="78" name="comment" style="height: 64px;"></textarea>
<input type="submit" name="submitbutton" value="Add Comment" class="comment-submit-button" >
<br> <?php
$ans_id= $answer['id']; //i am fatching the $answer['id'] from database
?>
<input type="hidden" name="ques" value="<?php echo $_GET['$ans_id'] ?>" />
<span class="counter ">enter at least 15 characters</span>
<span class="form-error"></span>
</form>
<?php }} ?>
You might have typo here !! it should be..
<input type="hidden" name="ques" value="<?php echo $ans_id; ?>" />
Other thing, you can add get param to action link it self.
<form id="add-comment" action="comment_submit_process.php?<?php echo $answer['id']; ?>" >
Instead of setting the ans_id, every time to the hidden field .
Generate a string of ans_id seperated with "," until while loop ends append the string and assign that value to the hidden field and in form action page you can get that value and generate van array from that string with delimiter ",".Now you can have the array of ans_id in your form action page
$answer_array = "nothing";
while($answer = mysql_fetch_array($ans))
{
if( $answer_array == "nothing")
$answer_array = $answer;
else
$answer_array .= ",".$answer;
}
<input type="hidden" name="answer_arr" value="<?=$answer_array?>">
In Form action page you can get that hidden value
$ans_array= explode(",",$_GET['answer_arr']);
You can echo answer_id in form action tag as additional parameter like this:
<form id="add-comment" action="comment_submit_process.php?ans_id=$ans_id" >
//Your stuff here
</form>
in comment_submit_process.php you can identify answer by
$ans_id=$_GET['ans_id'];
You can do further processing by using $ans_id
Edit:
change this line:
<input type="hidden" name="ques" value="<?php echo $_GET['$ans_id'] ?>"
to:
<input type="hidden" name="ques" value="<?php echo $ans_id; ?>" />
so that value of that field would be $ans_id fetched from DB.