if (isset($_POST["submit"])) - php

I am a completely new programmer and I am trying to develop a website in php. All I want to do in this part of the code is to "read" the user's inputs and save them as session variables in order to do/ calculate something else in another subpage. In order to see if everything is working fine I added the lines echo "Welcome ",$_SESSION["firstname"]; echo "ok" but it is not working. Can you help me?
Here is my code:
<!DOCTYPE html>
<html>
<body>
<form method="post" action="">
<b>First Name:</b> <input name="firstname" type="text" value=""></br></br>
<b>Last Name:</b> <input name="lastname" type="text" value=""></br></br>
<b>Age:</b> <input name="age" type="number" min="0" value=""></br></br>
<b>Number of people in household:</b> <input name="numberofpeopleinhousehold" type="number" min="1"value=""></br></br>
</br></br>
<input type="submit" name="submit" value="Submit">
</form
<?php
session_start();
if (isset($_POST["submit"])){
$_SESSION["firstname"]= $_POST["firstname"];
$_SESSION["lastname"]= $_POST["lastname"];
$_SESSION["age"]= $_POST["age"];
$_SESSION["numberofpeopleinhousehold"]= $_POST["numberofpeopleinhousehold"];
}
echo "Welcome ".$_SESSION["firstname"];//here is....
echo "ok"
?>
</body>
</html>

What session_start() does is sends a cookie in the page header when it's served to the browser. If you've already send some data to the browser, like your form, then PHP will not be able to start a session. You need to move session_atart() to the very top of your document, above the form or any echos. Also, you're missing a semi-colon, as noted by others. Also, you need to properly close your <form> tag.

That is actually working. If you look at the source code you will see the your name is echoed out.
One issue is you are missing a greater than sign at the end of your form tag.
</form
should be
</form>
Also, you need to start the session before any html. Try this...
<?php
session_start();
if (isset($_POST["submit"])){
$_SESSION["firstname"]= $_POST["firstname"];
$_SESSION["lastname"]= $_POST["lastname"];
$_SESSION["age"]= $_POST["age"];
$_SESSION["numberofpeopleinhousehold"]= $_POST["numberofpeopleinhousehold"];
}
?>
<!DOCTYPE html>
<html>
<body>
<form method="post" action="">
<b>First Name:</b> <input name="firstname" type="text" value=""></br></br>
<b>Last Name:</b> <input name="lastname" type="text" value=""></br></br>
<b>Age:</b> <input name="age" type="number" min="0" value=""></br></br>
<b>Number of people in household:</b> <input name="numberofpeopleinhousehold" type="number" min="1"value=""></br></br>
</br></br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if ($_SESSION["firstname"] != ""){
echo "Welcome ",$_SESSION["firstname"];
echo "ok";
}
?>
</body>
</html>

echo output inside if, delcare session start on top, tested and works 100%. Note- add php code on top and close form tag
<?php
session_start(); //session start must be first line
if (isset($_POST["submit"])){
$_SESSION["firstname"]= $_POST["firstname"];
$_SESSION["lastname"]= $_POST["lastname"];
$_SESSION["age"]= $_POST["age"];
$_SESSION["numberofpeopleinhousehold"]= $_POST["numberofpeopleinhousehold"];
echo "Welcome ".$_SESSION["firstname"]; //inside if condition
echo "ok";
}
?>
<!DOCTYPE html>
<html>
<body>
<form method="post" action="#">
<b>First Name:</b> <input name="firstname" type="text" value=""/></br></br>
<b>Last Name:</b> <input name="lastname" type="text" value=""/></br></br>
<b>Age:</b> <input name="age" type="number" min="0" value=""/></br></br>
<b>Number of people in household:</b> <input name="numberofpeopleinhousehold" type="number" min="1"value=""/></br></br>
</br></br>
<input type="submit" name="submit" value="Submit"/>
</form> <!-- close form tag -->
</body>
</html>

Related

Passing hidden value to a php variable in input tag

I am new to coding and pardon me if it a silly thing I am missing. I have searched through the forum & did not find an answer that suits my need. I have 2 files: jobs.php & jobprocess.php
Jobs.php goes as
<?php session_start();
include('dbConnect.php');
$q1="abc";
$q2="pqr";
$q3="xyz";
$opportunity=29;
echo "Opportunity is". $opportunity;
?>
<html>
<head>
<div align="center">
<form method="post" method="post" action="jobprocess.php">
<input type="text" name="q1" placeholder="<?php echo $q1;?>"><br>
<input type="text" name="q2" placeholder="<?php echo $q2;?>"><br>
<input type="text" name="q3" placeholder="<?php echo $q3;?>"><br>
<input type="hidden" name="opportunity" value="<?php echo $opportunity;?>">
<ul class="actions">
<li><input type="submit" name="submit" value="I would like to join!! "></li>
</ul>
</form>
</div>
</head>
<body>
</body>
</html>
jobprocess.php goes with the code
<?php session_start();
include('dbConnect.php');
$opportunity = $_GET['opportunity'];
echo "opportunity is " . $opportunity;
?>
Unfortunately, the above code is not defining value="29" for opportunity on 2nd page. Thanks in advance
If you echo anything before the html tag it would effectively make the html invalid. Also, the head of the document MUST not have presentational html elements such as forms, divs etc
<?php
session_start();
include('dbConnect.php');
$q1="abc";
$q2="pqr";
$q3="xyz";
$opportunity=29;
?>
<html>
<head>
<title>must have a title</title>
</head>
<body>
<?php
echo "Opportunity is". $opportunity;
?>
<div align="center">
<form method="post" method="post" action="jobprocess.php">
<input type="text" name="q1" placeholder="<?php echo $q1;?>"><br>
<input type="text" name="q2" placeholder="<?php echo $q2;?>"><br>
<input type="text" name="q3" placeholder="<?php echo $q3;?>"><br>
<input type="hidden" name="opportunity" value="<?php echo $opportunity;?>">
<ul class="actions">
<li><input type="submit" name="submit" value="I would like to join!! "></li>
</ul>
</form>
</div>
</body>
</html>
And because the form is set to POST you should probably check and use the POSTed variable rather than a GET variable
<?php
session_start();
include('dbConnect.php');
$opportunity = $_POST['opportunity'];
echo "opportunity is " . $opportunity;
?>
Surprisingly my answer that suggested using session variable instead of hidden form field was deleted?! I guess session variables are illegal now?
The answer was chosen for the best answer even.

showing undefined index while passing variable through url in php

//passing value through url
while($rowcontent=mysqli_fetch_array($details))
{
echo "<tr><td><a href=http://localhost/study/study2/edit.php?toedit=$rowcontent[rollnumber]>edit</a></td><tr>";
}
//receiving value from url
<html>
<form method="GET" action="edit.php">
<input type="text" name="name">Enter Name <br>
<input type="text" name="rollnumber" required>Enter Rollnumber <br>
<input type="text" name="mark">Enter Mark <br>
<input type="text" name="dept">Enter Department <br>
<input type="submit" name="submit" value="submit"> <br>
</form>
<?php
$rollnumber=$_GET["toedit"];
echo $rollnumber;
if(isset($_GET["submit"]))
{
$name=$_GET["name"];
$nrollnumber=$_GET["rollnumber"];
$mark=$_GET["mark"];
$department=$_GET["dept"];
$connect=mysqli_connect("","root","","details");
mysqli_query($connect,"UPDATE student SET name='$name' rollnumber='$nrollnumber' mark='$mark' department='$department' WHERE rollnumber='$rollnumber'");
mysqli_close($connect);
}
?>
above are two parts of code, where im trying to edit values in a DB by passing value (a roll number) through url but in editing code the value is not being received correctly or some other problem i cant figure out. i did the same for deleting a value from url but it seems to work.
You can use like below:
//receiving value from url
<html>
<form method="GET" action="edit.php">
<?php
while($rowcontent=mysqli_fetch_array($details))
{
?>
<input type="hidden" name="toedit" value="<?php echo $rowcontent[rollnumber]; ?>" />
<?php
}
?>
<input type="text" name="name">Enter Name <br>
<input type="text" name="rollnumber" required>Enter Rollnumber <br>
<input type="text" name="mark">Enter Mark <br>
<input type="text" name="dept">Enter Department <br>
<input type="submit" name="submit" value="submit"> <br>
</form>
inside edit.php
<?php
if(isset($_GET["submit"]))
{
$name=$_GET["name"];
$nrollnumber=$_GET["rollnumber"];
$mark=$_GET["mark"];
$department=$_GET["dept"];
$connect=mysqli_connect("","root","","details");
mysqli_query($connect,"UPDATE student SET name='$name' rollnumber='$nrollnumber' mark='$mark' department='$department' WHERE rollnumber='$rollnumber'");
mysqli_close($connect);
}

PHP,HTML Need to direct from one form to another form

I have written a code to get First name,last name and NIC no. If First name is missing I will show a error call "missing" infront of the textbox. All this thing is working and it will send the information in to the same page.
If first name is missing show the error in the same form which will allow user to refill it. If the user entered information correctly, I need to redirect user to another form. How can I do it?
<?php
$fnameerr="";
$name="";
if($_SERVER['REQUEST_METHOD']=="POST")
{
if(empty($_POST["fname"]))
{
$fnameerr="Missing";
}
}
?>
<head>
<title>Untitled Document</title>
</head>
<body>
<form name="form1" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
First Name:<input type="text" name="fname" value="<?php echo htmlspecialchars($name);?>">
<span class="error" style="color:#FF0000">*<?php echo $fnameerr;?></span>
<br/><br/>
Last Name:<input type="text" name="lname" /><br/><br/>
NIC:<input type="text" name="nic" /><br/><br/>
<input type="submit" name="sub" value="Register"/>
<input type="reset" name="re" value="Reset"/>
</form>
</body>
</html>
Use header() to redirect
if(empty($_POST["fname"])) {
$fnameerr="Missing";
}
else {
header("Location: anotherForm.php");
}

How can i pass values from two html pages to php?

I have 2 html pages :
page1.html
<html>
<body>
<form action="page2.html" method="post">
Enter First name: <input type="text" id="text1">
<input type="submit" value="Next">
</form>
</body>
</html>
page2.html
<html>
<body>
<form action="test.php" method="post">
Enter Last name: <input type="text" id="text2">
<input type="submit" value="Submit">
</form>
</body>
</html>
Now, i would like to retrieve the value of text1 from page1.html and text2 from page2.html. How can i go about it?
Looks like you are looking for forms. See this tutorial http://www.w3schools.com/php/php_forms.asp. Post your form data from page1.php to page2.php. On page2.html you can access them via $_POST.
Please be aware: This is not a secure example, just for showing purposes! When you want to show user generated data in your frontend, please use sanitizing and validation http://www.php.net/manual/en/filter.examples.sanitization.php.
page1.php:
<form action="page2.php" method="post">
Enter first name: <input type="text" name="firstName"><br>
<input type="submit">
</form>
page2.php
<h1>Hey <?php echo $_POST['firstName']; ?></h1>
<form action="lastpage.php" method="post">
Enter last name: <input type="text" name="lastName"><br>
<input type="hidden" name="firstName" value="<?php echo $_POST['firstName']; ?>">
<input type="submit">
</form>
lastpage.php
<h1>Yo, my mate <?php echo $_POST['firstName']; ?> <?php echo $_POST['lastName']; ?>!</h1>

How do I multiply values in PHP?

I am trying to find a basic input where user enters one number and the second number and then multiplies it.
I got it to work without the isset function, but now I am trying to echo out the error line when the page first starts up. If you see the input it is named, name and name2 so I call them in PHP.
My original code did not use isset and it worked but I got error before any input. This is my PHP code:
<html>
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
if (isset($_POST['name'])) && (isset($_POST['name2'])){
$num=$_POST['name'];
$num2=$_POST['name2'];
echo $num*$num2;
}
else{
echo '';
}
?>
</body>
</html>
You have closed your IF parentheses too soon. The line should be like this:
if (isset($_POST['name']) && isset($_POST['name2'])) {
This is working code you have some extra parenthesis. If you are multiplying integer values from user always use intval function so that you always have integer value. If user enters string or characters it intval will change to zero
<html>
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
if (isset($_POST['name']) && isset($_POST['name2'])){
$num = intval($_POST['name']);
$num2 = intval($_POST['name2']);
echo $num*$num2;
}
else{
echo '';
}
?>
Try this I think it is helpful to you:
<form method="POST">
<input type="text" name="value1" placeholder="Enter 1st Value" required>
<input type="text" name="multiply" value="*" readonly>
<input type="text" name="value2" placeholder="Enter 2nd Value" required>
<input type="submit" name="submit" value="Calculate">
</form>
<?php
if(isset($_POST['submit'])){
$value1 = $_POST['value1'];
$multiply = $_POST['multiply'];
$value2 = $_POST['value2'];
if($multiply == "*"){
echo $value1*$value2;
}
}
?>
The main problem is paranthesis are not closed properly it is
if(condition1)&& (condition2){
}
it should be
if((condition1)&&(condition2)){
}
you can use single condition for this also as shown in below code
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send" name="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
//if (isset($_POST['name'])) && (isset($_POST['name2'])){ problem is here your paranthesis are not closed properly
if (isset($_POST['send'])){ //use this as this will ensure that your send button is clicked for submitting form
$num=$_POST['name'];
$num2=$_POST['name2'];
echo $num*$num2;
}
else{
echo '';
}
?>
</body>
</html>

Categories