Not able to reset the input field jquery php wordpress - php

I need to reset the field onclick of reset button
But I am not able to do that.
Here is my html code
<table cellspacing="2" cellpadding="5" style="width: 100%;" class="form-table1" id="form-table1" >
<tbody>
<tr class="form-field1">
<th valign="top" scope="row">
<label for="approved_mailCc"><?php _e('OpenLab Cc', 'custom_table_example')?></label>
</th>
<td>
<input id="approved_mailCc" name="approved_mailCc" type="email" value="<?php echo isset($item['approved_mailCc']) ? $item['approved_mailCc'] : ''; ?>"
size="50" class="code" placeholder="<?php _e('OpenLab Cc', 'custom_table_example')?>" required />
<input type="button" value="Reset" id="approved_mailCc" name="openLab"/>
</td>
</tr>
jQuery code
jQuery(document).ready(function($){
console.log("plugin script loaded2");
$('#approved_mailCc').click(function(){
$(this).val('');
});
});
1 I am getting console log.
2 alert is not going inside onclick
3 i used reset function also.

You have two inputs with the same ID of "approved_mailCc" you can't have more than one item with the same ID, it will confuse jQuery. Give them unique IDs like below:
<table cellspacing="2" cellpadding="5" style="width: 100%;" class="form-table1" id="form-table1" >
<tbody>
<tr class="form-field1">
<th valign="top" scope="row">
<label for="approved_mailCc"><?php _e('OpenLab Cc', 'custom_table_example')?></label>
</th>
<td>
<input id="approved_mailCc_email" name="approved_mailCc" type="email" value="<?php echo isset($item['approved_mailCc']) ? $item['approved_mailCc'] : ''; ?>"
size="50" class="code" placeholder="<?php _e('OpenLab Cc', 'custom_table_example')?>" required />
<input type="button" value="Reset" id="approved_mailCc_button" name="openLab"/>
</td>
</tr>
and then use this jQuery code:
jQuery(document).ready(function(){
console.log("plugin script loaded2");
jQuery('#approved_mailCc_button').click(function(){
jQuery('#approved_mailCc_button_email').val('');
});

First, you are not supposed to need Javascript to reset a form (but you need the tag) :
https://jsfiddle.net/9xfonyou/
Second, there are errors on your code double IDs.
Here "approved_mailCc" is both your input file and your button. Then you can do this in Javascript to make it work.
HTML :
<form>
<table cellspacing="2" cellpadding="5" style="width: 100%;" class="form-table1" id="form-table1">
<tbody>
<tr class="form-field1">
<th valign="top" scope="row">
<label for="approved_mailCc"></label>
</th>
<td>
<input id="input_field" name="approved_mailCc" type="email" value="" size="50" class="code" placeholder="test" required />
<input type="reset" id="reset_button" name="openLab" />
</td>
</tr>
</tbody>
</table>
</form>
JS :
jQuery(document).ready(function($) {
console.log("plugin script loaded2");
$('#reset_button').click(function(e) {
e.preventDefault();
$('#input_field').val('');
});
});
https://jsfiddle.net/9xfonyou/1/

Related

How to insert duplicated rows of different data into Database in PHP & HTML

This is example html code i used. Basically, when I click 'Add New', the new row appear and I typed in the data. But the problem is only the data of last row (if i add 3 rows, it is only inserting 3rd row's data) is inserting into database
currently i used the simple sql query method to insert into database and I don't know how to modify the sql query or codes to make it store all the duplicated rows' data into databse.
If you can help me with this, it'd be most appreciated. Thanks !!
http://jsfiddle.net/2HGdv/13/
Here is how its work on jsfiddle.
one point is I don't use that <? $i ?> beside my variables in my actual html codes because when I add it, I got errors.
<br>
<table id="maintable" width="50%" cellpadding="0" cellspacing="0" class="pdzn_tbl1" border="#729111 1px solid">
<tr>
<th align="center">Roll No</th>
<th align="center">First Name</th>
<th align="center">Last Name</th>
</tr>
<tr id="rows">
<div style="padding-left: 5px">
<td style="padding:5px;">
<input type="text" name="rollno<? $i ?>" />
</td>
<td style="padding:5px;">
<input type="text" name="firstname<? $i ?>" />
</td>
<td style="padding:5px;">
<input type="text" name="lastname<? $i ?>" />
</td>
</div>
</tr>
</table>
<br><div id="add_new">ADD NEW
</div>
This is the script for duplicating the rows.
$("#add_new").click(function () {
$("#maintable").each(function () {
var tds = '<tr>';
jQuery.each($('tr:last td', this), function () {
tds += '<td>' + $(this).html() + '</td>';
});
tds += '</tr>';
if ($('tbody', this).length > 0) {
$('tbody', this).append(tds);
} else {
$(this).append(tds);
}
});
});
You overwrite your data because input attribute name remains the same. You need to make an HTML array that holds each row. Then in your js you will append one row with index the total .rows like var total_rows = $(".rows").length;
<form method="post" action="stack.php">
<table id="maintable" width="50%" cellpadding="0" cellspacing="0" class="pdzn_tbl1" border="#729111 1px solid">
<tr>
<th align="center">Roll No</th>
<th align="center">First Name</th>
<th align="center">Last Name</th>
</tr>
<tr class="rows">
<td style="padding:5px;">
<input type="text" name="item[0][rollno]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[0][firstname]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[0][lastname]" />
</td>
</tr>
<tr class="rows">
<td style="padding:5px;">
<input type="text" name="item[1][rollno]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[1][firstname]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[1][lastname]" />
</td>
</tr>
</table>
<div id="add_new">ADD NEW</div>
<input type="submit" value="Save all items" name="saveButton">
</form>
<?php
if (isset($_POST['item']) && is_array($_POST['item'])) {
$items = $_POST['item'];
$i = 0;
foreach($items as $key => $value) {
echo 'Item '.$i++.': '.$value['rollno'].' '.$value['firstname'].' '.$value['lastname'].'<br />';
}
}
?>
So your next row will be the following:
<tr class="rows">
<td style="padding:5px;">
<input type="text" name="item[2][rollno]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[2][firstname]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[3][lastname]" />
</td>
</tr>

Using tabbed pane for multiple form data insertion from php into mysql

I am creating an application in Html, php with Mysql. On one of my webform I am using Tabs created with Radio buttons as tab heads to select the tab perticular tab. I am facing the issue to insert tab1 values into the database while other 4 tabs fire same query and their data is easily inserted. Its the problem with the 1st Tab only and I realized it when I changed the tabs order. Please help me I want to work with Tabbed pane to provide multiple input forms to the user. Thanks in advance.
Here here is my web form Tab1.php
<form action="" enctype="multipart/form-data" method="post">
<table width="100%" border="0" align="center">
<tr>
<td> Date </td>
<td><input type="DATE" required name="ClaimDate"/></td>
</tr>
<tr>
<td>From</td>
<td><input type="text" maxlength="" required size="20" Placeholder="Source" name="ClaimFrom"></td>
<td align="center">To</td>
<td><input type="text" size="20" maxlength="" required Placeholder="Destination" name="ClaimTo"></td>
</tr>
<tr>
<td>Amount</td>
<td><input type="number" size="5" name="ClaimAmt" required Placeholder="In Rupees" ></td>
<td colspan = 3><input type="file" name="ClaimReceipt"></td>
</tr>
<tr>
<td colspan="6" ><table border="0" Width="30%" height=40px >
<tr>
<td width="20%" ><input type="submit" name="Add" value="Add"></td>
<td width="20%" ><input name="AdBtn3" type="reset" value="clear" width="48" height="40" ></td>
</tr>
</table></td>
</tr>
</table>
</form>
InsertData.php
include('dbcon.php');
$User = 'abc';
$ExCategory = mysql_real_escape_string($_REQUEST['ExCategory']);
$ClaimDate = mysql_real_escape_string($_REQUEST['ClaimDate']);
if(isset($_REQUEST['ClaimFrom']) && isset($_REQUEST['ClaimTo']))
{
$ClaimFrom = mysql_real_escape_string($_REQUEST['ClaimFrom']);
$ClaimTo = mysql_real_escape_string($_REQUEST['ClaimTo']);
}
$ClaimAmt = mysql_real_escape_string($_REQUEST['ClaimAmt']);
$ClaimClass = mysql_real_escape_string($_REQUEST['ClaimClass']);
$ClaimReceipt = $_FILES['ClaimReceipt']['size'];
$ClaimRowid = "";
if (isset($_POST['Add']))
{
mysql_query("INSERT INTO `test`.`tbluserexpenses` VALUES('$User','$ExCategory','$ClaimDate','$ClaimFrom','$ClaimTo','$ClaimAmt','$ClaimClass','$ClaimReceipt','$ClaimRowid');");
}
<div style="position:relative; z-index:1; top:85px; left:390px;"><ul class="tabs">
<li>
<input type="radio" name="tabs" id="tab1" checked />
<label for="tab1">Local</label>
<div id="tab-content1" class="tab-content">
<?php include 'Tab1.php';?>
</div>
</li>
<li>
<input type="radio" name="tabs" id="tab2" />
<label for="tab2">Express</label>
<div id="tab-content2" class="tab-content">
<?php include 'Tab2.php';?>
</div>
</li>
This way Tab1.php is attached to my mainform as a tabbed pane.

Validate dynamically created text inputs in jQuery

What I presently have is this code which displays all the invoices under a specific catalogue. On change of the catalogue dropdown, the table should be populated based on the selected dropdown value. The table displays the invoices pertaining to the selected catalogue with 3 textboxes for each recordset. And what I need is to validate those textboxes. Lets say there are 3 invoices/records for a specific catalogue, then there should be 9 textboxes.
The View Page contains:-
<script type="text/javascript">
$(document).ready(function(){
$('#catalogue').change(function(){
$.post('../controller/valuation.php', {catalogue:valuation_form.catalogue.value},
function(result){
$('#valuationResult').html(result).show();
})
});
});
</script>
<form action="" id="valuation_form" name="valuation_form">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="16%">Catalogue Code</td>
<td width="15%">
<select name="catalogue" id="catalogue" style="width:75px;">
<option></option>
<?php
require_once '../model/catalogue.php';
#$result=Catalogue::getAllCatalogues();
while($value=mysql_fetch_assoc($result)){
?>
<option value="<?php echo $value['catalogue_id']; ?>">
<?php echo $value['catalogue_code'] ?>
</option>
<?php } ?>
</select>
</td>
</tr>
</table>
</form>
<div class="atable">
<form action="../controller/valuation.php" method="POST" enctype="multipart/form-data">
<div id="valuationResult"></div>
</form>
</div>
The Controller Page contains:-
function getValuationDetails(){
require_once '../model/sales.php';
$catalogue=$_POST['catalogue'];
$obj=new Sales();
$result=$obj->getValuationEntryLotsForCatalogue($catalogue);
#$num=mysql_num_rows(#$result);
if($num>0){
$table='<table id="tabled" class="tftable" border="1">
<thead>
<tr>
<th style="display:none" width="0%">INVOICE ID</th>
<th width="6%">LOT #</th>
<th width="15%">SELLING MARK</th>
<th width="9%">INVOICE #</th>
<th width="8%">PACKAGES</th>
<th width="8%">GRADE</th>
<th width="13%">NET WEIGHT(Kg)</th>
<th style="text-align:center">DESCRIPTION</th>
<th width="11%" style="text-align:center;">VALUATION </th>
</tr>
</thead>';
while($value=mysql_fetch_assoc($result)){
$table.='<tr>
<td style="display:none">
<input type="text" id="invoice" name="invoice[]" value="'.$value['invoice_id'].'"/>
</td>
<td>'.$value['lot_no'].'</td>
<td>'.$value['selling_mark'].'</td>
<td>'.$value['ref_invoice_no'].'</td>
<td>'.$value['no_of_packages'].'</td>
<td>'.$value['grade_desc'].'</td>
<td style="text-align:right;">'.$value['total_net_qty'].' </td>
<td>
<textarea style="width:270px;max-width:270px;" class="description" name="description[]"></textarea>
</td>
<td>
<input type="text" class="value1" name="value1[]" size="2">
<input type="text" class="value2" name="value2[]" size="2">
</td>
</tr>';
}
$table.='<tr><td colspan="9" style="text-align:right">
<input type="hidden" name="action" value="valuation_entry"/>
<input type="submit" name="save" id="save" value="Save"/>
<input type="reset" value="Clear"/>
</td></tr>';
echo $table;
}
else{
echo "";
}
}
The three textboxes/textareas are as follows:-
1. Description- textarea that allows only letters.
2. Value1- textbox that allows only numbers.
3. Value1- textbox that allows only numbers.
*All the 3 cannot be empty.
I have tried all sorts of options to try this but unfortunately I cannot figure out what and where I am missing.
Any help is very much appreciated!!!
Use class "required" to mark the fields that cannot be empty.
Then on Jquery you should loop through these elements:
var error = false;
$('.required').each(function(i){
if(!$(this).val()){
$(this).css('border', '2px solid red');
error = true;
}else{
$(this).css('border','1px solid #BBBBBB');
}
});
if(error) {
alert('All red fields are required!!!!!');
}

Submitting two form using javascript but failed

i have been trying to Submit two form using javascript and failed. i have searched stackoverflow but none of the javascript works ...
also tried this Submit two forms with one button
here is my code ... my full page is very long so i have just pasted the two form code here ...
And each field of form1 (regForm) is required .. and form2 (frmAdd) contains a while
loop... both of them forms are different i guess... so any solution with javascript or any alternative with these forms ...
<script>
submitForms = function(){
document.forms["regForm"].submit();
document.forms["frmAdd"].submit();
}
</script>
<form action="userdata.php" method="post" name="regForm" id="regForm" >
<h2><em>Personal Details : </em></h2></br>
<table width="80%" border="0" cellpadding="3" cellspacing="3" class="forms">
<tr>
<td width="22%">NAME<span class="required"><font color="#CC0000">*</font></span></td>
<td width="78%">
<input name="full_name" type="text" id="full_name" size="40" value="<? echo $row_settings['full_name']; ?>" class="required"></td>
</tr>
<tr>
<td>FATHER'S NAME<span class="required"><font color="#CC0000">*</font></span></td>
<td>
<input name="f_name" type="text" id="f_name" size="40" value="<? echo $row_settings['f_name']; ?>" class="required"></td>
</tr>
<tr>
<td>MOTHER'S NAME<span class="required"><font color="#CC0000">*</font></span></td>
<td>
<input name="m_name" type="text" id="m_name" size="40" value="<? echo $row_settings['m_name']; ?>" class="required"></td>
</tr>
<tr>
<td>NATIONALITY<span class="required"><font color="#CC0000">*</font></span></td>
<td>
<input name="nationality" type="text" id="nationality" size="40" value="<? echo $row_settings['nationality']; ?>" class="required"></td>
</tr>
<tr>
<td>RELIGION<span class="required"><font color="#CC0000">*</font></span></td>
<td>
<input name="religion" type="text" id="religion" size="40" value="<? echo $row_settings['religion']; ?>" class="required"></td>
</tr>
</table></br>
<p align="center">
<!-- previous button <input name="doSave" type="submit" id="doSave" value="Submit"> -->
</p>
</form>
<form action="userdata.php" name="frmAdd" method="post">
<table width="80%" border="0" cellpadding="3" cellspacing="3" class="forms">
<tr>
<td width="5"> <div align="center">NO</div></td>
<td width="91"> <div align="center">Employer's NAME</div></td>
<td width="160"> <div align="center">COUNTRY</div></td>
<td width="198"> <div align="center">POSITION</div></td>
<td width="70"> <div align="center">FROM</div></td>
<td width="70"> <div align="center">TO</div></td>
<td width="70"> <div align="center">SALARY</div></td>
<td width="70"> <div align="center">REASONS FOR LEAVING</div></td>
</tr>
<?php for($i=1;$i<=4;$i++) { ?>
<tr>
<th width="5"> <div align="center"><? echo $i . "."; ?></div></th>
<td><input type="text" name="emp_name<?=$i;?>" size="25"></td>
<td><input type="text" name="emp_country<?=$i;?>" size="10"></td>
<td><input type="text" name="emp_pos<?=$i;?>" size="10"></td>
<td><input type="text" name="emp_frm<?=$i;?>" size="5"></td>
<td><input type="text" name="emp_to<?=$i;?>" size="5"></td>
<td><input type="text" name="emp_sal<?=$i;?>" size="5"></td>
<td><input type="text" name="emp_lev<?=$i;?>" size="25"></td>
</tr>
<?php } ?>
</table>
</br>
<!-- previous button <input type="submit" name="doHis" value="Save"> -->
<input type="hidden" name="hdlfrm" value="<?=$i;?>">
<input type="button" value="Click Me!" onclick="submitForms()" />
You can not submit two different forms on one page. You have a race condition.
If you want to submit two forms at once, either combine them and handle it at the server OR you can use Ajax to submit them.
I think the easiest way to do this is install jQuery and use the find method then serialize the whole thing and send it using a jQuery post, get, or ajax method.
There are missing "tags" and the code is not correct in some places.
It would be much easier if you just shortened the code to relevant parts and then posted.
So I had to do that for you.
You should test the following code. As long as that does not work, there is no need even more "form input" to write.
the test.php
<!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" />
<script type="text/javascript">
submitForms = function(){
document.forms["regForm"].submit();
window.setTimeout(submitB,1000)
}
function submitB() {
document.forms["frmAdd"].submit();
}
</script>
<title>Emergency Table - Update</title>
</head>
<body>
<form action="writea.php" method="post" name="regForm" id="regForm" >
<h2><em>Personal Details : </em></h2></br>
<table width="80%" border="0" cellpadding="3" cellspacing="3" class="forms">
<tr>
<td width="22%">NAME<span class="required"><font color="#CC0000">*</font></span></td>
<td width="78%">
<input name="full_name" type="text" id="full_name" size="40" value="Romeo Delta" class="required"></td>
</tr>
</table></br>
</form>
<form action="writeb.php" name="frmAdd" method="post">
<table width="80%" border="0" cellpadding="3" cellspacing="3" class="forms">
<tr>
<td width="5"> <div align="center">TEST</div></td>
</tr>
</table>
</br>
<input type="hidden" name="hdlfrm" value="1000">
<input type="button" value="Click Me!" onclick="submitForms()" />
</form>
</body>
</html>
writea.php
<?php
if (isset($_POST['full_name'])) { $hdl = $_POST['full_name']; } else { $hdl = "Err."; }
$fp = fopen('dataa.txt', 'a+');
fwrite($fp, "Hello from write A : ".$hdl."\r\n");
fclose($fp);
?>
writeb.php
<?php
if (isset($_POST['hdlfrm'])) { $hdl = $_POST['hdlfrm']; } else { $hdl = "Err."; }
$fp = fopen('datab.txt', 'a+');
fwrite($fp, "Hello from write B : ".$hdl."\r\n");
fclose($fp);
?>
After submit the content of dataa.txt and datab.txt should be.
dataa.txt
Hello from write A : Romeo Delta
datab.txt
Hello from write B : 1000
If you do normal form post in your page the first submit action will done and redirects to that page and second form post will not done my solution is better to use ajax post method and submit these two forms hope this helpful..
Found a Solution for this ... used this jquery and jquery form js ...
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>
<script>
$(document).ready(function() {
$('#form1').ajaxForm(function() {
});
$('#form2').ajaxForm(function() {
alert("Your Submitted Information has been Saved !");
window.location = "userdata.php";
return true;
});
$("#submitEverything").click(function(){
mySubmitFunction();
return false;
});
});
function mySubmitFunction() {
$("#form2").submit();
$("#form1").submit();
}
</script>
userdata.php is the page where this code was pasted
and have to use two different action in forms (like form1 action="page1" and form2 action="page2")

form 2 button + javascript issue

<form action="index.php?page=checkin" method="post" name="regForm">
<div id="First_1">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td>
<table cellpadding="0" cellspacing="5">
<tr>
<td style="padding: 5px;">
Fullständiga namn:
</td>
<td>
<input name="full_name" type="text" id="full_name" class="required">
</td>
</tr>
<tr>
<td style="padding: 5px;">
Email:
</td>
<td>
<input name="usr_email" type="text" id="usr_email3" class="required">
</td>
</tr>
<tr>
<td style="padding: 5px;">
Sex:
</td>
<td>
<select name="sex">
<option value="male">
Kille
</option>
<option value="female">
Tjej
</option>
</select>
</td>
</tr>
<td>
<input type="submit" id="continue" value="Fortsätt">
</td>
</table>
</td>
</tr>
</table>
</div>
<div id="Next_2" style="display: none">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td>
<table cellpadding="0" cellspacing="5">
<tr>
<td style="padding: 5px;">
Lösenord:
</td>
<td>
<input name="pwd" type="password" class="required password" id="pwd">
</td>
<td>
En gång till..
</td>
<td>
<input name="pwd2" id="pwd2" class="required password" type="password">
</td>
<td>
<input name="doRegister" type="submit" id="doRegister" value="Register">
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</form>
<script type="text/javascript">
$(function() {
$("#continue").click(function() {
$.ajax({
type: "POST",
url: "dbc.php",
data: {
check: 'First',
full_name: $('#full_name').val(),
usr_email: $('#usr_email3').val()
},
success: function(msg) {
if (msg == '1') {
$("#First_1").hide();
$("#Next_2").toggle();
} else {
alert(msg);
}
}
});
return false;
});
});
</script>
Thats my form. You fill 3 form elements to start with, then you press the Continue button, and then it sends ajax call to check the fields. If everythings OK (if output = 1 from ajax call), Next_2 gets toggled on, where you enter your password twice. After that you press on the final button Register, which should do "action=index.php?page=checkin", but it doesnt, when i press register it just get blank, and i can see in firebug that it sends another ajax call which i cannot understand because the button doesnt have id="continue" but id="doRegister"
Since the "Continue" button doesn't actually submit the form, try declaring it as a normal button instead of a submit button, like this:
<input type="button" id="continue" value="Fortsätt">
I'm guessing that having two submit buttons on the same form is somehow causing interactions with the handler.
I think you should make the "Continue" button as type="button" and not submit
<input type="button" id="continue" value="Fortsätt">

Categories