javascript is not working, form is not validating - php

my login form is not getting validate. javascript is not working. please help me out. I am not getting any alerts.Do Tell me what is wrong with script.
<script type="text/javascript">
function checkForm(form)
{
if(form.username.value == "") {
alert("Error: Username cannot be blank!");
form.username.focus();
return false;
}
re = /^\w+$/;
if(!re.test(form.username.value)) {
alert("Error: Username must contain only letters, numbers and underscores!");
form.username.focus();
return false;
}
if(form.npassword.value == "") {
alert("Error: Username cannot be blank!");
form.npassword.focus();
return false;
}
re = /[A-Z]/;
if(!re.test(form.npassword.value)) {
alert("Error: password must contain at least one uppercase letter (A-Z)!");
form.npassword.focus();
return false;
}
alert("You entered a valid password: " + form.npassword.value);
return true;
}
</script>
this is login form
<div class="section section_with_padding" id="contact">
<h2>Change Password</h2>
<div class="half left">
<h4> </h4>
<table width="91%" align='center' cellpadding= "0" cellspacing= "0" class='main_tab'>
<tr>
<td class='login_table' align='left'><form action="changepass.php" method="post" onsubmit="return checkForm(this);>
<table class="login_tab">
<td class='login_table' align='left'><form method='post'>
<tr>
<td colspan="2" class='login_nam' valign='middle'>Change Password</td>
</tr>
<tr>
<td class='login style1'>Username:</td>
<td class='login'><input type='text' name='username'> </td>
</tr>
<tr>
<td class='login style1'>Old Password:</td>
<td class='login'><input type='password' name='opassword'> </td>
</tr>
<tr>
<td class='login style1'>New Password:</td>
<td class='login'><input type='password' name='npassword'> </td>
</tr>
<tr>
<td class='login' colspan='2'><input type='submit' name='submit' value='Change'>
</td>
</tr>
</table>
</form> </td>
</tr>
</table>
</div>
</div>
please some one help in validating the login page with errors on not providing any input(it should display respective error message).

Looks like you have created a nested form. Please delete the second one.
<form action="changepass.php" method="post" onsubmit="return checkForm(this);> /* Form HTML */
<td class='login_table' align='left'>
<form method='post'> /* Remove this */
Hope this helps.
Peace! xD

Related

CodeIgniter: changing password in controller

I'm trying to allow users to change their passwords in my site.
I'm getting stuck in the controller. My doubt is whether $sql = $this->db->select("*")->from("logins_table")->where('lt_username',$this->session->userdata('email'))->get(); is working or not.
My controller:
<?php
class Changepw extends MY_Controller {
public function Changepwd(){
}
public function reset() // we will load models here to check with database
{
$sql = $this->db->select("*")->from("logins_table")->where('lt_username',$this->session->userdata('email'))->get();
foreach ($sql->result() as $my_info) {
$db_password = $my_info->lt_password;
$db_id = $my_info->lt_id;
}
if($this->input->post('opassword') == $db_password && ($this->input->post('npassword') != '') && ($this->input->post('cpassword')!='')) {
$fixed_pw = mysql_real_escape_string(md5($this->input->post('npassword')));
$update = $this->db->query("Update 'logins_table' SET 'lt_password'= '$fixed_pw' WHERE 'id'= '$db_id'")or die(mysql_error());
//$this->form_validation->set_message('change',"sucess");
echo json_encode(array("success"=>true));
}else {
//$this->form_validation->set_message('change', "err");
echo json_encode(array("success"=>false));
}
exit;
}
}
and my view page is:
<table width="95%" border="0" cellspacing="5" cellpadding="5">
</tbody>
<tr>
<td width="35%" class="heading">Email</td>
<td><input type="text" name="email" ></td>
<tr>
<td class="heading">Existing Password</td>
<td><input type="password" name="opassword" ></td>
</tr>
<tr title="Ignore new password if you dont want to change password">
<td class="heading">New Password</td>
<td><input type="password" name="npassword"></td>
</tr>
<tr>
<td class="heading">Confirm Password</td>
<td><input type="password" name="cpassword"></td>
</tr>
<tr>
<td> </td>
<td><button name="Submit" id="forgotBtn" class="customBtn" value="Submit">Save changes</button>
</td>
<tr>
<td> </td>
<td ><div class="errorMsg" id="errMsg" style="display:none"> Error in updating </div></td>
</tr>
</tr>
</tbody>
</table>
</form>
$("#forgotBtn").on('click', function()
{
$.post( "/changepw/reset", $("#forgotForm").serialize(), // serializes the form's elements.
function(data) {
data = jQuery.parseJSON(data);
if(data.error == false) {
$("#successMsg").hide();
}else{
$("#errMsg").show();
}
} );
return false;
});
Thanks.
I think according to the code, your error is here.
$this->input->post('opassword') == $db_password// this line
While storing password in database you have used md5(), but here you are just checking for the plain text which is wrong
md5($this->input->post('opassword')) == $db_password
This should help you.

php form validation and display errors

I think this is very simple but I don't know how to execute it.
I have a form with some data and I have created a another php file to validate the form data, but I am unable to display the error message back into the form if validation fails. I have attached both files, but I don't know hot to execute it :(
My form.php looks like
<form name="form1" method="post" action="process/process_add_page.php">
<fieldset>
<legend>Add Page</legend>
<table width="1056" height="365" border="1">
<tr>
<th width="77" scope="col">Page Title</th>
<th width="962" scope="col"><label for="page_title"></label>
<input type="text" name="page_title" id="page_title"><span style="color:#FF0000">* <?php echo $titleerror;?></span></th>
</tr>
<tr>
<th scope="row">Page Description</th>
<td><label for="page_description"></label>
<textarea name="page_description" class="ckeditor" id="page_description" cols="100" rows="5"></textarea></td>
</tr>
<tr>
<th scope="row">Seo Title</th>
<td><label for="seo_title"></label>
<input type="text" name="seo_title" id="seo_title"></td>
</tr>
<tr>
<th scope="row">Seo Description</th>
<td><label for="seo_description"></label>
<textarea name="seo_description" class="ckeditor" id="seo_description" cols="45" rows="5"></textarea></td>
</tr>
<tr>
<th scope="row">Page Order</th>
<td><label for="page_order"></label>
<input type="text" name="page_order" id="page_order"></td>
</tr>
<tr>
<th scope="row">Page Status</th>
<td><label for="page_status"></label>
<select name="page_status" id="page_status">
<option value="1">Active</option>
<option value="0">Inactive</option>
</select></td>
</tr>
<tr>
<th colspan="2" scope="row"><input type="submit" name="btnsubmit" id="btnsubmit" value="Submit"></th>
</tr>
</table>
<p> </p>
</fieldset>
</form>
and my process_add_pages.php looks like this
<?php
require_once('../../classes/database.php');
require_once('../../classes/pages.php');
require_once('../../classes/redirect.php');
$objPage=new Page();
$titleerror='';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(empty($_POST['page_title'])){
$titleerror = "Title is required";
echo $titleerror;
}else
{
$page_title=mysqli_real_escape_string($objPage->conxn,$_POST['page_title']);
if (!preg_match("/^[a-zA-Z ]*$/",$page_title)) {
$titleerror = "Only letters and white space allowed";
}
}
$page_description=mysqli_real_escape_string($objPage- >conxn,$_POST['page_description']);
$seo_title=mysqli_real_escape_string($objPage->conxn,$_POST['seo_title']);
$seo_description=mysqli_real_escape_string($objPage->conxn,$_POST['seo_description']);
$page_order=mysqli_real_escape_string($objPage->conxn,$_POST['page_order']);
$page_status=mysqli_real_escape_string($objPage->conxn,$_POST['page_status']);
}
$objPage->setPage_title($page_title);
$objPage->setPage_description($page_description);
$objPage->setSeo_title($seo_title);
$objPage->setSeo_description($seo_description);
$objPage->setPage_status($page_status);
if($objPage->addPage()){
new Redirect('../index.php?page=page&action=view&success=The page has been created');
}else{
new Redirect('../index.php?page=page&action=view&error=The page could not be created');
}
?>
There are few little changes you have to do to make it work..
There is a couple of problem in your process_add_pages.php file. your code is ..
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(empty($_POST['page_title'])){
$titleerror = "Title is required";
echo $titleerror; /*error part-1 */
}else
{
$page_title=mysqli_real_escape_string($objPage->conxn,$_POST['page_title']);
if (!preg_match("/^[a-zA-Z ]*$/",$page_title)) {
$titleerror = "Only letters and white space allowed"; /*error part-2 */
}
}
$page_description=mysqli_real_escape_string($objPage- >conxn,$_POST['page_description']);
$seo_title=mysqli_real_escape_string($objPage->conxn,$_POST['seo_title']);
$seo_description=mysqli_real_escape_string($objPage->conxn,$_POST['seo_description']);
$page_order=mysqli_real_escape_string($objPage->conxn,$_POST['page_order']);
$page_status=mysqli_real_escape_string($objPage->conxn,$_POST['page_status']);
}
if anything goes wrong in the validation it will echo the error message in the process_add_pages.php file. but it will also execute the sql queries. so if there is any problem in validation you can redirect to the form page with an error message.
you can try this new code format...
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(empty($_POST['page_title'])){
$titleerror = "Title is required";
new Redirect('../index.php?error="The page need a page title"');
}elseif
{
$page_title=mysqli_real_escape_string($objPage->conxn,$_POST['page_title']);
if (!preg_match("/^[a-zA-Z ]*$/",$page_title)) {
new Redirect('../index.php?error="Only letters and white space allowed"');
}
}
else {
$page_description=mysqli_real_escape_string($objPage- >conxn,$_POST['page_description']);
$seo_title=mysqli_real_escape_string($objPage->conxn,$_POST['seo_title']);
$seo_description=mysqli_real_escape_string($objPage->conxn,$_POST['seo_description']);
$page_order=mysqli_real_escape_string($objPage->conxn,$_POST['page_order']);
$page_status=mysqli_real_escape_string($objPage->conxn,$_POST['page_status']);
}
}
I Think, this is the better way to Check validation. Add onsubmit in form. Check CheckValidation() function in tag.
<form name="form1" method="post" action="process/process_add_page.php" onsubmit="return CheckValidation()">
<fieldset>
<legend>Add Page</legend>
<table width="1056" height="365" border="1">
<tr>
<th width="77" scope="col">Page Title</th>
<th width="962" scope="col"><label for="page_title"></label>
<input type="text" name="page_title" id="page_title">
<span style="color:#FF0000" style="display:none" id="RequiredTitle">* Title is required</span>
<span style="color:#FF0000" style="display:none" id="OnlyLetters">* Only letters and white space allowed</span>
</th>
</tr>
<tr>
<th scope="row">Page Description</th>
<td><label for="page_description"></label>
<textarea name="page_description" class="ckeditor" id="page_description" cols="100" rows="5"></textarea></td>
</tr>
.
.
.
//Rest of the code
</form>
<script>
function CheckValidation()
{
var Title=$('#page_title').val();
if(Title=="")
{
$('#RequiredTitle').show();
return false;
}
else
{
if(!preg_match("/^[a-zA-Z ]*$/",Title))
{
$('#OnlyLetters').show();
return false;
}
}
}
</script>

How to validate a form in php?

I know how to validate a form in JavaScript, but not in php.
Here is my code:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Cafeteria Ordering System</title>
<style type="text/css">
#import "cafeteriastyle.css";
</style>
</head>
<body>
<?php
if(isset($_POST['btnSubmit']))
{
//2nd page
}
else
{
?>
<form name="form1" method="post">
<table width="500" border="0" align="center" class="TableBorder">
<tbody>
<tr>
<td colspan="2" align="center" class="TableTitle TableHeadingFill">Cafeteria Ordering System</td>
</tr>
<tr>
<td width="250" align="right"><p>Customer ID</p></td>
<td><input type="text" name="txtID"></td>
</tr>
<tr>
<td align="right"><p>Num. Of Items Order</p></td>
<td><input type="text" name="txtItems"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="btnSubmit"></td>
</tr>
</tbody>
</table>
</form>
<?php
}
?>
</body>
</html>
I have a form call "form1", inside the form1, I have 2 textfield.
When the Submit button is clicked, I want to perform a form validation check, to make sure the both textfield is filled, and jump to the 2nd page. If textfield is empty, display a alert message and stay on the same page.
I know how to do it with JavaScript, for example:
<script language="javascript" type="text/javascript">
function formValidation()
{
if(form1.elements["Name"].value.trim() == "")
{
alert("Please enter the name");
form1.elements["Name"].select();
return false;
}
return true;
}
</script>
and just need to add onSubmit="return formValidation" into the form tag like:
<form name="form1" method="post" onSubmit="return formValidation()"> then it will working probably. But how to do that with php instead of JS?
<?php
if(isset($_POST['btnSubmit']))
{
$txnid=trim($_POST['txtID']);
$txtItems=trim($_POST['txtItems']);
if(!empty($txnid) && !empty($txtItems))
{
//2nd page
}
else
{
echo 'Both Fields are Required.';
}
}
else
{
?>
<form name="form1" method="post">
<table width="500" border="0" align="center" class="TableBorder">
<tbody>
<tr>
<td colspan="2" align="center" class="TableTitle TableHeadingFill">Cafeteria Ordering System</td>
</tr>
<tr>
<td width="250" align="right"><p>Customer ID</p></td>
<td><input type="text" name="txtID"></td>
</tr>
<tr>
<td align="right"><p>Num. Of Items Order</p></td>
<td><input type="text" name="txtItems"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="btnSubmit"></td>
</tr>
</tbody>
Use zebra form http://stefangabos.ro/php-libraries/zebra-form/
it is the best
will do both php and js validation
Use this check with jquery it might be help you
function formValidation()
{
if($("input[name='name']").trim() == "")
{
alert("Please enter the name");
$("input[name='name']").focus();
return false;
}
return true;
}
Try this...
function formValidation()
{
if($("input[name='name']").val().trim() == "")
{
alert("Please Enter The Name");
return false;
}
return true;
}

Cannot find validation in html page

<head>
<title> Add User page</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<form name='f2' action="insert_ac.php" method="post" >
<script src="validation.js" type="javascript/text"></script>
</head>
<body onload="firstfocus();">
<table align="center" border="0" cellpadding="3" cellspacing="1">
<tr>
<td> First Name</td><td > : </td>
<td> <input type='text' name='fname' id='fid' size="50" style="background-color:#abcddd; height:18px;" value='' maxlength="100" onblur="fname_validation(5,12);"> </td>
</tr>
<tr>
<td> Last Name</td><td> : </td>
<td> <input type='text' name='lname' id='lid' size="50" style="background-color:#abcddd; height:18px; "value='' maxlength="100" onblur="lname_validation(5,12);"> </td>
</tr>
<tr>
<td> Gender</td><td> : </td>
<td> <input type='radio' name='gend' id='m' value='M' checked>Male <input type='radio' name='gend' id='f' value='F'>Female</td>
</tr>
<tr>
<td> Phone Number</td><td> : </td>
<td> <input type='number_format' name='phone' id='phno'size="50" style="background-color:#abcddd; height:18px; " value=''onblur="allnumeric();"></td>
</tr>
<tr>
<td> Work Experiance</td><td> : </td>
<td><select name="exp" onblur="expselect();">
<option > Select One</option>
<option selected="" value="Default"> Select One </option>
<option value="F"> Fresher </option>
<option value="E"> Experiance</option>
</select></td>
</tr>
<tr>
<td>User Name</td><td>:</td><td> <input type='txt' name='uname' id='uid'size="50" style="background-color:#abcddd; height:18px; " value=''onblur="userid_validation(5,10);"> </td>
</tr>
<tr>
<td>Password</td><td>:</td><td> <input type='password' name='pwd' id='pid' size="50" style="background-color:#abcddd; height:18px; " value=''onblur="passid_validation(7,12);"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td width="84"><input name="enter" class="btn_login" type="submit" value="Submit" onsubmit="alert('Data stored successfully');"/> <input name="cancle" class="btn_login" type="reset" value="Cancle" /></td>
</tr>
</table>
</form>
</body>
</html>
I included the external javascript file using script tag but the validations are not working.
This is my external javascript code which was saved as validation.js and i am unable to find errors in it please help me.
// After form loads focus will go to first name field.
function firstfocus()
{
var fname = document.f2.fname.focus();
return true;
}
// This function will validate First name
function fname_validation(mx,my)
{
var fname = document.f2.fname;
var fname_len = fname.value.length;
var letters = /^[A-Za-z]+$/;
if (fname_len == 0 || fname_len >= my || fname_len < mx)
{
alert("First Name should not be empty / length be between "+mx+" to "+my);
fname.focus();
return false;
if(fname.value.match(letters))
{
// Focus goes to next field i.e.Last Name
document.f2.lname.focus();
return true;
}
}
}
// This function will validate Last name
function lname_validation(mx,my)
{
var lname = document.f2.lname;
var lname_len = lname.value.length;
var letters = /^[A-Za-z]+$/;
if (lname_len == 0 || lname_len >= my || lname_len < mx)
{
alert("Last Name should not be empty / length be between "+mx+" to "+my);
lname.focus();
return false;
if(fname.value.match(letters))
{
// Focus goes to next field i.e.Phone Number
document.f2.phone.focus();
return true;
}
}
}
// This function will validate Phone Number.
function allnumeric()
{
var phone = document.f2.phone;
var numbers = /^[0-9]+$/;
if(phone.value.match(numbers))
{
// Focus goes to next field i.e. Experiance.
document.f2.exp.focus();
return true;
}
else
{
alert('Phone Number must have numeric characters only');
phone.focus();
return false;
}
}
// This function will select Experiance.
function expselect()
{
var exp = document.f2.exp;
if(exp.value == "Default")
{
alert('Select your Experiance from the list');
exp.focus();
return false;
}
else
{
// Focus goes to next field i.e. Username Code.
document.f2.uname.focus();
return true;
}
}
// This function will validate User Name.
function allLetter()
{
var uname = document.f2.uname;
var letters = /^[A-Za-z]+$/;
if(uname.value.match(letters))
{
// Focus goes to next field i.e. Password.
document.f2.pwd.focus();
return true;
}
else
{
alert('Username must have alphabet characters only');
uname.focus();
return false;
}
}
// This function will validate Password.
function passid_validation(mx,my)
{
var passid = document.registration.passid;
var passid_len = passid.value.length;
if (passid_len == 0 ||passid_len >= my || passid_len < mx)
{
alert("Password should not be empty / length be between "+mx+" to "+my);
passid.focus();
return false;
}
}
try to change <script type="javascript/text"> to <script type="text/javascript">
and double check the reference of your js file. Also, put an alert check to determine if it is really going through the firstFocus() function.
You have to reference the .js if its not in the same folder as html
EDIT
function fname_validation(mx,my)
{
is missing a closing bracket!
I changed the java script file and now i can find validations in my form...
Thanks for your help..
here i am sharing my code...
HTML code
<form name='f1' action="insert_ac.php" method="post" onSubmit="return validateForm();" >
<tr>
<td height="25" height="25" colspan="2" bgcolor="#EC6921" class="form_heading">Add User Form</td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt"> First Name:</td>
<td bgcolor="#FFFFFF"> <input type='text' name='fname' id='fid' size="50" style="height:18px;" value='' maxlength="100" ></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt"> Last Name:</td>
<td bgcolor="#FFFFFF" > <input type='text' name='lname' id='lid' size="50" style=" height:18px; "value='' maxlength="100" ></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt"> Gender:</td>
<td bgcolor="#FFFFFF" > <input type='radio' name='gend' id='m' value='M' checked>Male <input type='radio' name='gend' id='f' value='F'>Female</td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt"> Phone Number:</td>
<td bgcolor="#FFFFFF" > <input type='number_format' name='phone' id='phno'size="50" style=" height:18px; " value=''></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt"> Work Experiance:</td>
<td bgcolor="#FFFFFF" ><select name="exp" >
<option selected="" value="Default"> Select One </option>
<option value="Fresher"> Fresher </option>
<option value="Experiance"> Experiance</option>
</select></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt">User Name:</td><td bgcolor="#FFFFFF"> <input type='txt' name='uname' id='uid'size="50" style=" height:18px; " value=''></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt">Password:</td><td bgcolor="#FFFFFF"> <input type='password' name='pwd' id='pid' size="50" style=" height:18px; " value=''></td>
</tr>
</table>
<table align="center">
<tr>
<td ><input name="enter" class="btn_login" type="submit" value="Submit" align="center"><input name="cancle" class="btn_login" type="reset" value="Cancle" valign="right" /></td>
</tr>
</table>
</div>
</div>
</div>
</form>
</body>
</html>
Here is my java script file which is working fine....
var RE = /^.+#.+\..{3}$/;
var RE1 = /^[a-zA-Z]+$/;
var RE2 = /^[0-9]{10}$/;
var RE3 = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
function validateForm()
{
if (document.f1.fname.value == "")
{
window.alert("first name should NOT BE empty");
document.f1.fname.focus();
return false;
}
else
if (RE1.test(document.f1.fname.value) == false)
{
alert("Invalid first name\n\
");
return false;
}
else
if (document.f1.fname.value.length < 3)
{
window.alert("Firstname must have atleast Three characters");
document.f1.fname.focus();
return false;
}
else
if (document.f1.lname.value == "")
{
window.alert("Lastname should not be empty");
document.f1.lname.focus();
return false;
}
else
if (RE1.test(document.f1.lname.value) == false)
{
alert("Invalid last name");
return false;
}
else
if (document.f1.lname.value.length < 4)
{
window.alert("Lastname must have atleast four characters");
document.f1.lname.focus();
return false;
}
else
if (document.f1.lname.value == "")
{
window.alert("Last name should not be empty");
document.f1.lname.focus();
return false;
}
else
if (document.f1.phone.value == "")
{
window.alert("phne no should NOT BE empty");
document.f1.phone.focus();
return false;
} else
if (RE3.test(document.f1.phone.value) == false)
{
alert("Invalid phone number");
document.f1.phone.focus();
return false;
}
else
if (document.f1.exp.selectedIndex == 0)
{
window.alert("please select work experiance ");
return false;
}
else
if (document.f1.uname.value == "")
{
window.alert("UserName should not be empty");
document.f1.uname.focus();
return false;
}
else
if (RE1.test(document.f1.uname.value) == false)
{
window.alert("Invalid userName ");
document.f1.uname.focus();
return false;
}
else
if (document.f1.pwd.value == "")
{
window.alert("password should not be empty");
document.f1.pwd.focus();
return false;
}
else
if (document.f1.pwd.value.length < 6)
{
window.alert("password must have atleast six characters");
document.f1.pwd.focus();
return false;
}
else
{
window.alert("User has been added successfully.");
return true;
}
}

Javascript add MySQL data when checkbox is ticked

I am currently using this javscript and PHP code
<script>
function add(total)
{
form2.thetotal.value = document.forms["form1" + total].total.value;
}
</script>
<form name="form2">
<table width="800" border="0" cellspacing="0" cellpadding="10" style="position:fixed; z-index:-999; background-color:#FFF;">
<tr bgcolor="#eeeeee">
<td> </td>
<td colspan="2" width="50%"><strong>Total: </strong><input type="text" name="thetotal" id="thetotal" size="20" value="0" /></td>
<td colspan="2" width="50%"><strong>VAT:</strong> </td>
</tr>
<tr bgcolor="#eeeeee">
<td width="5%"> </td>
<td width="20%"><strong>Invoice Number</strong></td>
<td width="35%"><strong>Company</strong></td>
<td width="20%"><strong>Date</strong></td>
<td width="20%"><strong>Total</strong></td>
</tr>
</table>
</form>
<form name="form1">
<table width="800" border="0" cellspacing="0" cellpadding="10">
<?php
$sql="SELECT * from billing_pdf_archive order by invoice_number ASC ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs))
{
$counter++;
$sql2="SELECT * from customer where sequence = '".$result["customer_sequence"]."' ";
$rs2=mysql_query($sql2,$conn) or die(mysql_error());
$result2=mysql_fetch_array($rs2);
$sql3="SELECT * from reseller where sequence = '".$result["reseller_sequence"]."' ";
$rs3=mysql_query($sql3,$conn) or die(mysql_error());
$result3=mysql_fetch_array($rs3);
if($result["customer_sequence"] != '0')
{
$company = $result2["company"];
}
elseif($result["reseller_sequence"] != '0')
{
$company = '<strong>Reseller: </strong>'.$result3["company"];
}
$total = $result["total"];
echo '<tr>
<td width="5%"><input type="checkbox" name="check" id="check" onclick=\'add('.$total.');\' /></td>
<td width="20%">'.$result["invoice_number"].'</td>
<td width="35%">'.$company.'</td>
<td width="20%">'.$result["datetime"].'</td>
<td width="20%">£'.$result["total"].'</td>
</tr>';
}
?>
</table>
</form>
so as you can see it is selecting from the MySQL database and i am trying to make it so when one of the checkboxes is ticked it adds the total into the "thetotal" text field (in form 2) but it is just leaving that box as zero - any ideas on what i could do?
You did not specify the total field for form 1, then it raised javascript error, it wont work.
Check it out
<script>
function add(total)
{
form2.thetotal.value = total + parseFloat(document.form1.formtotal.value);
}
</script>
<form name="form1">
<!-- replace field name formtotal with anything that you want-->
<!-- replace field value with anything that you get from your mysql result-->
<input type="text" name="formtotal" id="formtotal" value="10" />
</form>
Here is how you can do it (assuming total is int and not float):
<script>
function add(total, this_chk_bx)
{
//(if its float, use `parseFloat` instead of `parseInt`)
if(this_chk_bx.checked==true){
//add if its checked
var tot_1 = parseInt(form2.thetotal.value);
form2.thetotal.value = tot_1+parseInt(total);
}
else{
//subtract if its unchecked
}
}
</script>
.
in your php, you have to send the checkbox like this:
onclick=\'add('.$total.', this);\'
.

Categories