How to create PHP script - php

I am trying to create simple HTML form/PHP script from were user can input info and what i want to do is just to appear on screen bellow if it was successful or when you press button reset to give you message it is erased. As i am learning about PHP i am trying to do some project but i can not figure it out.
<title>Login Page</title>
</head>
<body>
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$lastName = $_POST['lastName'];
$submit="Your name and last name is input in!";
echo $submit;
}
if(isset($_POST['reset'])){
echo "Yours data are erased!";
}
?>
<h2>Login Page</h2>
<p>Please input your info in form bellow:</p>
<form name="login" method="post" action="index.php">
<table border="0">
<tr>
<td>Name: </td>
<td><input type="text" name="name" size="20" /></td>
</tr>
<tr>
<td>Last Name: </td>
<td><input type="password" name="lastName" size="20" /></td>
</tr>
</table>
<input type="submit" name="submitButton" value="Submit" />
<input type="reset" name="resetButton" value="Reset" />
</form>
</body>
</html>

PHP Scripts must be processed by the PHP interpreter.
On windows, a common and easy solution is to use Apache through a WAMP setup.
Instructions can be found here

You need to change if(isset($_POST['submit'])){ into if(isset($_POST['submitButton'])){
and change reset in to <input type="submit" name="resetButton" value="Reset" /> and call this also by the name resetButton
If i where you i would check if(isset($_POST['submitButton'])){ for entres because pressing submitButton will echo your message eather.
Checkin for entry
if(isset($_POST['submitButton'])){
$name = $_POST['name'];
$lastName = $_POST['lastName'];
if (!empty($name) && !empty($lastName)) {
$submit="Your name and last name is input in!";
echo $submit;
} else {
echo 'Show this line that there is not entery in $name or $lastName';
}
}

Related

insert data into database using oops and mysqli

Here is my db connection file. (db_configenter code here.php)
<?php
$con = mysqli_connect("localhost","root","","oop");
if(mysqli_connect_errno())
{
echo "failed to connect to mysql:" . mysqli_connect_error();
}
?>
Here is my user.class.php code
<?php
include "db_config.php";
class User
{
public function registration($fname,$lname,$username,$email,$password)
{
$sql = $con->query("INSERT INTO users_registration(fname,lname,username,email,password)
VALUES('$fname','$lname','$username','$email','$password',)");
return $sql;
}
}
?>
And finally my registrtaion.php file is here
<?php
include "class.user.php";
$user = new User();
if (isset($_POST['submit']))
{
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$user->registration($fname,$lname,$username,$email,$password);
echo "registration success";
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Registration Form</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body>
<div class="wrapper"><!--wrapper start here-->
<form action="" method="POST">
<input type="text" name="fname" placeholder="First name"/><br>
<input type="text" name="lname" placeholder="Lastst name"/><br>
<input type="text" name="username" placeholder="User name"/><br>
<input type="text" name="email" placeholder="Email id"/><br>
<input type="text" name="password" placeholder="Password"/><br>
<input type="text" name="cpassword" placeholder="Confirm Password"/><br>
<input type="button" value="submit" name="submit" class="btn"/>
</form>
</div><!--wrapper ends here-->
</body>
</html>
i am not able to insert data into database, when i submit button nothing happend.Plz help me to solve the prob.i am new on php.
Also want to add an image in the same field.
Thanks in advance.
You have an unwanted , in your query.
$sql = $con->query("INSERT INTO users_registration(fname,lname,username,email,password)
VALUES('$fname','$lname','$username','$email','$password',)");
^
Also, to submit an HTML form, the button type should be submit. Change
<input type="button" value="submit" name="submit" class="btn"/>
to
<input type="submit" value="submit" name="submit" class="btn"/>
<?php
class dbins
{
function insert_data($name,$age)``
{
$runn = mysqli_connect("localhost","root","","oop");
$ins = "INSERT into ajax(name,age) values('$name','$age')";
$run = mysqli_qyery($runn,$ins);
return $run;
}
}
?>
<form name="insert" method="POST">
<table align="center">
<thead>
<h1 align="center">Simple Insert Use OOP</h1>
<tr>
<th>Name</th>
<td><input type="text" name="name" required="" placeholder="Enter Your Name"></td>
</tr>
<tr>
<th>Age</th>
<td><input type="text" name="age" required="" placeholder="Enter Your Age"></td>
</tr>
<tr align="center">
<td><input type="submit" name="insert" value="Insert" ></td>
</tr>
</thead>
</table>
</form>
<?php
$con = new dbins();
if(isset($_POST['insert']))
{
$name = $_POST['name'];
$age = $_POST['age'];
$con->insert_data($name,$age);
}
?>

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>

Should I include HTML within my PHP

So recently I've been using action="form.php" in my forms. But now I see that you can do without needing an action, since the PHP and the form are in the same file. Which way would be more secure. A PHP file by itself, followed by another file taking care of the form or both combined?
So would I do? Is it safer?
<?php
$name = $_POST['name'];
?>
<form method="post">
<input type="text" name="name">
<input type="submit>
</form>
user <?php echo $_SERVER['PHP_SELF'];?>
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table border="0">
<tr><td> Subject:</td><td> <input type="text" name="subject" /></td></tr>
<tr><td> Message:</td><td> <input type="text" name="message" /></td></tr>
<tr><td> <input type="submit" value="Submit" name="submit"/></td><td><input type="reset" value="Clear" /></td></tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
$email = "example#test.com";
mail($email, $subject, $message);
echo "<center>Email sent</center>";
}
?>

Wamp Server Not Processing PHP properly to Database

As requested by Shankar I am going to post the form code and the php code to this issue. Since yesterday I have moved over to my MAC and installed XAMPP to try to solve the problem but I am still having the same issue. Firstly here is the form code as set in text mate:
<!DOCTYPE HTML>
<head>
</head>
<title>Practising my PHP</title>
<body>
<h2>Subscriber Form</h2>
</body>
<form id="subscriber" action="practice.php" method="post">
<table cellpadding="0" cellspacing="25" border-width="0">
<tr>
<td><label for name="name" id="name" value="">Name:</td>
<td><input type="text" id="name" maxlength="30" maxsize="24"></td>
</tr>
<tr>
<td><label for name="email" id="email" value="">Email:</td>
<td><input type="text" id="name" maxlength="30" maxsize="24"></td>
</tr>
<tr>
<td><input type="reset" id="reset" value="reset"></td>
<td><input type="submit" id="submit" value="submit"</td>
</tr>
</table>
</form>
</html>
Next we have the processing PHP code.
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$server = mysql_connect('localhost', 'root', '') or die("The Server Cannot
Be Accessed at This Time");
$db = mysql_select_db('practising') or die("The Database Cannot Be Accessed,
Please Try Again Later");
$query = mysql_query("INSERT INTO `subscribers`(`name`,`email`)
VALUES ('$name','$email')");
if(empty($name) || empty($email)) {
echo "All Fields Must Be Filled Out";
} else {
echo "Your Information has Been Added to Our Database";
}
}
?>
I look forward to your comments and help. Oh by the way both files are saved as .php files.
Whatever errors there are in the above can somebody please advise.
What is happening at my end is this. I open the newsletter.php page which displays the form in the browser. When I do anything like fill in one field and test for if statement to notify me that all fields must be filled in nothing happens. I just get the form processing and a bland newsprocess.php page is returned in the processor. Also there is nothing being inserted into the database. I hope that this information is clear enough if you can help me it will be much appreciated. I have also changed my copy of WAMP to 4.0.4. to see if this helps. I also open the file in the urle with the following http://localhost/wamp/TrainingPHP/newsletter.php
Another strange thing is if I us a <?php echo "hello world" ?>as a test outside of this code but on the same page then the echo returns the hello world.
change your mysql_query code
$query = mysql_query($sql, "INSERT INTO subscribers(name,email)
VALUES('$name','$email')") or die(mysql_error());
to
$query = mysql_query("INSERT INTO subscribers(name,email)
VALUES('$name','$email')") or die(mysql_error());
Remove $sql inside mysql_query
Note: Try to use Mysqli or PDO as mysql_* function will be deprecated in future versions of PHP
=========== Edit ===========
change this line <td><input type="submit" id="submit" value="submit"</td>
to
<td><input type="submit" id="submit" name="submit" value="submit"></td>
you have missed ending ">" for input type = submit
give name to the submit button like name="submit" as i have given. as with out name PHP will not recognize this field
you only have given id to the email and name fields you have to give them names. PHP do not recognize ids but it know a field by name. change your both email and name html field like this.
as
<input type="text" id="name" name="name" maxlength="30" maxsize="24">
<input type="text" id="email" name="email" maxlength="30" maxsize="24">
your PHP code is ok but you have issues in your HTML code. you need to have some more practice in HTML and there were some basic mistakes.anyways im posting working code of HTML you can use this and compare it with yours and remove mistakes in you code.
<!DOCTYPE HTML>
<head>
<title>Practising my PHP</title>
</head>
<body>
<h2>Subscriber Form</h2>
<form id="subscriber" action="practice.php" method="post">
<table cellpadding="0" cellspacing="25" border-width="0">
<tr>
<td><label for name="name" id="name" value="">Name:</td>
<td><input type="text" id="name" name="name" maxlength="30" maxsize="24"></td>
</tr>
<tr>
<td><label for name="email" id="email" value="">Email:</td>
<td><input type="text" name="email" id="name" maxlength="30" maxsize="24"></td>
</tr>
<tr>
<td><input type="reset" id="reset" value="reset"></td>
<td><input type="submit" id="submit" name="submit" value="submit"></td>
</tr>
</table>
</form>
</body>
</html>

Returning PHP variables data back to an HTML Page

I am fairly new to php and have only written basic post email returns. However what im trying to achive now is to return the data held within a variable back to the webpage in a specific location on the page.
So far I have a form that collects the data Name & Surname, this is collected by php.
$firstnameField = $_POST ['name'];
$surnameField = $_POST ['surname'];
All I want to do is print the data within these variables back to the screen in a specific location on the page. I have looked around but others are talking about databases and ajax, and I havent the slightest about that stuff.
Thanks in advance.
It's pretty simple: just use echo/print inside a <?php ?> code block wherever you want that variable printed in your document:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$firstname = $_POST['name'];
}
?>
<html>
<body>
<?php if (isset($firstname)) { echo "<p>Hello, $firstname</p>"; } ?>
<form method="POST">
Enter a name: <input type="text" name="name" />
</form>
</body>
</html>
<?php
if ((isset($_POST["dataEntered"])) && ($_POST["dataEntered"] == "Yes"))
echo "Hello! " . $_POST["name"] . " " . $_POST["surname"];
else
{
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" name="form1" id="form1">
<table>
<tr>
<td>Name:</td>
<td><input name="name" type="text" id="name" value="" size="32" /></td>
</tr>
<tr>
<td>Surname:</td>
<td><input name="surname" type="text" id="surname" value="" size="32" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Send Form" /></td>
</tr>
</table>
<input type="hidden" name="dataEntered" value="Yes" />
</form>
<?php
}
?>

Categories