Just tried to make something of your code. But there are a few things you really need to take a look at:
- You are missing some closing tags in your html, for example you are missing a
</div>&</form>tag. - You can put all your php code at the top of your page. It's a best practice to seperate your logic from your view.
- Proper php opening and closing tags looks like the following :
<?php ... ?>not<= or <? A proper PDO connection looks like this
<?php try { $db = new PDO("pgsql:dbname=pdo;host=localhost", "username", "password" ); echo "PDO connection object created"; } catch(PDOException $e){ echo $e->getMessage(); } ?>If you want to concatenate a ul to output your errors you will have to do that the as follow:
<?php?>str .= '<li></li>';
str .= '</ul>';
The foundation of the errors you are getting are caused by points like the above. Goodluck and ask if you have any more questions.
Answer from Frank W. on Stack OverflowJust tried to make something of your code. But there are a few things you really need to take a look at:
- You are missing some closing tags in your html, for example you are missing a
</div>&</form>tag. - You can put all your php code at the top of your page. It's a best practice to seperate your logic from your view.
- Proper php opening and closing tags looks like the following :
<?php ... ?>not<= or <? A proper PDO connection looks like this
<?php try { $db = new PDO("pgsql:dbname=pdo;host=localhost", "username", "password" ); echo "PDO connection object created"; } catch(PDOException $e){ echo $e->getMessage(); } ?>If you want to concatenate a ul to output your errors you will have to do that the as follow:
<?php?>str .= '<li></li>';
str .= '</ul>';
The foundation of the errors you are getting are caused by points like the above. Goodluck and ask if you have any more questions.
What about closing the form tag:
<div id="slide4">
<div class="slidetitle">Contact Me</div>
<div class="content">
<form action="index.php#slide4" method="POST" id="contactform">
<input type="hidden" name="postid" value="<?=$postid?>">
<table>
<tr>
<td><b>First Name:</b>
</td>
<td>
<input type="text" name="firstName" size="45" value="<?=$firstName?>">
</td>
</tr>
<tr>
<td><b>Last Name:</b>
</td>
<td>
<input type="text" name="lastName" size="45" value="<?$lastName?>">
</td>
</tr>
<tr>
<td><b>Email:</b>
</td>
<td>
<input type="email" name="email" size="45" value="<?$email?>">
</td>
</tr>
<tr>
<td><b>Phone:</b>
</td>
<td>
<input type="number" name="phone" size="45" value="<?$phone?>">
</td>
</tr>
<tr>
<td><b>Content:</b>
</td>
<td>
<textarea name="content" form="contactform" rows="10" cols="100">
<?php echo $content; if (empty($content)){ echo "Enter text here...";} ?>
</textarea>
</td>
</tr>
<tr>
<td colspan=2 style="text-align: center">
<input type=submit name="submit1" value="Leave me a message">
</td>
</tr>
</table>
</form> <!-- MISSING !! -->
</div>
</div>
You could make two forms with 2 different actions
<form action="login.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="submit" value="Login">
</form>
<br />
<form action="register.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="submit" value="Register">
</form>
Or do this
<form action="doStuff.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="hidden" name="action" value="login">
<input type="submit" value="Login">
</form>
<br />
<form action="doStuff.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="hidden" name="action" value="register">
<input type="submit" value="Register">
</form>
Then you PHP file would work as a switch($_POST['action']) ... furthermore, they can't click on both links at the same time or make a simultaneous request, each submit is a separate request.
Your PHP would then go on with the switch logic or have different php files doing a login procedure then a registration procedure
Well you can have each form go to to a different page. (which is preferable)
Or have a different value for the a certain input and base posts on that:
switch($_POST['submit']) {
case 'login':
//...
break;
case 'register':
//...
break;
}
You can tell the form to submit to the PHP's self, then check the $_POST variables for form processing. This method is very good for error checking as you can set an error and then have the form reload with any information the user's previously submitted still in tact (i.e. they don't lose their submission).
When the "submit" button is clicked, it will POST the information to the same page, running the PHP code at the top. If an error occurs (based on your checks), the form will reload for the user with the errors displayed and any information the user supplied still in the fields. If an error doesn't occur, you will display a confirmation page instead of the form.
<?php
//Form submitted
if(isset($_POST['submit'])) {
//Error checking
if(!$_POST['yourname']) {
$error['yourname'] = "<p>Please supply your name.</p>\n";
}
if(!$_POST['address']) {
$error['address'] = "<p>Please supply your address.</p>\n";
}
//No errors, process
if(!is_array($error)) {
//Process your form
//Display confirmation page
echo "<p>Thank you for your submission.</p>\n";
//Require or include any page footer you might have
//here as well so the style of your page isn't broken.
//Then exit the script.
exit;
}
}
?>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<?=$error['yourname']?>
<p><label for="yourname">Your Name:</label><input type="text" id="yourname" name="yourname" value="<?=($_POST['yourname'] ? htmlentities($_POST['yourname']) : '')?>" /></p>
<?=$error['address']?>
<p><label for="address">Your Address:</label><input type="text" id="address" name="address" value="<?=($_POST['address'] ? htmlentities($_POST['address']) : '')?>" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
</form>
The easiest construction is to detect whether the $_POST array is not empty
if(isset($_POST['myVarInTheForm'])) {
// Process the form
}
// do the regular job
You can check in the same file if the submit button is pressed. (http://pastebin.com/8FC1fFaf)
if (isset($_POST['submit']))
{
// Process Form
}
else
{
// Show Form
}
Regarding form checking, you can save the user input, and if one of their data is unvalid you can echo the old data back and show the form again.
edit: As PeeHaa stated, you need to leave the action blank though ;)
Yes it is possible. As adviced by Pekka, it's not really suggested to do so. But here is how it can be done.
Simply using the isset method to check if the form has been posted. So say in your form you have the follwing input:
<input type="text" name="age" />
At the top of your php script you can use the following to know if the form has been submitted and thus process it.
<?php if(isset($_POST["age"])) {
//do processing
} ?>
Hope this helps ;)
This should do what you want. It loads previous posts from posts.txt, adds the current one, displays the posts and saves it. You'll need to make sure posts.txt exists and has correct permissions.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<label>Please type in a message
<input type="text" name="msg" id="msg" />
</label>
<label>and your name
<input type="text" name="name" id="name" />
</label>
<p>
<label>Submit
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</p>
</form>
<?php
$msg = $_POST["msg"];
$name = $_POST["name"];
$posts = file_get_contents("posts.txt");
$posts = "$msg - $name\n" . $posts;
file_put_contents("posts.txt", $posts);
echo $posts;
?>
</body>
</html>
That's where a database is needed. Just make a simple sql database or create a file that is appended every time a user posts his/her message. So whenever your page loads, it must first load the previous stored data and then proceed further.
In order to stay on the same page on submit you can leave action empty (action="") into the form tag, or leave it out altogether.
For the message, create a variable ($message = "Success! You entered: ".$input;") and then echo the variable at the place in the page where you want the message to appear with <?php echo $message; ?>.
Like this:
Copy<?php
$message = "";
if(isset($_POST['SubmitButton'])){ //check if form was submitted
$input = $_POST['inputText']; //get input text
$message = "Success! You entered: ".$input;
}
?>
<html>
<body>
<form action="" method="post">
<?php echo $message; ?>
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
The best way to stay on the same page is to post to the same page:
Copy<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">
This is what hidden inputs are for. Add one for your id:
<input type="hidden" name="id[]" value="<?=$rows['id']?>" />
Also, you don't need to save the count. You can count the number of items in an array using count($id).
Finally, sanitize your inputs. Never put user submitted data straight into a query. Use intval for integers and mysql_real_escape_string for strings, or use prepared statements.
Please check this out... I have added two lines in your code and ID is being fetched and you can update data accordingly. I have commented as NOTE HERE..
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="inventory"; // Database name
$tbl_name="computers"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$school=$_POST['school'];
$make=$_POST['make'];
$model=$_POST['model'];
$barcode=$_POST['barcode'];
$location=$_POST['location'];
//Note here
$id = $_POST['id'];
?>
<?php
include 'nav-bar.php';
?>
<h2 align="center">Filter Computers</h2>
<form name="form" method="post" style="margin: 0; text-align: center;">
<p><label>School Name:</label><input type="text" name="school" size="8" id="school" tabindex="1"</p>
<p><label>Make:</label><input type="text" name="make" size="25" id="make" tabindex="1"</p>
<p><label>Model:</label><input type="text" name="model" size="25" id="model" tabindex="1"</p>
<p><label>Barcode:</label><input type="text" name="barcode" size="12" id="barcode" tabindex="1"</p>
<p><label>Location:</label><input type="text" name="location" size="25" id="location" tabindex="1"</p>
<p><label>Serial:</label><input type="text" name="serial" size="25" id="location" tabindex="1"</p>
<p><label>Date Acquired yyyy-dd-mm:</label><input type="text" name="date" size="8" id="location" tabindex="1"</p>
<p><label>Processor:</label><input type="text" name="processor" size="25" id="location" tabindex="1"</p>
<p><label>RAM:</label><input type="text" name="ram" size="25" id="location" tabindex="1"</p>
<p><input align="center" type="submit" name="Filter" value="Filter">
</form>
<?php
if($_POST['Filter']){
$sql = "SELECT * FROM $tbl_name WHERE school like '%$school' AND make like '%$make' AND model like '%$model' AND location like '%$location'";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
$_SESSION['count']=$count;
?>
<strong>Update Multiple Computers</strong><br>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>School</strong></td>
<td align="center"><strong>Make</strong></td>
<td align="center"><strong>Model</strong></td>
<td align="center"><strong>Barcode</strong></td>
<td align="center"><strong>Location</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td>
<td align="center"><input name="school[]" type="text" id="school" value="<?php echo $rows['school']; ?>"></td>
<td align="center"><input name="make[]" type="text" id="make" value="<?php echo $rows['make']; ?>"></td>
<td align="center"><input name="model[]" type="text" id="model" value="<?php echo $rows['model']; ?>"></td>
<td align="center"><input name="barcode[]" type="text" id="barcode" value="<?php echo $rows['barcode']; ?>"></td>
<td align="center"><input name="location[]" type="text" id="location" value="<?php echo $rows['location']; ?>"></td>
<!-- Note here-->
<input type="hidden" name="id[]" id="id" value="<?php echo $rows['id']; ?>" />
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Update" value="Update"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
}
// Check if button name "Update" is active, do this
if(isset($_POST['Update'])){
for($i=0;$i<$_SESSION['count'];$i++){
$sql1="UPDATE $tbl_name SET school='$school[$i]', make='$make[$i]', model='$model[$i]' , barcode='$barcode[$i]' , location='$location[$i]' WHERE id='$id[$i]'";
$result1=mysql_query($sql1);
}
session_destroy();
}
if(isset($result1)){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=update_multiple.php\">";
}
?>
I hope that will help you.