Skip to main content

Posts

Showing posts with the label form

Calculator using PHP

This Calculator model will take inputs from the Number 1 and Number 2 fields and when the user clicks on the relevant operator the result will be displayed in the Results field. For log10(), to radian, to degree, sin, cos, tan operations only require one input. Hence, the user is instructed to input the values to the 1st field only. First, before proceeding with the calculation, we need to obtain the values from the text boxes. For that we should include all the form elements inside a form. The result is directed to the same page. Therefore we will use the form action as $_SERVER['PHP_SELF'] and the method as post. Next, we can obtain the values in the text boxes.       $_POST[' form_element_name '] will give you the value of the respective element. We can write the php code as follows (in the <head>) to obtain the value from Number 1 and Number 2 fields.       <?php              $num1=...

Transferring data from a web form to a database

STEP 1: HTML FORM <body> <h2>Data Form</h2> <form action="info.php" method="post" > //Data entered is redirected to 'info.php' page Name:<input type="text" name="name" size="60" /> <br /> <br /> E-mail:<input type="text" name="email" size="60" /> <br /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> STEP 2: PHP <body> <?php  //Connecting to a database $servername="localhost"; $username="root"; $password=""; $dbname="info"; $connect=mysqli_connect("$servername","$username","$password","$dbname"); if (mysqli_connect_error()) { die("Database connection failed: ". mysqli_connect_error()); } else  { echo "Created connection successful...

Data Form using PHP

SOURCE CODE:- <!DOCTYPE html> <html> <style> .error{ color:red; } body { font-family:arial; } </style> <body> <form method="post" action="<?php echo ($_SERVER["PHP_SELF"]);?>"> <!-- ($_SERVER["PHP_SELF"]) will return the filename of the currently executing script.--> <?php $name = $email = $gender = $address =$dob = ""; $nameError=$genderError=$emailError=""; if ($_SERVER["REQUEST_METHOD"] == "POST") {      if (empty($_POST["name"])) {      $nameError = "Name is required"; //Error if name is empty     } else {      $name = ($_POST["name"]);      if (!preg_match("/^[a-zA-Z ]*$/",$name)) {        $nameError = "Only letters and white space allowed"; //Error if name contains characters other than letters and white space.     }     }         $dob = ($_POST["dob"]);  ...