Chained Selects with Jquery Ajax - php

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

Related

How to make edit button with php, html only no mysql

I want to bring many $_REQUEST from Tag but only $_REQUEDT['editid'] works. how to solve it.
echo "<td><a href=product.php?editid=$id, editname=$name, editqty=$qty, editprice=$price>Edit</a></td>";
//when I put $_REQUEST['editid'] in input it works but others not.
<form action="product.php">
<table>
<tr>
<td>ID</td>
<td><input type="text" name="tid" value="<?php if(isset($_REQUEST['editid'])){echo $_REQUEST['editid'];} ?>"></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="tname" value="<?php if(isset($_REQUEST['editname'])){echo $_REQUEST['editname'];}?>"></td>
</tr>
<tr>
<td>Qty</td>
<td><input type="text" name="tqty" ></td>
</tr>
<tr>
<td>Price</td>
<td><input type="text" name="tprice" ></td>
</tr>
<tr>
<td></td>
<td><button type="submit" name="btn-add">Add</button>
<button type="submit" name="btn-clear">Clear</button></td>
</tr>
</table>
</form>
The URL parameters after the 1st one should be separated by & (not ,)
Change from
<a href=product.php?editid=$id, editname=$name, editqty=$qty, editprice=$price>
to
<a href="product.php?editid=$id&editname=$name&editqty=$qty&editprice=$price">

Trying to multiply two user inputs PHP

I'm trying to make a movie theatre webpage. Very nooby, with almost no PHP and HTML experience. We are learning in school, and we are just using Adobe Dreamweaver to insert stuff like recordsets, bindings, tables etc.
Now I want to make the page where the user can order a ticket, I've created a form, however it only inserts the value of one ticket(obv). I want to make the price to show for 1 ticket, but be stored in MySQL database as price*amount of tickets. I'd be very happy if someone could help me out here:
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<table align="center">
<tr valign="baseline">
<td nowrap="nowrap" align="right">Pris_kr:</td>
<td><?php echo $row_Recordset1['pris_kr']; ?> pr billett<input type="hidden" value="<?php $row_Recordset1['pris_kr']*$row_antall_billetter;?>"/></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Personid:</td>
<td><select name="personid">
<?php
do {
?>
<option value="<?php echo $row_Recordset1['personid']?>" ><?php echo $row_Recordset1['epost']?></option>
<?php
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
?>
</select></td>
</tr>
<tr> </tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Antall_billetter:</td>
<td><input type="text" name="antall_billetter" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Rad:</td>
<td><input type="text" name="rad" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Sete:</td>
<td><input type="text" name="sete" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"> </td>
<td><input type="submit" value="Insert record" /></td>
</tr>
</table>
<input type="hidden" name="bestillingsid" value="" />
<input type="hidden" name="visningsid" value="<?php echo $_GET['visningsid']; ?>" />
<input type="hidden" name="MM_insert" value="form1" />
</form>
again, I'm a newbe and I'm aware of the fault in the code:
<td nowrap="nowrap" align="right">Pris_kr:</td>
<td><?php echo $row_Recordset1['pris_kr']; ?> pr billett<input type="hidden" value="<?php $row_Recordset1['pris_kr']*$row_antall_billetter;?>"/></td>
Am I heading towards something that could help me though?
I'm also using two views here, to get the right time and date, so the url parameters here are "visningsid" and "bestillingsid".
Some translations:
Pris_kr=Price in kr, Antall_billetter=Amount of tickets
The others are not very important I guess.
Thanks in advance
//Sander

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)

PHP form within jQuery

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.

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"]="";
}

Categories