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>
Related
I have complete coding in PHP Codeignitor 4 for inserting data into database, but whenever i try to access localhost its giving error.
i changed my base url too but nothing working...
i have project name ci4_crud then app and subfolders i m using different links like localhost/insert or localhost/ci4_crud/app/views/insert but nothing working ...
in App.php my base url is http://localhost/ci4_crud/
can someone help me plz
<!DOCTYPE html>
<html>
<head>
<title> Registration Form </title>
</head>
<body>
<form method="post" action="<?=base_url() ?> Crud/savedata">
<table border="1" width="600">
<tr>
<input type="text" name="first_name" placeholder="Enter First Name">
</tr>
<tr>
<input type="text" name="last_name" placeholder="Enter Last Name">
</tr>
<tr>
<input type="text" name="address" placeholder="Enter Address">
</tr>
<tr>
<input type="text" name="email" placeholder="Enter Email ID">
</tr>
<tr>
<input type="submit" name="save" value="Save Data">
</tr>
</table>
</body>
</html>
Try putting the following rule in public/.htaccess on line 17:
RewriteBase /ci4_crud
try this
action="<?php echo base_url(); ?>crud/savedata"
or this
action="<?php echo base_url(); ?>index/crud/savedata"
and also may I know what error message you get.
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';
}
}
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
}
?>
I've got a form like this:
<form name="htmlform" method="post" action="script/gen.php?postData">
<table width="450px">
</tr>
<tr>
<td valign="top">
<label for="customer">Customer:</label>
</td>
<td valign="top">
<input type="text" name="customer" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top"">
<label for="nol">Number of licences: </label>
</td>
<td valign="top">
<input type="text" name="nol" maxlength="50" size="30">
</td>
</tr>
<tr>
<td>
<form method="post" id="submit" action="script/gen.php">
<input type="button" onClick="getKey()"; value="Generate key"/>
</td>
</tr>
<div id="innhold">
<h4>Licence Key: </h>
</div>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
The codelines of interest is:
<input type="text" name="nol" maxlength="50" size="30">
and
<input type="text" name="customer" maxlength="50" size="30">
I try to write this information to a database like this:
function postData($key1) {
//just to check if the key is equal to the one thats posted to the user
//echo '<h5>From postData' . $key1 . '</h5>';
/*echo '<script type="text/javascript"> alert("The order has been submitted successfully");
location = "/Webpanel/index.html";
</script>';*/
$customerVar = $POST['customer'];
$nolVar = $POST['nol'];
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("licencedatabase");
$query_add = "INSERT INTO licence (`customer_name`,`licence_count`) VALUES ('$customerVar','$nolVar')";
$query_exec = mysql_query($query_add) or die(mysql_error());
mysql_close();
}
But I keep getting the error:
Undefined variable: POST
How can I accomplish this? Thanks in advance.
Its $_POST not $POST.
$customerVar = $_POST['customer'];
$nolVar = $_POST['nol'];
That's because it's called $_POST.
Try using $_POST instead of $POST
to access the superglobal POST use $_POST not $POST
All PHP superglobals (such as those for GET and POST) are prefixed with an underscore, so: $POST should be $_POST.
Have a look here for more information about the available superglobals in PHP:
http://php.net/manual/en/language.variables.superglobals.php
Try using $_POST instead of $POST
Check following example:
The predefined $_POST variable is used to collect values from a form sent with method="post".
Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
Example:
<form action="submitform.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
When the user clicks the "Submit" button, the URL will look like this: http://localhost/submitform.php
The "submitform.php" file can now use the $_POST variable to collect form data (the names of the form fields will automatically be the keys in the $_POST array):
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
might you will understand clearly.
I'm having a problem with one of my PHP form validation scripts.
Basically, I have a form that when a user submits information it does validation checking (via PHP) and outputs the result (Success, Error1, Error2 etc) via an echo statement
However, after a user Submits the form (the action=same page ie. it posts to itself) the validation message appears at the top of the form or next to it. I want the echo message to appear under the form.
This is what is happening now in the page:
Error: Your password is incorrect
Username:
Email:
Password:
Submit Button
This is what I want it to look like:
Username:
Email:
Password:
Submit Button
Error: Your password is incorrect
Does anyone know how to remedy this?
Here is the code:
<form id="username_check" name="username_check" method="post">
<tr>
<td><b>Username:</b></td>
<td><input name="Username" type="text" class="textfield" id="Username"
value="<?php echo($_POST['Username']); ?>"
/>
</td>
</tr>
<tr>
<td><b>Email Address:</b></td>
<td><input name="email_address" type="text" class="textfield" id="email_address"
value="<?php echo($_POST['email_address']); ?>"
/>
</td>
</tr>
<tr>
<td><b>Password:</b></td>
<td><input name="PASSWORD" type="password" class="textfield" id="PASSWORD" />
</td>
</tr>
<td><input type="submit" name="submit" value="Submit" /></td>
</form>
<?php
//If form was submitted
if (array_key_exists('submit',$_POST)){
//Do something
echo "Form validation here.....";
}
?>
This is dependent on the order of execution of the code. If you are using an IDE, set a breakpoint before the validation and step through. Otherwise, I'm pretty sure we'll need to see some of the code to get an idea of why this is happening.
Try:
<form id="username_check" name="username_check" method="post">
<table>
<tr>
<td><b>Username:</b></td>
<td><input name="Username" type="text" class="textfield" id="Username"
value="<?php echo($_POST['Username']); ?>"
/>
</td>
</tr>
<tr>
<td><b>Email Address:</b></td>
<td><input name="email_address" type="text" class="textfield" id="email_address"
value="<?php echo($_POST['email_address']); ?>"
/>
</td>
</tr>
<tr>
<td><b>Password:</b></td>
<td><input name="PASSWORD" type="password" class="textfield" id="PASSWORD" />
</td>
</tr>
<td><input type="submit" name="submit" value="Submit" /></td>
</table>
</form>
<?php
//If form was submitted
if (array_key_exists('submit',$_POST)){
//Do something
echo "Form validation here.....";
}
?>
If you are calling the
echo $error_message;
before showing the form, then the message is displayed before the form.
If you want the error message after the form just move the echo command after the form is printed out.
Sounds like you are echo'ing out your error message, then echo'ing out the form.
Do it the other way around.
Don't echo the error save it in to a variable and print it behind the form print.
That way you can also check every time if an error occur during form validation, simple check if error-variable is emtpy/false