How to submit error free form - php

Being a beginner, I am at a cross road on how to submit an error free form. Reference to the php file below, I have used 3 action settings with the following consequence:
Method 1
action = ""
This indicates the errors on same page whether there is an error or not.
Method 2:
action=""
This indicates the errors on same page whether there is an error or not.
Method 3:
action="User sch contact.php"
This opens a new page (User sch contact) with all the user input whether there is error or not.
Now I do not want the form submitted until every error is cleared by the user. Please how can I achieve this? Is there anything I am missing in php, sql, Mysql that will help me achieve this?
<!DOCTYPE html>
<html>
<head>
<title>PHP Exercise</title>
<style type="text/css">
body {font-family: Arial, Verdana, Helvetica,Sans-serif; padding: 0px;
margin-left: 50px;}
input:hover {background-color: rgb(250,250,150);}
.btn {padding: 5px 5px 5px 5px; background-color: rgb(240,240,240); font-
size: 12px; font-weight: bold; font-style: italic; border: 1px; }
.btn:hover {background-color: rgb(250,250,150);}
th {font-weight: bold; font-size: 14px; border: 1px; border-style: solid;
margin: 0px; border-spacing: 0px;}
.cdtl {text-align: left;}
td {font-weight: Normal; font-size: 14px; border: 1px; border-style: solid;
margin: 0px; border-spacing: 0px;}
.err {color: rgb(250,50,10); width: 200px; font-style: italic; border: 0px;
font-size: 14px;}
</style>
<script type="text/javascript">
var debugScript = true;
</script>
</head>
<?php
$username = $useraddy1 = $useraddy2 = $userfone = $useremail = $userurl
= "";
$firstgrade = $secondgrade = $thirdgrade = $fourthgrade = "";
$usernameerr = $useraddy1err = $useraddy2err = $userfoneerr =
$useremailerr = $userurlerr = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["username"])) {
$usernameerr = "Name is required";
} else {
$username = test_input($_POST["username"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z0-9 ]*$/",$username)) {
$usernameerr = "Only alphanumerics and white space allowed";
} else {$usernameerr = "";}
}
if (empty($_POST["useraddy1"])) {
$useraddy1err = "Address is required";
} else {
$useraddy1 = test_input($_POST["useraddy1"]);
if (!preg_match("/^[a-zA-Z0-9 ]*$/",$useraddy1)) {
$useraddy1err = "Only alphanumerics and white space allowed";
} else {$useraddy1err = "";}
}
if (empty($_POST["useraddy2"])) {
$useraddy2err = "Address is required";
} else {
$useraddy2 = test_input($_POST["useraddy2"]);
if (!preg_match("/^[a-zA-Z ]*$/",$useraddy2)) {
$useraddy2err = "Only letters and white space allowed";
} else {$useraddy2err = "";}
}
if (empty($_POST["userfone"])) {
$userfoneerr = "Address is required";
} else {
$userfone = test_input($_POST["userfone"]);
if (!preg_match("/^[+0-9]*$/",$userfone)) {
$userfoneerr = "Only + numbers and white space allowed";
} else {$userfoneerr = "";}
}
if (empty($_POST["useremail"])) {
$useremailerr = "Email is required";
} else {
$useremail = test_input($_POST["useremail"]);
// check if e-mail address is well-formed
if (!filter_var($useremail, FILTER_VALIDATE_EMAIL)) {
$useremailerr = "Invalid email format";
}
}
if (empty($_POST["userurl"])) {
$userurl = "";
} else {
$userurl = test_input($_POST["userurl"]);
// check if URL address syntax is valid (this regular expression also
allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&##\/%?
=~_|!:,.;]* [-a-z0-9+&##\/%=~_|]/i",$userurl)) {
$userurlerr = "Invalid URL";
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h4>User Input</h4>
<form id="setUp" method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table id="contact" class="userInfo">
<tbody>
<tr>
<th class="cdtl">User Name:</td>
<td><input type="text" name='username' value="<?php echo $username;?>" />
</td>
<td class='err'><?php echo $usernameerr;?></td>
</tr>
<tr>
<th class="cdtl">Address1:Number, Street, City</td>
<td><input type="text" name='useraddy1' value="<?php echo $useraddy1;?>" />
</td>
<td class='err'><?php echo $useraddy1err;?></td>
</tr>
<tr>
<th class="cdtl">Address2:State, Country</td>
<td><input type="text" name='useraddy2' value="<?php echo $useraddy2;?>" />
</td>
<td class='err'><?php echo $useraddy2err;?></td>
</tr>
<tr>
<th class="cdtl">Telephone (International format)</td>
<td><input type="text" name='userfone' value="<?php echo $userfone;?>" />
</td>
<td class='err'><?php echo $userfoneerr;?></td>
</tr>
<tr>
<th class="cdtl">e-mail Address</td>
<td><input type="email" name='useremail' value="<?php echo $useremail;?>" />
</td>
<td class='err'><?php echo $useremailerr;?></td>
</tr>
<tr>
<th class="cdtl">Website (Optional)</td>
<td><input type="url" name='userurl' value="<?php echo $userurl;?>" /></td>
<td class='err'><?php echo $userurlerr;?></td>
</tr>
</tbody>
</table>
<br>
<input class="btn" type="submit" value="Submit" />
</form>
</body>
</html>

I have resubmitted this question in a different way. Basically, I was trying to validate a form on the client side. The best approach is to use both required and pattern in the input. With these 2, the form will not be submitted with a blank field & if user input does not follow the pattern. Further validation will of course be done on the server side for double assurance.
<form>
<b>User Id:<b><br>
<input id="userid" name="userid" type="text" required pattern="[A-Za-
z0-9.-_]{5,10}"><br>
<b>Password:<b><br>
<input id="userpswd" name="userpswd" type="password" required
pattern="[A-Za-z0-9&*]{8,20}"><br><br>
<b>SS Level:<b><br>
<input id="sslevel" name="sslevel" type="text" required pattern="[A-Za-z0-9]
{3,8}">
<input id='submit' type="submit" value="Submit form">
</form>

Related

Form disappears after pressing SUBMIT button

I'm pretty new to html, php, mysql and i have to like learn the basics # my new workplace.
I'm having an annoying problem with my Form Validation. I'm using ubuntu server in combination with PuTTY
My problem is: that my 'Validation' and 'empty Field' check is not working propperly.
So when i go into my browser, my Form (Table) shows up as it should. When I hit the Submit button WITHOUT writing any stuff into the fields, the Form stays on the page and my Errors appear: ("Name is required, email, Nachname") That's right so far.
But when i fill in anything into the field(s), and then hit the Submit button, the form just disappears and i get like a blank page (but still having my CSS background n stuff).
No matter if comes up to the requirements, or not.
I'm trying to find out whats wrong since 3 whole days 9hrs/day # my workplace.
So hopefully anyone of you can help me finally get this thing work.
everything i post now is in the same order as i have it in my PuTTy
(nano)
My script starts like this:
CSS:
<html>
<head>
<title> Formular FINAL </title>
<style>
body {
background-image: url("http://fewpict.com/images/background-pictures/background-pictures-01.jpg");
}
.db_table {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
overflow: hidden;
overflow-y: auto;
position: fixed;
top: 80%;
left: 50%;
transform: translate(-50%, -50%);
width: 50%;
height: 100px;
}
.db_table td, tr {
color: white;
text-align: center;
}
.center_div {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.center_div td {
font-family: "Comic Sans", Comic Sans MS, cursive;
color: white;
text-align: left;
}
.error {color: #FF0000;}
</style>
</head>
<body>
PHP-Form Validation:
<?php
$VornameErr = "";
$emailErr = "";
$NachnameErr = "";
$Vorname = $_POST['Vorname'];
$email = $_POST['email'];
$Nachname = $_POST['Nachname'];
$allesok = "";
//input type hidden
if(isset($_POST['action'])){
//ÜBERPRÜFUNGSVARIABLE
$allesok = 1;
$errors = array();
if (empty($_POST) === false) {
$required_fields = array('Vorname', 'Nachname', 'email');
foreach($_POST as $key=>$value) {
if (empty($value) && in_array($key, $required_fields) === true ){
$errors[] = 'Fields marked with an asterisk are required';
break 1;
}
}
}
//Vorname Überprüfen
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["Vorname"])) {
$allesok = 0;$VornameErr = "Name is required";
} else {
$Vorname = test_input($_POST["Vorname"]);
if (!preg_match("/[a-zA-Z]{3,}/",$Vorname)) {
$allesok = 0;$VornameErr = "Only letters and atleast 3 alpha characters Allowed";
}
}
}
if (empty($_POST["email"])) {
$allesok = 0;$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$allesok = 0;$emailErr = "Invalid email format";
}
}
//Nachname Überprüfen
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["Nachname"])) {
$allesok = 0; $NachnameErr = "Nachname is required";
} else {
$Nachname = test_input($_POST["Nachname"]);
if (!preg_match("/[a-zA-Z]{3,}/",$Nachname)) {
$allesok = 0;$NachameErr = "Only letters and atleast 3 alpha characters Allowed";
}
}
}
function check_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
}
MySQL:
if ($allesok) {
define('DB_NAME', 'formular');
define('DB_USER', 'David');
define('DB_PASSWORD', '****');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
if(isset($_POST['sent'])) {
$value1 = $_POST['Vorname'];
$value2 = $_POST['Nachname'];
$value3 = $_POST['email'];
$sql = "INSERT INTO formular (Vorname, Nachname, email) VALUES ('$value1', '$value2', '$value3')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
} else {
$msg1='<p> Your information was submitted successfully.</p>';
}
}
Echo Form:
if(isset($_POST['sent'])) {
?>
<div class="center_div">
<table>
<tr>
<td style="width: 200px;">Vorname: </td>
<td style="border-bottom: 1px solid black;"><?php echo $_POST['Vorname']; ?> </br></td>
</tr>
<tr>
<td style="width: 200px;">Nachname: </td>
<td style="border-bottom: 1px solid black;"><?php echo $_POST['Nachname']; ?> </br></td>
</tr>
<tr>
<td style="width: 200px;">E-Mail: </td>
<td style="border-bottom: 1px solid black;"><?php echo $_POST['email']; ?> </br> </td>
</tr>
</table>
<input type="button" value="Zurück" onClick="history.back();">
</div>
<?php
echo $msg1."<br /><br /><br />";
//Liste anzeigen
} elseif(isset($_POST['show_table'])) {
//fake formular <-----was made for still having the possibility to fill out stuff when i view the LIST
echo "<div class='center_div'>";
echo "<form action='toto2.php' method='POST'/>";
echo"<table>";
echo "<tr>";
echo "<th></th>";
echo "<th></th>";
echo "<th>span class='error'>* required field.</span></th>";
echo "<tr>";
echo "<td style= 'width: 200px;' > Vorname:* </td>";
echo "<td> <input type='text' name='Vorname' placeholder='Your Vorname...' /></td>";
echo "<td><span class='error'>*$VornameErr </span></td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width: 200px;'> Nachname:* </td>";
echo "<td> <input type='text' name='Nachname' placeholder='Your Nachname...' /></td>";
echo "<td><span class='error'>*$NachnameErr</span></td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width: 200px;'> E-Mail:* </td>";
echo "<td><input type='email' name='email' placeholder='Your E-Mail address...' /></td>";
echo "<td><span class='error'>*$emailErr</span></td>";
echo "</tr>";
echo "</table>";
echo "<input type='submit' value='SEND' name='sent' />";
echo "<input type='submit' value='Einträge anzeigen' name='show_table' />";
echo "<input type='button' value='Einträge ausblenden' onClick='history.back();'>";
echo "</div>";
echo "</form>";
//DB Tabelle
$query = "SELECT * FROM formular;";
$result = mysql_query($query);
echo '<div class="db_table">';
echo '<table>';
echo '<tr>';
echo '<th>ID</th>';
echo '<th>Vorname</th>';
echo '<th>Nachname</th>';
echo '<th>email</th>';
echo '</tr>';
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "</tr>";
}
echo '<tr>';
echo '<td>';
echo '<input type="button" value="Zurück" onClick="history.back();">';
echo '</td>';
echo '</tr>';
echo '</table>';
echo '</div>';
}
} else {
?>
HTML Form:
<div class="center_div">
<span class="error"></span>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table>
<tr>
<th></th>
<th></th>
<th><span class="error">* required field.</span></th>
<tr>
<td style= "width: 200px;" > Vorname:* </td>
<td> <input type="text" name="Vorname" placeholder="Your Vorname..." /></td>
<td><span class="error">* <?php echo $VornameErr;?></span></td>
</tr>
<tr>
<td style="width: 200px;"> Nachname:* </td>
<td> <input type="text" name="Nachname" placeholder="Your Nachname..." /></td>
<td><span class="error">* <?php echo $NachnameErr;?></span></td>
</tr>
<tr>
<td style="width: 200px;"> E-Mail:* </td>
<td><input type="text" name="email" placeholder="Your E-Mail address..." /></td>
<td><span class="error">* <?php echo $emailErr;?></span></td>
</tr>
</table>
<input type="hidden" name="action" value="1">
<input type="submit" value="SEND" name="sent" />
<input type="submit" value="Einträge anzeigen" name="show_table" />
<input type="button" value="Einträge ausblenden" onClick="history.back();">
</form>
</div>
<?php
}
mysql_close();
?>
</body>
</html>

Admin / Member login PHP not redirecting

I am building a login system where there is a member and a user. I have a table that has a column of name, username, password, email and type. Type designates the type of user. I have created already a working log-in form. My problem is every time I try to log in it does not redirect to the designated page. It stays on the same page but gives me a blank page. I've been figuring out what is wrong with the code. Can someone help me ?
Thank you.
PHP Login code:
<?php
include 'try_connect.php';
if (isset($_POST['login'])) {
$user = $_POST['username'];
$pass = $_POST['password'];
$hsl = mysql_query("SELECT name, username, password, type FROM users WHERE username='$user' and password='$pass'");
$data = mysql_fetch_array($hsl);
$username = $data['username'];
$password = $data['password'];
$type = $data['type'];
$name = $data['name'];
if ($user==$username && $pass==$password) {
session_start();
$_SESSION['name']=$name;
if ($type =='admin') {
header('Location: try_admin.php');
}
elseif ($type =='user') {
header('Location:try_user.php');
}
}
}
?>
LOG IN FORM
<form action="try_login.php" method="POST">
<table style="margin-left: 30%; margin-bottom: 1%;">
<tr>
<th style="font-family: Arial; line-height: 5px;">Username:</th>
<td><input type="text" name="username" style="margin-left: 10%; width: 120%; margin-bottom: 5%;"></td>
</tr>
<tr>
<th style="font-family: Arial;">Password:</th>
<td><input type="password" name="password" style="margin-left: 10%; width: 120%; margin-bottom: 0%; margin-top: 10%;"></td>
</tr>
</table>
<input type="submit" value="login" name="login" style="background-color: black; color: white; margin-bottom: 5%; margin-top: 0%; margin-left: 59%; border-color: #89cff0; border-style: double solid;">
Try this:
<?php
include 'try_connect.php';
if (isset($_POST['login'])) {
$user = $_POST['username'];
$pass = $_POST['password'];
$hsl = mysql_query("SELECT name, username, password, type FROM users WHERE username='$user' and password='$pass'");
$data = mysql_fetch_array($hsl);
$username = $data['username'];
$password = $data['password'];
$type = $data['type'];
$name = $data['name'];
if ($user==$username && $pass==$password) {
session_start();
$_SESSION['name']=$name;
if ($type =='admin') {
header("Location: try_admin.php");
}
elseif ($type =='user') {
header("Location:try_user.php");
}
}
}
?>
Use exit() after header(). exit() will terminate your script immediately.
if ($type =='admin') {
header('Location: try_admin.php');
exit();
}
elseif ($type =='user') {
header('Location:try_user.php');
exit();
}
Also make sure your header() function is called before any html output even before <html> tag

Page become white

Hello I have a error on my page.. When i go to www.mysite.com/request.php the page is just white..
<?php
include("../includes/config.php");
include("../includes/functions.php");
######
include('../config_radio.php'); //you may edit this path to fit your server environment otherwise leave it alone
$scfp = fsockopen("$scip", $scport, &$errno, &$errstr, 30);
if(!$scfp) {
$scsuccs=1;
echo''.$scdef.' is Offline';
}
if($scsuccs!=1){
fputs($scfp,"GET /admin.cgi?pass=$scpass&mode=viewxml HTTP/1.0\r\nUser-Agent: SHOUTcast Song Status (Mozilla Compatible)\r\n\r\n");
while(!feof($scfp)) {
$page .= fgets($scfp, 1000);
}
###########################################################
///////////////////////// Part 1 \\\\\\\\\\\\\\\\\\\\\\\\\
###########################################################
//define xml elements
$loop = array("STREAMSTATUS", "BITRATE", "SERVERTITLE", "CURRENTLISTENERS");
$y=0;
while($loop[$y]!=''){
$pageed = ereg_replace(".*<$loop[$y]>", "", $page);
$scphp = strtolower($loop[$y]);
$$scphp = ereg_replace("</$loop[$y]>.*", "", $pageed);
if($loop[$y]==SERVERGENRE || $loop[$y]==SERVERTITLE || $loop[$y]==SONGTITLE || $loop[$y]==SERVERTITLE)
$$scphp = urldecode($$scphp);
// uncomment the next line to see all variables
//echo'$'.$scphp.' = '.$$scphp.'<br>';
$y++;
}
//end intro xml elements
###########################################################
///////////////////////// Part 2 \\\\\\\\\\\\\\\\\\\\\\\\\
###########################################################
//get song info and history
$pageed = ereg_replace(".*<SONGHISTORY>", "", $page);
$pageed = ereg_replace("<SONGHISTORY>.*", "", $pageed);
$songatime = explode("<SONG>", $pageed);
$r=1;
while($songatime[$r]!=""){
$t=$r-1;
$playedat[$t] = ereg_replace(".*<PLAYEDAT>", "", $songatime[$r]);
$playedat[$t] = ereg_replace("</PLAYEDAT>.*", "", $playedat[$t]);
$song[$t] = ereg_replace(".*<TITLE>", "", $songatime[$r]);
$song[$t] = ereg_replace("</TITLE>.*", "", $song[$t]);
$song[$t] = urldecode($song[$t]);
$dj[$t] = ereg_replace(".*<SERVERTITLE>", "", $page);
$dj[$t] = ereg_replace("</SERVERTITLE>.*", "", $pageed);
$r++;
}
//end song info
fclose($scfp);
}
########
if($_GET['sendreq'] == "true" && $_GET['dj'] && $_GET['name'] && $_GET['type'] && $_GET['message']) {
$name = $_GET['name'];
$dj = $_GET['dj'];
$type = $_GET['type'];
$message = $_GET['message'];
$name = clean($name);
$dj = clean($dj);
$type = clean($type);
$message = clean($message);
$name = censor($name);
$dj = censor($dj);
$type = censor($type);
$message = censor($message);
$ip = $_SERVER['REMOTE_ADDR'];
$a1 = date("r");
$a2 = explode(" ", $a1);
$insert = mysql_query("INSERT INTO requests (`name`, `dj`, `type`, `msg`, `time`, `ip`) VALUES ('$name', '$dj', '$type', '$message', '$a2[4]', '$ip')") or die('Could not send request! Error: '.mysql_error());
echo('<strong>Thanks!</strong> Your message has been sent to the DJ specified!');
die();
}
?>
<!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=iso-8859-1" />
<title>Request Line</title>
<style type="text/css">
<!--
#title { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-weight: bold; }
#sub_title { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-weight: normal; font-style: italic; }
#text { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-weight: normal; font-style: none; }
#results { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-weight: normal; font-style: none; }
input { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-weight: normal; font-style: none; }
select { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-weight: normal; font-style: none; }
textarea { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-weight: normal; font-style: none; }
-->
</style>
<script>
var xmlHttp
function GetXmlHttpObject()
{
var objXMLHttp = null
if (window.XMLHttpRequest)
{
objXMLHttp = new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}
function sendreq() {
// Open function
// Set object
xmlHttp = GetXmlHttpObject()
if (xmlHttp == null)
{
alert ("Browser does not support HTTP Request")
return
}
// We have set teh object
// Check for empty strings
if(document.request.type.value == "") {
alert("You must select a type of request!")
return
}
if(document.request.name.value == "") {
alert("You must enter a name!")
return
}
if(document.request.message.value == "") {
alert("You must enter a message!")
return
}
// We have alerted if any strings are emptyyy
var url = "request.php?sendreq=true&dj="+document.request.dj.value+"&name="+document.request.name.value+"&type="+document.request.type.value+"&message="+document.request.message.value
xmlHttp.open("GET",url,true)
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) {
document.getElementById("results").innerHTML = xmlHttp.responseText;
document.getElementById("form").innerHTML = "" // This removes the form, stopping them from sending the same request over and over again - SMART, EH?
}
};
xmlHttp.send(null);
// Now we clear out the forms and hide the form to TRY to prevent spamz!
// End function
}
</script>
</head>
<body>
<span id="title">Kontakta studion</span><br />
<span id="sub_title">Edit the css for this to fit YOUR site!</span>
<br />
<br />
<div id="form" name="form" style="display: block">
<form method="post" name="request" onsubmit="sendreq();return false;">
<span id="text"><strong>
Radio DJ:<br />
<label>
<select name="dj" id="dj">
<?php
$query = mysql_query("SELECT * FROM users WHERE level != 'banned'") or die('ERROR '.mysql_error());
while($rows = mysql_fetch_array($query)) {
if(preg_match("/". $rows[username] ."/i", $servertitle)) {
echo("<option value='". $rows[username] ."' SELECTED>DJ ". $rows[username] ." lol</option>");
} else {
echo("<option value='". $rows[username] ."'>DJ ". $rows[username] ."</option>");
}
}
?>
</select></label>
</strong><br />
<br />
<strong>Ditt Namn:</strong><br />
<label>
<input name="name" type="text" id="name" />
</label>
<br />
<br />
<strong>Typ:</strong><br />
<label>
<select name="type" id="type">
<option value="request">Låtönskning</option>
<option value="shoutout">Hälsning</option>
<option value="competition">Tävlingssvar</option>
<option value="joke">Skämt</option>
</select>
</label>
<br />
<br />
<strong>Meddelande:</strong></span>
<br />
<label>
<textarea name="message" cols="30" rows="4" id="message"></textarea>
</label>
<br />
<br />
<label>
<input type="submit" name="Submit" value="Skicka!" />
</label>
</form>
<br /><br />
</div>
<div id="results"></div>
</body>
</html>
Thats the code... I got 500 Internal Server Error
Any help on whats wrong with this?
I have search on google but no answer there... Maybe I search wrong but anyway. I hope to get some help here instead :).
Turn on error reporting by editing your php.ini file.
Inside your php.ini file, you will find two settings - error_reporting and display_errors. You should set error_reporting to E_ALL, and display_errors to 1. Restart your webserver, and reload your page. You should now see a more descriptive error message.
Note that other answers ask you to enable error reporting at runtime, but not all errors are triggered at runtime. Parse errors (eg bad syntax) will be triggered before your code is even run, and so the error config will still be using the values defined in the php.ini file, still leaving you with a blank page.
As a side note, PHP errors may also be reported to the web server's event log (the location of which will vary on web server and operating system), even if error_reporting is disabled.
Before while loop on line 13 you need to set $page variable, so:
$page = "";
while(!feof($scfp)) {
$page .= fgets($scfp, 1000);
}
In while loop you add st. to undefined $page, so you need to define that first.
Turn on error reporting.
error_reporting(E_ALL);
ini_set('display_errors', 'on');
You will get the error by this.

login page wont log in

I am getting a problem where my login details wont let me log into my page :(
so my set up page is
<?php
$db = mysql_connect("$host", "$dblogin", "$dbpassword");
mysql_select_db("$dbname");
$res = mysql_query("SELECT * FROM userdb WHERE email='$email'");
$playerinfo = mysql_fetch_array($res);
$date2 = date("H:i");
function error($type)
{
if($type == "field")
{
echo '<body link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF" bgcolor="#000000" text="#FFFFFF">';
echo '<p align=center><font color="red">You have left fields blank. Please relogin</font></center></p>';
}
elseif($type == "password")
{
echo '<p><center><font color="red">Incorrect password. Please relogin</font></center></p>';
}
}
echo '</body>';
?>
my login page is
<body link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF" bgcolor="#000000" text="#FFFFFF">
<p align="center">
<img border="0" src=logo.jpg></img></p>
<p align="center">
<form action=check.php method=post>
<p align="center"><font size=1><b><font face="Tahoma" size="2">Login.</font></b><br><br>
<samp style="font-weight: normal; font-size: 7pt; font-family: tahoma"><font face="Tahoma" size="2"><b>Email -</fomt></b></font></samp><font face="Tahoma" size="2"><b>
<input type="text" name="email" size="20" style="border: 1px solid black">
</b></font></font><font face="Tahoma" size="2">
<br>
<samp style="font-weight: normal; font-size: 7pt; font-family: tahoma">
<b><font face="Tahoma" size="2">Pass -</font></b></samp><b><font size="1" face="Tahoma"><input type="password" name="password" size="20" style="border: 1px solid black"></font></b><br><input type="submit" value="Log In" style="border: 1px solid black">
<br>
</form>
</body>
and my check.php page is
<?
session_start();
include("setup.php");
if(!$email || !$password) {
error("field");
exit();
}
if($password == $info['password']) {
session_register("password");
session_register("email");
include("top.php");
echo "<p><b><center>Welcome</center></b></p>";
echo "<p><u><b>Members Area</b></u></p>";
echo "<p><center>- <b><a href=>Page</a></b> -</center></p>";
echo "<p><center>- <b><a href=logout.php>Log Out</a></b> -</center></p>";
include("bottom.php");
}
else
{
error("password");
}
?>
I always get a error of You have left fields blank. Please relogin from my index page so I cannot get my user to log in
where have I failed?
I think you have not even set your variables :D That is why it is saying "You have left fields blank"
session_start();
include("setup.php");
After this include
$email = $_POST["email"];
$password = $_POST["password"];
Besides this : Use prepared statements to avoid SQL Injection
The problem may be this:
$db = mysql_connect("$host", "$dblogin", "$dbpassword");
I think you meant:
$db = mysql_connect("{$host}", "{$dblogin}", "{$dbpassword}");
You cannot include variables into a string without the {}.
But even in that case, you need to define those varaibles before this line.
$host = "ipaddress";
Also, once you get beyond that, you are treating strings as booleans:
if(!$email || !$password)

editing CSS of PHP grid

I have an editable grid where I want to edit the CSS such that the textarea to show the maximum width, but somehow I can't increase the width of the text area.
My database has three columns:
ID
Name
Gossip
I'm retrieving everything and displaying it in an editable grid using PHP.
index.php code
<?php
$db = new mysqli('localhost', 'root', '', 'bollywood');
$db->set_charset('utf8');
if ($db->connect_errno) {
die('Check the database connection again!');
}
$userQuery = 'SELECT Id,Name,Gossip FROM bollywood';
$stmt = $db->query($userQuery);
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var textBefore = '';
$('#grid').find('td input').hover(function() {
textBefore = $(this).val();
$(this).focus();
}, function() {
var $field = $(this),
text = $field.val();
$(this).blur();
// Set back previous value if empty
if (text.length <= 0) {
$field.html(textBefore);
} else if (textBefore !== text) {
// Text has been changed make query
var value = {
'row': parseInt(getRowData($field)),
'column': parseInt($field.closest('tr').children().find(':input').index(this)),
'text': text
};
$.post('user.php', value)
.error(function() {
$('#message')
.html('Make sure you inserted correct data')
.fadeOut(3000)
.html('&nbsp');
$field.val(textBefore);
})
.success(function() {
$field.val(text);
});
} else {
$field.val(text);
}
});
// Get the id number from row
function getRowData($td) {
return $td.closest('tr').prop('class').match(/\d+/)[0];
}
});
</script>
<title></title>
</head>
<body>
<?php if ($stmt): ?>
<div id="grid">
<p id="message">Click on the field to Edit Data</p>
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Gossip</th>
</tr>
</thead>
<tbody>
<?php while ($row = $stmt->fetch_assoc()): ?>
<tr class="<?php echo $row['Id']; ?>">
<td><input type="text" value="<?php echo $row['Id']; ?>" /> </td>
<td><input type="text" value="<?php echo $row['Name']; ?>" /></td>
<td ><input type="textarea" cols="500" rows="100" value="<?php echo $row['Gossip']; ?>" /></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
<?php else: ?>
<p>No actors added yet</p>
<?php endif; ?>
</body>
</html>
user.php code
<?php
// Detect if there was XHR request
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$fields = array('row', 'column', 'text');
$sqlFields = array('Id', 'Name', 'Gossip');
foreach ($fields as $field) {
if (!isset($_POST[$field]) || strlen($_POST[$field]) <= 0) {
sendError('No correct data');
exit();
}
}
$db = new mysqli('localhost', 'root', '', 'bollywood');
$db->set_charset('utf8');
if ($db->connect_errno) {
sendError('Connect error');
exit();
}
$userQuery = sprintf("UPDATE bollywood SET %s='%s' WHERE Id=%d",
$sqlFields[intval($_POST['column'])],
$db->real_escape_string($_POST['text']),
$db->real_escape_string(intval($_POST['row'])));
$stmt = $db->query($userQuery);
if (!$stmt) {
sendError('Update failed');
exit();
}
}
header('Location: index.php');
function sendError($message) {
header($_SERVER['SERVER_PROTOCOL'] .' 320 '. $message);
}
style.css code
body {
font: normal 14px Comic Sans, Comic Sans MS, cursive;
}
table {
width: 500px;
}
td, th {
border: 1px solid #d8d8bf;
}
th {
padding: 5px;
font: bold 14px Verdana, Arial, sans-serif;
}
td {
padding: 10px;
width: 200px;
}
td input {
margin: 0;
padding: 0;
// width:200px;
font: normal 14px sans-serif;
/** Less flicker when :focus adds the underline **/
border: 1px solid #fff;
}
td input:focus {
outline: 0;
border-bottom: 1px dashed #ddd !important;
}
#grid input {
// width: 200%;
}
You doing it wrong
<td ><input type="textarea" cols="500" rows="100" value="<?php echo $row['Gossip']; ?>" /></td>
Should be:
<td ><textarea cols="500" rows="100"><?php echo $row['Gossip']; ?></textarea>
textarea is html tag name but not input type. so change this.
<td ><input type="textarea" cols="500" rows="100" value="<?php echo $row['Gossip']; ?>" /></td>
to
<td ><textarea cols="500" rows="100"><?php echo $row['Gossip']; ?></textarea>
also add this css.
<style>
textarea {
resize: both;
width:700px;
}
</style>
also are you sure that you can get content using this.
<?php echo $row['Gossip']; ?>

Categories