This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 6 years ago.
i want to change value of 2nd input after submitting the value of first input but it's not happening. i want to print email in 2nd input value
<?php
$db = mysqli_connect('localhost','root','root','rahul');
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$sql = mysqli_query($db,"select * from register where email='$email'");
if(mysqli_fetch_array($sql)==true)
{
echo '<input type="text" name="question" value="$email">'; /* 2nd input */
}
else
{
echo "no";
}
}
?>
<center><br><br>
<form action="" method="post" >
<input type="text" name="email"><br><br> /* first input*/
<input type="submit" name="submit">
</form>
echo '<input type="text" name="question" value="$email">'; : This will not work because you have to use double quotes to echo php var in it
Like this :
echo "<input type='text' name='question' value='$email'>";
or like this :
echo "<input type='text' name='question' value='".$email."'>";
Related
This question already has answers here:
increment variable each time page gets called or opened?
(7 answers)
Closed 6 years ago.
Good day i have a form which its values are grabbed from a database,
<?php
$questionId = 1;
$pdo = new PDO("mysql:host=localhost;dbname=mydb", 'root', '');
$sth = $pdo->prepare("SELECT * FROM questions WHERE questionid = $questionId");
$sth->execute();
while ($row = $sth->fetch()):
?>
<form action="exam1.php" method="POST">
<?php echo "<h4>".$row['instructions'] ."</h4><br>"; ?><h4><?php echo $questionId. ". ".$row['name']; ?></h4>
<input type="radio" name="choices" id="choices" value="<?php echo $row['choice1']; ?>"><?php echo $row['choice1']; ?> ...(other choices)
<input type="submit" name="submitAnswers">
</form>
<?php endwhile;?>
what i want is how to increment the $questionId variable once the form is being submitted. i tried this
if (isset($_GET['choices'])) {
echo $_GET['choices'];
$questionId = $questionId + 1;
}
the question id updated but just once.
i need it to always update by adding 1 to it.
<form method="post" >
<input type="submit" name="submit">
</form>
<?php
session_start();
$_SESSION['id'];
if (isset($_POST['submit'])) {
$_SESSION['id']=$_SESSION['id']+1;
}
echo "Value is => ",$_SESSION['id'],"<br>";
?>
How to dynamically add and remove input fields the form will stay while page refresh using PHP?. could it possible to use session? please any PHP script for this? thanks in advance!!
You could use something like this
<?php
$showEnterOther = "";
$showEnterOtherAsWell = "";
if(isset($_POST["show_form"])) {
$showEnterOther = "<input type='text' name='whatever' />";
$showEnterOtherAsWell = "<input type='text' name='whatever' />";
}
?>
<form action="#" method="post">
<input type="text" name="username" placeholder="Enter Username" value="<?php if(isset($_POST['username'])){echo $_POST['username'];} ?>"/>
<?php echo $showEnterOther; ?>
<?php echo $showEnterOtherAsWell; ?>
<input type="submit" name="show_form" value="Continue!" />
</form>
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 9 years ago.
I am using the following code to change button when it is clicked by echoing the javascript's document.getElemenById property but the button remains as it is:
while($result=mysql_fetch_array($result1))
{
$i=1;
echo "
<tr>
<form action='student.php' method='post'>
<td>$result[0]<input type=\"hidden\" name=\"company\" value=\"$result[0]\"></td>
<td>
<input type=\"checkbox\" name=\"yes\" />
<input type=\"submit\" name=\"apply$i\" id=\"apply$i\" class=\"buttons\" style=\"border: none\" value=\"Apply now!\"
onclick=\"return confirm('Are You Sure you want to apply?');\">
<input type=\"hidden\" name=\"apply$i\" value=\"apply$i\">
</td>
</form>
</tr>
<tr><td><input type=\"text\" id=\"ankur\"></td></tr>
";
$i++;
}
echo "</table></center>";
Here the button with the id apply$i should change to Applied string as per the following code
$id=$_SESSION['sessionid'];
if(isset($_POST['apply1']))
{
if(isset($_POST['yes']))
{
$yes=trim(mysql_prep($_POST['yes']));
$company=trim(mysql_prep($_POST['company']));
$button_id=trim(mysql_prep($_POST['apply1']));
if($yes=="on")
{
$query ="select `Branch`,`Class` from `student_details` where `Userid`='$id'";
$temp1= mysql_query($query);
$temp2= mysql_fetch_array($temp1);
$query="insert into `student_applied` values ('$id' ,'$temp2[0]' ,'$temp2[1]' ,'$company')";
//$query="UPDATE `student`.`users` SET `Activated` = '1' WHERE `users`.`Userid` = '$student'";
$result = mysql_query($query);
confirm_query($result);
if($result)
{
echo "ok";
echo "<script language='javascript' type='text/javascript'>document.getElementById('apply1').innerHTML ='Applied';</script>"
Does anybody know the reason?Plz help
Instead of settingg $i inside loop, you should set it outside and then increment value,
like
$i=1;
while($result=mysql_fetch_array($result1)){
//Your rest of code to output content
$i++;
}
That way you could have different ID for each button, and Javascript should work perfectly for you.
I want to get a variable from a conditional if of form assigned to take the value of a textbox:
<form action="" method="POST">
<input type="text" name="name">
<input type="submit" value="Click Here!" name="submit">
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
echo "<br /><input type=\"submit\" value=\"Show it!\" name=\"show\">";
}
if (isset($_POST['show'])) {
echo $name; //i got "Notice: Undefined variable: name" here
}
?>
</form>
I want show value of $name after input:name pressed.
This should solve the issue
$name = "";
if (isset($_POST['submit'])) {
$name = $_POST['name'];
echo "<br /><input type=\"submit\" value=\"Show it!\" name=\"show\">";
}
if (isset($_POST['show'])) {
echo $name;
}
The problem in your code is that the scope of $name is limited to the first if
Hello and welcome to stackoverflow,
If you want to make your form in 2 steps, you need to store the "name" value in the intermediate form.
<form action="" method="POST">
<input type="text" name="name">
<input type="submit" value="Click Here!" name="submit">
<?php
if (isset($_POST['submit']))
{
$name = htmlentities($_POST['name']);
echo "<input type=\"hidden\" value=\"{$name}\" name=\"name\">";
echo "<br /><input type=\"submit\" value=\"Show it!\" name=\"show\">";
}
if (isset($_POST['show']))
{
$name = htmlentities($_POST['name']);
echo $name;
}
?>
</form>
Several things to point out :
in a field of type "hidden" you store your $name
in such a way you can recover it in the second step
you should also have a look to the htmlentities() function
Hope this helps!
Try it here
Below is the source code of a program. Can anyone help me to figure out the working of a program.
<?php
session_start();
?>
<?php
$aCaptcha = array (
array(),
array('crocodile'),
array('panda', 'panda bear', 'giant panda'),
array('pig'),
array('tiger'),
array('zebra'),
array('cow'),
array('elephant')
);
if (isset($_POST['register'])) {
$error = array();
if (!in_array(strtolower($_POST['captcha']), $aCaptcha[$_SESSION['captcha']])) {
$error['captcha'] = "<span style='color:red'>The name of the animal is not correct.</span>";
}
if (count($error) == 0) {
echo "<span style='color:red'>Thank you for completing the form.
We shall contact you soon.</span>";
die();
}
}
?>
<form action="index.php" method="post">
<?php
$_SESSION['captcha'] = rand(1, 7);
?>
<td colspan="3"><strong>Contact Form</strong></td>
<p>Full Name : <input type="text" name="Nmaes" value='' />
<p>Mobile No. : <input type="text" name="Nmaes" value='' />
<p>Email id : <input type="text" name="Nmaes" value='' />
<p>Subject : <input type="text" name="Nmaes" value='' />
<p>Message : <input type="text" name="Nmaes" value='' />
<p><img src="<?php echo $path;?>captcha/<?php echo $_SESSION['captcha'];?>.jpg" /></p>
<p>Type the name of the animal you see in the picture above. <input type="text" name="captcha" value='' />
<?php echo(isset($error['captcha']))?$error['captcha']:"";?></p>
<p><label> </label><input type='submit' name='register' value='register' /></p>
</form>
On the first page
random number between 1 and 7 is generated and stored in session
form is displayed
picture in the captcha directory is displayed based on the random number
On the second page
array with acceptable answers is generated - the keys are numbers 1 and 7 and the values are arrays of acceptable answers
the below code checks that the answer given by the user $_POST['captcha'] is one of the acceptable answers $aCaptcha[$_SESSION['captcha']]
if (!in_array(strtolower($_POST['captcha']), $aCaptcha[$_SESSION['captcha']])) {
$error['captcha'] = "<span style='color:red'>The name of the animal is not correct.</span>";
if acceptable then a message is printed out and PHP stops executing
echo "<span style='color:red'>Thank you for completing the form. We shall contact you soon.</span>";
die();