PHP form within jQuery - php

I have a PHP form posting to a database fine. Have included it within a jQuery web page. The form is not posting to the database. Below is the code for the webpage and the PHP file. The form behaves fine when it the javascript files are not included.
form
<link href="jquery.mobile-1.0.min.css" rel="stylesheet" type="text/css"/>
<script src="jquery-1.6.4.min.js" type="text/javascript"></script>
<script src="jquery.mobile-1.0.min.js" type="text/javascript"></script>
<meta name="viewport" content="width=device-width; initial-scale= -3.0; maximum-scale=0.0; user-scalable=0;"/>
<meta name="format-detection" content="telephone=no" />
</head>
<body>
<table border="1">
<tr>
<td align="center">Form Input Employees Data</td>
</tr>
<tr>
<td>
<table>
<?php include 'input.php'; ?>
<form method="post" action="input.php">
<tr>
<td>Date</td>
<td><input type="date" name="date" size="20">
</td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="name" size="20">
</td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="address" size="40">
</td>
</tr>
<tr>
<td>Phone</td>
<td><input type="tel" name="phone" size="20">
</td>
</tr>
<tr>
<td>Email</td>
<td><input type="email" name="email" size="20">
</td>
</tr>
<tr>
<td>Problem</td>
<td><input type="text" name="problem" size="40">
</td>
</tr>
<tr>
<td>Description</td>
<td><textarea cols="40" rows="5" name="description"></textarea>
</td>
</tr>
<tr>
<td>Time Arrived</td>
<td><input type="time" name="timeArrived" size="20">
</td>
</tr>
<tr>
<td>Time Departed</td>
<td><input type="time" name="timeDeparted" size="20">
</td>
</tr>
<tr>
<td>Cost of Material</td>
<td><input type="number" name="material" size="40">
</td>
</tr>
<tr>
<td>Total Job Cost</td>
<td><input type="number" name="total" size="40">
</td>
</tr>
<tr>
<td>Completed Yes/No</td>
<td><input type="checkbox" name="completed" value="Yes">Yes<br>
<input type="checkbox" name="completed" value="No">No
</td>
</tr>
<tr>
<td>Reason fo Incompletion</td>
<td><textarea cols="40" rows="5" name="incomplete"></textarea>
</td>
</tr>
<tr>
<td></td>
<td align="right"><input type="submit" name="submit" value="Sent"></td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</body>
</html>
PHP File:
<?
mysql_connect("localhost","$user","$password");//database connection
mysql_select_db("&database");
$order = "INSERT INTO table1
(date, first_name, address, phone, email, problem, description, time_arrived, time_departed, cost_material, total, completed, reason)
VALUES
('$_POST[date]', '$_POST[name]', '$_POST[address]', '$_POST[phone]','$_POST[email]','$_POST[problem]','$_POST[description]','$_POST[timeArrived]','$_POST[timeDeparted]',
'$_POST[material]','$_POST[total]','$_POST[completed]','$_POST[incomplete]')";
$result = mysql_query($order); //order executes
if($result){
echo("<br>Input data is succeed");
} else{
echo("<br>Input data is fail");
}
?>

First, I'd rewrite the php code to use mysqli or PDO and prepared statements. As it is right now, it's very vulnerable to sql injections.
Next, I'd revise the javascript code using to actually submit the form.
Finally, I'd either fire up a PHP debugger or print out the _POST array to make sure that the values are being passed correctly.

I think the code where you include the script is right, I don't underestand what do you wanna do when you say that: "the javascript files are not included" but the code to include the script is right, that code should work unless you have a problem with PATH.

Related

PHP: trouble with understanding POST and GET at the same time

I have a webpage where I ask input from the user and when he clicks the add button, it should insert the input from the user into the database. I always thought that you couldn't use POST and GET at the same time but according to this and this answers it should be possible. Yet, I can't seem to get their solutions working.
The script inserts something in the db (an id is generated) but the seriesName field remains empty.
The PHP script:
<?php
$con=mysqli_connect("localhost","user","passwd","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$issueSeries = mysqli_real_escape_string($con, $_GET['addSeries']);
mysqli_query($con,"INSERT INTO series (seriesName) VALUES ('$issueSeries')");
mysqli_close($con);
?>
The HTML:
<div id="issueAddInformationLayout">
<div id="issueAddCredits">
<form action="addIssue.php" method="post">
<table>
<tr id="lblAddCreator">
<td><p>NR</p></td>
<td><p>Series</p></td>
<td><p>Volume</p></td>
<td><p>Title</p></td>
<td><p>Publisher</p></td>
</tr>
<tr>
<td><input type="text" id="addNR" size="3%"/></td>
<td><input type="text" id="addSeries" size="25%" /></td>
<td><input type="text" id="addVolume" size="25%" /></td>
<td><input type="text" id="addTitle" size="30%" /></td>
<td><input type="text" id="addPublisher" size="30%" /></td>
</tr>
</table>
<table>
<tr id="lblAddCreator">
<td><p>Writer</p></td>
<td></td>
<td></td>
</tr>
<tr id="txtAddCreator">
<td><input type="text" id="addWriter1" size="30%" /></td>
<td><input type="text" id="addWriter2" size="30%" /></td>
<td><input type="text" id="addWriter3" size="30%"/></td>
<td><input type="text" id="addWriter4" size="30%"/></td>
</tr>
<tr id="lblAddCreator">
<td><p>Editor</p></td>
<td></td>
<td></td>
</tr>
<tr id="txtAddEditor">
<td><input type="text" id="addEditor1" size="30%" /></td>
<td><input type="text" id="addEditor2" size="30%"/></td>
<td><input type="text" id="addEditor3" size="30%"/></td>
<td><input type="text" id="addEditor4" size="30%"/></td>
</tr>
<tr id="lblAddCreator">
<td><p>Letterer</p></td>
<td></td>
<td></td>
</tr>
<tr id="txtAddLetterer">
<td><input type="text" id="addLetterer1" size="30%"/></td>
<td><input type="text" id="addLetterer2" size="30%"/></td>
<td><input type="text" id="addLetterer3" size="30%"/></td>
<td><input type="text" id="addLetterer4" size="30%"/></td>
</tr>
<tr id="lblAddCreator">
<td><p>Colourist</p></td>
<td></td>
<td></td>
</tr>
<tr id="txtAddColourist">
<td><input type="text" id="addColourist1" size="30%"/></td>
<td><input type="text" id="addColourist2" size="30%"/></td>
<td><input type="text" id="addColourist3" size="30%"/></td>
<td><input type="text" id="addColourist4" size="30%"/></td>
</tr>
<tr id="lblAddCreator">
<td><p>Cover Artist</p></td>
<td></td>
<td></td>
</tr>
<tr id="txtAddCoverArtist">
<td><input type="text" id="addCoverArtist1" size="30%"/></td>
<td><input type="text" id="addCoverArtist2" size="30%"/></td>
<td><input type="text" id="addCoverArtist3" size="30%"/></td>
<td><input type="text" id="addCoverArtist4" size="30%"/></td>
</tr>
<tr id="lblAddCreator">
<td><p>Inker</p></td>
<td></td>
<td></td>
</tr>
<tr id="txtAddInker">
<td><input type="text" id="addInker1" size="30%"/></td>
<td><input type="text" id="addInker2" size="30%"/></td>
<td><input type="text" id="addInker3" size="30%"/></td>
<td><input type="text" id="addInker4" size="30%"/></td>
</tr>
<tr id="lblAddCreator">
<td><p>Penciler</p></td>
<td></td>
<td></td>
</tr>
<tr id="txtAddPenciler">
<td><input type="text" id="addPenciler1" size="30%"/></td>
<td><input type="text" id="addPenciler2" size="30%"/></td>
<td><input type="text" id="addPenciler3" size="30%"/></td>
<td><input type="text" id="addPenciler4" size="30%"/></td>
</tr>
<tr id="lblAddCreator">
<td><p>Trade Paper Back</p></td>
<td><p id="lblAddCover">Cover</p></td>
</tr>
</table>
<table>
<tr id="txtAddTPB">
<td><input type="text" id="addTPB" size="30%"/></td>
<td>
<p id="btnAddCover" enctype="multipart/form-data" action="parse_file.php" method="post">
<input type="file" name="uploaded_file">
</p>
</td>
<td>
<id="btnAddIssue"><input type="submit" value="Add">
</td>
</tr>
</table>
</form>
</div>
</div>
There seemed to be some confusion about the HTML so I added the full HTML code. Keep in mind: this is purely for myself and I don't really care about stuff like that tables are outdated :)
In HTTP, you can't use GET and POST at the same time but you can make a POST request which has a query string as part of the URL.
PHP will populate $_POST with the body of the request and $_GET with the query string, even if the request was a POST request.
Since the form is POST, the data from the inputs will be put into the body and not the query string (so will appear in $_POST not $_GET).
If the value of addSeries was fixed then you could use it with $_GET like so:
<form id="btnAddIssue" action="addIssue.php?addSeries=someValue" method="post">
… but since you are taking user input, use $_POST to read the value after you put the input inside the form.
Your input is outside of your form so it isn't submitted.
<form id="btnAddIssue" action="addIssue.php" method="post">
<input type="text" id="addSeries" size="25%" />
<input type="submit" value="Add">
</form>
You then need to change $_GET['addSeries'] to $_POST['addSeries'] since your form is set to POST (or change your form to submit via GET).
Your input is outside the form tag which won't be considered when user submits the form.
<form id="btnAddIssue" action="addIssue.php" method="post">
<input type="text" id="addSeries" size="25%" />
<input type="submit" value="Add">
</form>
And since you are making a post request onto php side it would be $_POST to reference that input's value
Also, sometimes it would be good to make use of $_REQUEST which contains data from both GET and POST so you have proper data from request everytime
$issueSeries = mysqli_real_escape_string($con, $_REQUEST['addSeries']);
Your text filed is placed outside the form and so no value (of that text filed) will be submitted to the action page.
Change it to
<form id="btnAddIssue" action="addIssue.php" method="post">
<input type="text" id="addSeries" size="25%" />
<input type="submit" value="Add">
</form>
Also use $_POST, or you can even use $_REQUEST (you can use $_REQUEST for both get and post variables)

Chained Selects with Jquery Ajax

I new to programming, and have just finished the jquery course on CodeAcademy.I'm currently trying to create a chained select using Jquery's AJAX function to call a php page which runs a query on my database and echoes it out to my main html page.
Currently, I am able to load only my first , the second and third selects do not seem to be working, and i do not know what exactly it is that i'm doing wrong.
Jquery Code:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#category").load("AddItemCat.php");
});
$("#category").onchange(function(){
var category=$("#category").val();
$("#subcat").load("AddItemSubCat.php?category="+category);
});
$("#subcat").onchange(function(){
var category=$("#category").val();
var subcat=$("#subcat").val();
$("#subcat").load("AddItemSubCat.php?category="+category+"&subcat="+subcat);
});
My HTML Form:
<form action="<?PHP echo $_SERVER['PHP_SELF'] ?>" name="edititem" enctype="multipart/form-data" method="POST">
<table border='1'>
<tr>
<td colspan="3">
Category:
<select name="category" id="category" ></select>
SubCategory:
<select id="subcat" name="subcat"></select>
Item:
<select id="item" name="item"></select>
</td>
</tr>
<tr>
<td>Item Name</td>
<td><input type="text" name="itemname" size="30" maxlength="50" required="required"></td>
</tr>
<tr>
<td>Item Price</td>
<td><input type="number" name="itemprice" size="30" min="1" required="required"></td>
</tr>
<tr>
<td>Item Info</td>
<td><textarea name="iteminfo" col="40" rows="10" maxlength="300" required="required"></textarea>
</tr>
<tr>
<td>Filename:</td>
<td><input type="file" name="upload[]" /></td>
</tr>
<tr>
<td>Filename:</td>
<td><input type="file" name="upload[]" /></td>
</tr>
<tr>
<td>Filename:</td>
<td><input type="file" name="upload[]" /></td>
</tr>
<tr>
<td>Filename:</td>
<td><input type="file" name="upload[]" /></td>
</tr>
<tr>
<td>Filename:</td>
<td><input type="file" name="upload[]" /></td>
</tr>
<tr>
<td colspan="2"><input type="SUBMIT" name="Button" value="Submit"></td>
</tr>
<tr>
<td colspan="2"><?PHP if(isset($errormsg)){echo"$errormsg";}?></td>
</tr>
<tr>
<td colspan="3"><font color="#FF0000"></font></td>
</tr>
I would really appreciate it if someone could point out my mistake and give me pointers on rectifying it.Thanks!
Way to much code to help you but since you say that the jQuery part does not work:
$("#category").onchange(function(){
This should be
("#category").on("change", function(){
There is no onchange in jQuery 1.10 (or any other version?). A brief look into the console would have shown you the error I guess. Additionally, you shoud put all your other calls inside $(document).ready as well.
try use page url instead of file name. e.g - url to AddItemSubCat.php

Mysql update with php [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have a point system were there's a quiz. when they click submit from the quiz i want them to get +1 into points in mysql. i got a datbase called login and table users, and in the users i want the points to be updated with 1+ when they have taken the quiz.
the code for the quiz_form i use:
:<!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>Registration Form</title>
</head>
<body>
<table width="328" border="0" align="center">
<form id="form1" name="form1" method="post" action="script.php">
<tr>
<td colspan="2"><h2 style="color:#FF0000">Quiz1</h2></td>
</tr>
<tr>
<td>UserName:</td>
<td>
<input name="txtusername" type="text" id="txtusername" value="<?php
session_start();
if ($_SESSION['username'])
{
echo "".$_SESSION['username'];
}
else
header ("location: welcome1.html");
?>
" />
</td>
</tr>
<tr>
<td>Who is Obama?</td>
<td><input type="password" name="txtPassword" id="txtPassword" /></td>
</tr>
<tr>
<td>What was the bing thing that were going to happend in 2012?</td>
<td><input type="text" name="txtemail" id="txtemail" /></td>
</tr>
<tr>
<td>What is a farrari?</td>
<td>
<input type="text" name="txtnummer" id="txtnummer" />
</td>
</tr>
<tr>
<td>What is a spoon used for?</td>
<td>
<input type="text" name="txtnummer" id="txtnummer" />
</td>
</tr>
<tr>
<td>Witch letter does this url start with?</td>
<td>
<input type="text" name="txtnummer" id="txtnummer" />
</td>
</tr>
<tr>
<td>Are COD a game?</td>
<td>
<input type="text" name="txtnummer" id="txtnummer" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="btnRegister" id="btnRegister" value="submit" />
</td>
</tr>
</form>
</table>
<table width="328" border="0" align="center">
<form id="form1" name="form1" method="post" action="script2.php">
<tr>
<td colspan="2"><h2 style="color:#FF0000">Quiz2</h2></td>
</tr>
<tr>
<td>UserName:</td>
<td>
<input name="txtusername" type="text" id="txtusername" value="<?php
if ($_SESSION['username'])
{
echo "".$_SESSION['username'];
}
else
header ("location: welcome1.html");
?>
" />
</td>
</tr>
<tr>
<td>Who evented facebook?</td>
<td><input type="password" name="txtPassword" id="txtPassword" /></td>
</tr>
<tr>
<td>What is a cat?</td>
<td><input type="text" name="txtemail" id="txtemail" /></td>
</tr>
<tr>
<td>What is a lamborgini?</td>
<td>
<input type="text" name="txtnummer" id="txtnummer" />
</td>
</tr>
<tr>
<td>What is goodel used for?</td>
<td>
<input type="text" name="txtnummer" id="txtnummer" />
</td>
</tr>
<tr>
<td>What is the last letter in this url</td>
<td>
<input type="text" name="txtnummer" id="txtnummer" />
</td>
</tr>
<tr>
<td>Are Counter strik a game?</td>
<td>
<input type="text" name="txtnummer" id="txtnummer" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="btnRegister" id="btnRegister" value="submit" />
</td>
</tr>
</form>
</table>
<table width="328" border="0" align="center">
<form id="form1" name="form1" method="post" action="script3.php">
<tr>
<td colspan="2"><h2 style="color:#FF0000">Quiz3</h2></td>
</tr>
<tr>
<td>UserName:</td>
<td>
<input name="txtusername" type="text" id="txtusername" value="<?php
if ($_SESSION['username'])
{
echo "".$_SESSION['username'];
}
else
header ("location: welcome1.html");
?>
" />
</td>
</tr>
<tr>
<td>What is yourube used for?</td>
<td><input type="password" name="txtPassword" id="txtPassword" /></td>
</tr>
<tr>
<td>What is a dog?</td>
<td><input type="text" name="txtemail" id="txtemail" /></td>
</tr>
<tr>
<td>What is a kebab?</td>
<td>
<input type="text" name="txtnummer" id="txtnummer" />
</td>
</tr>
<tr>
<td>Who is the most famed tenager in 2012/13?</td>
<td>
<input type="text" name="txtnummer" id="txtnummer" />
</td>
</tr>
<tr>
<td>What is the 4th letter in this url?</td>
<td>
<input type="text" name="txtnummer" id="txtnummer" />
</td>
</tr>
<tr>
<td>100*2 - 5 +105 -55 +45= </td>
<td>
<input type="text" name="txtnummer" id="txtnummer" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="btnRegister" id="btnRegister" value="submit" />
</td>
</tr>
</form>
</table>
</body>
</html>
here is the script i use for the quiz:
<?php
//=============Configuring Server and Database=======
$host = 'localhost';
$user = 'root';
$password = '';
//=============Data Base Information=================
$database = 'login';
$conn = mysql_connect($host,$user,$password) or die('Server Information is not Correct'); //Establish Connection with Server
mysql_select_db($database,$conn) or die('Database Information is not correct');
//===============End Server Configuration============
//=============Starting Registration Script==========
$username = mysql_real_escape_string($_POST['txtusername']);
//=============To Encrypt Password===================
//============New Variable of Password is Now with an Encrypted Value========
if(isset($_POST['btnRegister'])) //===When I will Set the Button to 1 or Press Button to register
{
$query = mysql_query("SELECT * FROM quiz WHERE username='$username'");
if(mysql_num_rows($query) > 0 ){
echo "Sorry but you can only take a quize once:S";
}else{
mysql_query ("insert into quiz(username)values('$username')");
$sql = "UPDATE users SET points = COALESCE(level,0)+1 WHERE username = $username";
header('location: succes.php');
}
}
?>
again, this work great. but i just need so they get 1 more points. so if they have 10 points and take this quiz they get 11 after clicking submit
Try this update query
$qry = "UPDATE users SET points = points+1 WHERE username = $username"
and as was said in comments, start using PDO or MySqli with prepared statements for your future codes, as old mysql commands are deprecated.
I think you have a problem over here:
$sql = "UPDATE users SET points = COALESCE(level,0)+1 WHERE username = $username";
so, if you are doing like this.
try this,
$sql = "UPDATE users SET points = COALESCE(level+1,0) WHERE username = $username";

Error Placing on same page PHP

some of my code in addUser.php it pure html
addUser.php
<form action="process/addNewUser.php" method="POST" id="userForm">
<table width="79%" border="0" cellspacing="6" cellpadding="1"
class="tabcont">
<tr>
<td width="47%" align="right">Title:</td>
<td width="53%">
<select name="title"><option value='0'>- - select - -</option>
</select>
</td>
</tr>
<tr>
<td width="47%" align="right">
First Name:</td>
<td width="53%"><input type="text" id="firstname" name="firstName" class="required" /></td>
</tr>
<tr>
<td align="right">Middle Name:</td>
<td><input type="text" id="middlename" name="middleName" class="name" /></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input name="password" value="" readonly="readonly"
id="password" class="required" " /></td>
</tr>
<tr>
<td align="right" colspan="2">
<input name="addNewUser" type="submit" class="submit" id="submit" value="Submit" />
</td>
</tr>
</table>
</form>
addNewUser.php
Here i am doing validations and displaying error messages and if it is success sending him to another page.
But i want to show an error message on addUser.php instead of validations page. Please give me a sample code how i can do it.
addNewUser.php
Add the message in the session
$_SESSION["ErrorMsg"]="Name already exists";
And in your addUser.php , you put the following code in the place you need to show the msg :
if($_SESSION["ErrorMsg"])
{
echo $_SESSION["ErrorMsg"];
$_SESSION["ErrorMsg"]="";
}

Load Returned mySQL Values Into Form

I wonder whether someone may be able to help me please.
I've put together a form and php code (below) that allows an administrator to search for member records from a mysql database using the email address as the search criteria.
HTML Form
<form name="memberpasswordresetform" id="memberpasswordresetform" method="post" action="search.php">
<div class="container">
<p align="justify">Member Details </p>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="26%" height="25"><strong>Email Address </strong></td>
<td width="4%"> </td>
<td width="70%"><input name="email" type="email" id="email" size="50" /></td>
</tr>
<tr>
<td height="25"><strong>Confirm Email Address </strong></td>
<td> </td>
<td><input name="conf_email" type="email" id="conf_email" size="50" /></td>
</tr>
<tr>
<td height="25"><label>
<input type="submit" name="Submit" value="search" />
</label></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"><strong>First Name </strong></td>
<td> </td>
<td><input name="fname" type="text" id="fname" size="30" /></td>
</tr>
<tr>
<td height="25"><strong>Last Name </strong></td>
<td> </td>
<td><input name="lname" type="text" id="lname" size="30" /></td>
</tr>
<tr>
<td height="25"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"><strong>New Password</strong></td>
<td> </td>
<td><input name="newpass" type="password" id="newpass" size="30" /></td>
</tr>
<tr>
<td height="25"><strong>Confirm New Password </strong></td>
<td> </td>
<td><input name="conf_newpass" type="password" id="conf_newpass" size="30" /></td>
</tr>
<tr>
<td height="25"><input type="submit" name="save" value="save" /></td>
<td> </td>
<td> </td>
</tr>
</table>
</div>
</form>
PHP Script
<?php
include("admin/link.php");
include("admin/opendb.php");
mysql_select_db ("userdetails");
$term = $_POST['email'];
$sql = mysql_query("select forename, surname, email address from userdetails where emailaddress like '%$email%'");
while ($row = mysql_fetch_array($sql)){
echo '<br/> First Name: '.$row['forename'];
echo '<br/> Last Name: '.$row['surname'];
echo '<br/><br/>';
}
?>
The search functionality works fine, but I can't work out how to populate the forename and surname fields on my form from the records retrieved from my database. I've been looking for, and found examples on how to do this, if I want to simply show the data as a table, but I can't find any that explain how to populate the fields in a form.
I just wondered whether it would be at all possible that someone could provide some guidance please on how I can do this.
Many thanks
The previous answer will work fine if short tags are enabled on the server. You should stick to the long syntax as below in case you change hosting providers at a later date.
<input name="fname" type="text" id="fname" size="30" value="<?php echo $row['surname']; ?>" />
just set the value of your input element
<tr>
<td height="25"><strong>First Name </strong></td>
<td> </td>
<td><input name="fname" type="text" id="fname" size="30" value="<?= $row['surname'] ?>" /></td>
</tr>

Categories