How do I multiply values in PHP? - 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>

Related

PHP not working with HTML when checking user input

I am trying to check and see if the user has inputted something inside of the HTML page by using PHP and for some reason nothing is coming up on my screen when I test it.
...
<form>
<label for="age">Enter your age: </label>
<input type="text" name="age" id="age">
</br></br>
<input type="button" name="submit" id="submit" value="submit">
</form>
</body>
<?php
$theAge;
if(isset($_GET["submit"])){
$theAge = $_GET["age"];
if($theAge == "")
{
echo("<p>Please enter an age</p>");
}
?>
</html>
Your input type should be submit and not button.
<html>
<script src="https://code.jquery.com/jquery-3.2.0.min.js"></script>
<body>
<form>
<label for="age">Enter your age: </label>
<input type="text" name="age" id="age">
</br></br>
<input type="submit" name="submit" id="submit" value="submit">
</form>
</body>
</html>
First off, you're not setting a method on the form, and your check is redundant and not really working either. First off, you need to decide if you want GET or POST. Here's a version using POST:
<form method="POST" action="#">
<label for="age">Enter your age: </label>
<input type="text" name="age" id="age">
<input type="button" name="submit" id="submit" value="submit">
</form>
<?php
if(isset($_POST['submit'])){
$theAge = $_POST['age'] ?? '';
if($theAge == '') {
echo('<p>Please enter an age</p>');
} else {
// do something here
}
?>
If you rather wanna use GET, just change the POST to GET in the code.
Note that his uses PHP 7.x notation - if you're using an older version of PHP (you shouldn't), then you'll have to change this line: $theAge = $_POST['age'] ?? ''; to $theAge = isset($_POST['age']) ? $_POST['age'] : '';

How to use values in the URL in PHP

I am currently making a report error form that has 4 fields:
Job ID $jobid
Part ID part_id
Machine
Note
The user clicks on a table corresponding the their work and are brought to a new page with a url that has variable. At the moment all the fields are empty however I want the fields to be populated automatically except for notes.
Current Model
Link to report error form:
$EM_html = ''.$tick.'
Report error form:
<form action="" method="post">
Job Number: <input type="text" value="<?php print ($jobid) ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php print ($part_id) ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
Example URL
http://sra-pstest/report_error_form.php?JobID=KANBAN16-09-04-01&Machine=EM&PartID=124047
How do "extract" the information out of the url (JobID, Machine, PartID) and automatically fill out the form?
You can use $_GET
<?php
if(isset($_GET))
{
foreach($_GET as $key=>$value)
{
$$key=$value;
}
echo $JobID."<br>".$Machine."<br>".$PartID;
}
?>
Please try this
<?php
$jobid = #$_REQUEST['JobID'];
$part_id = #$_REQUEST['PartID'];
$machCode = #$_REQUEST['Machine'];
?>
<form action="" method="post">
Job Number: <input type="text" value="<?php print ($jobid) ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php print ($part_id) ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
You use $_GET Method like this code
<?php
$jobid=$part_id=$machine="";
if(isset($_GET['JobID']))
{
$jobid= $_GET['JobID'];
}
if(isset($_GET['Machine']))
{
$machine= $_GET['Machine'];
}
if(isset($_GET['PartID']))
{
$part_id= $_GET['PartID'];
}
?>
<form action="" method="post">
<?php $jobNumber = isset($_GET['JobID']) ? $_GET['JobID'] : '' ?>
Job Number: <input type="text" value="<?php echo jobNumber; ?>" name="jobNum"><br>
<input type="submit" name="submit" value="Submit">
</form>
Try using isset and post method to check if variable are declared and get the variable data on submit of form
<?php
if(isset($_POST['submit'])){
$jobid = $_POST['JobID'];
$part_id = $_POST['PartID'];
$machCode = $_POST['Machine'];
}
?>
<form action="" method="post">
Job Number: <input type="text" value="<?php echo $jobid; ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php echo $part_id; ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode" value="<?php echo $machCode; ?>"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
Hope this help

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);
}

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

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>

POST not working with no signs of error

haven't programmed PHP in a while but I
have to assemble something for a client really fast.
I've set up 2 forms with POST but when I go to the next file it's just blank space, for some reason POST isn't being registered but is set cause I'm not getting an error echo.
Hese's the forms:
<form action="Funkcije.php" method="post" name="AddFromDB">
<input type="text" placeholder="Šifra Art" name="ArtNo">
<input type="submit" value="Dodaj">
</form>
<br>
<div id="newItem">
<form action="Funkcije.php" method="post" name="AddNew">
<input type="text" placeholder="Šifra" name="Art">
<input type="text" placeholder="Ime Proizvoda" name="ImeProizvoda">
<input type="text" placeholder="Dobavljač" name="Dobava">
<input type="text" placeholder="Cijena" name="Cijena">
<input type="submit" value="Dodaj">
</form>
</div>
And here's the 2nd file:
if(isset($_POST["AddFromDB"], $_POST["ArtNo"])){
addExisting ($_POST["ArtNo"]);
}
else if(isset($_POST["AddNew"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
newItem ($_POST["Art"] && $_POST["ImeProizvoda"] && $_POST["Dobava"] && $_POST["Cijena"]);
}
else if (!isset ($_POST)){
echo "error";
}
So, by code I should be getting an error if POST is not set but I get nothing. Just a blank space.
here, you must be give a name to the submit button to check which form is POST like this...
<form method="post" name="AddFromDB">
<input type="text" placeholder="Šifra Art" name="ArtNo">
<input type="submit" value="Dodaj" name="form1">
</form>
<br>
<div id="newItem">
<form method="post" name="AddNew">
<input type="text" placeholder="Šifra" name="Art">
<input type="text" placeholder="Ime Proizvoda" name="ImeProizvoda">
<input type="text" placeholder="Dobavljač" name="Dobava">
<input type="text" placeholder="Cijena" name="Cijena">
<input type="submit" value="Dodaj" name="form2">
</form>
</div>
<?php
if(isset($_POST["form1"], $_POST["ArtNo"])){
echo "1";
}
else if(isset($_POST["form2"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
echo "2";
}
else{
echo "error";
}
?>
now, this work fine..
thank you.. enjoy coding...

Categories