Jquery empty field add message beside the field - php

Jquery:
$(document).ready(function(){
$("#login_form").submit(function(event){
var username = $.trim($("#username").val());
var password = $.trim($("#password").val());
if(username.length < 1){
if(!$("#user_error").length){
$("#username_error").append("<p id='user_error'>Required</p>");
}
}
if(username.length < 1|| password.length < 1){
$("#username_error").append("<p id='user_error'>Required</p>");
$("#password_error").append("<p id='pass_error'>Required</p>");
event.preventDefault();
}
});
});
View:
<? $attr = array( "id" => "login_form"); echo form_open("KGLogin/verify_login",$attr); ?>
<table id="loginform">
<tr>
<th>Log-in</th>
</tr>
<tr>
<td>
<input type="text" name="username" id='username' placeholder="Username" />
</td>
<td id="username_error"></td>
</tr>
<tr>
<td>
<input type="password" name="password" id='password' placeholder="Password" />
</td>
<td id="password_error"></td>
</tr>
<tr>
<td>
<input type="submit" name="login" value="Login" />
</td>
</tr>
</table>
<? echo form_close(); ?>
Is there a simpler way to do this? IF i submit it twice without inputting anything, it will continue to append. Im a newbie to jquery, Cant seem to find a simple way to do things, all search results in google directs me to complicated jqueries.
How do i keep the function from stacking the error message.

Ill just put this as an answer, so no one would suggest.
<?if(form_error('username'))echo "required";;?>

Put the following code in autoload file.
$autoload['helper'] = array('url','array','html','form');
Then try this in view file
<?php if (form_error('your field name'))
{
echo "required";
}
;?>

Related

submit button to input value to database without form action PHP

I would like click on submit and the value input in the field to be stored in database.
However, I do not want to use a form action. Is it possible to do it without creating form action with PHP?
<tr>
<form method="post">
<tr>
<td>
<label for="Item name"><b>Finish Product:</b></label>
</td>
<td>
<input id="finish_product" type="text" maxlength="100" style="width:100px"name="finish_product" required>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Save" id="submit" />
</td>
</tr>
<?php
if(isset($_POST['submit']))
{
var_dump($_POST); exit;
$SQL = "INSERT INTO bom (finish_product) VALUES ('$finish_product')";
$result = mysql_query($SQL);
}?>
</tr>
Use Jquery ajax to do this. Try this:
HTML
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<body>
<table>
<tr>
<td><label for="Item name"><b>Finish Product:</b></label></td>
<td><input id="finish_product" type="text" maxlength="100" style="width:100px" name="finish_product" required></td>
</tr>
</table>
<input type="button" value="Save" id="submit" />
<script>
$(document).ready(function(){
$('#submit').click(function(){
$.ajax({
url: '1.php',
data: {finish_product: $('#finish_product').val()},
success: function (result) {
alert(result)
}
});
});
});
</script>
</body>
PHP
(1.php)
Note: Use mysqli_query since mysql_query is depricated in latest versions. And use bind param instead of directly appending values to query.
<?php
if (!empty($_GET)) {
$SQL = "INSERT INTO bom (finish_product) VALUES ('".$_GET['finish_product:']."')";
$result = mysql_query($SQL);
echo 1;
} else {
echo -1;
}
?>
This isn't a form, this is just a series of table elements with various inputs and a submit button. Clean up the code - there's no closing tr tag, and where is the submit button supposed to be?
Add a form element around it.
You need an method attribute to the form - in this case, "post". Without the action attribute, it will default to the same page.
<!-- add form tag -->
<form method="post">
<tr>
<td>
<label for="Item name"><b>Finish Product:</b></label>
</td>
<td>
<input id="finish_product" type="text" maxlength="100" style="width:100px"name="finish_product" required>
</td>
</tr>
<!-- added new row for submit button - you might want to have the td element span two columns? -->
<tr>
<td>
<input type="submit" value="Save" id="submit" />
</td>
</tr>
</form>
<-- end of form -->
<?php
if(isset($_POST['submit']))
{
//see what is being POSTED - comment out this line when you're happy with the code!
var_dump($_POST); exit;
$SQL = "INSERT INTO bom (finish_product) VALUES ('$finish_product')";
$result = mysql_query($SQL);
}

PHP How to Insert Data Input from a Dynamically-Generated Form

I need assistance in taking the next step in inserting data entered into a dynamically-generated form. I have looked for a number of days at other similar questions and am still missing a piece.
Here is my HTML form (minus error trapping/validations):
<form id="form" name="form" method="post" action="recipeaddsubmit.php">
<table id="myTable">
<tr>
<td width="10%">Amount</td>
<td width="10%">Measure</td>
<td width="35%">Ingredient</td>
</tr>
<tr>
<td valign="top">
<input type="text" name="Amt1" id="Amt1" size="1" />
<input type="text" name="Amt2" id="Amt2" size="1" />
</td>
<td valign="top">
<select name="Measure" id="Measure">
<option>Cup</option>
<option>Ounce</option>
</select>
</td>
<td valign="top">
<input type="text" name="Ing" id="Ing" size="40" />
</td>
</tr>
</table>
<button type="button" onclick="displayResult()">Insert new row</button>
<input type="submit" name="button" id="button" value="Submit" />
</form>
Here is the javascript in my header:
<script>
function displayResult()
{
var table=document.getElementById("myTable");
var row=table.insertRow(-1);
var cell1=row.insertCell(0);
var cell2=row.insertCell(1);
var cell3=row.insertCell(2);
cell1.innerHTML="<input type='text' name='Amt1' id='Amt1' size='1' />
<input type='text' name='Amt2' id='Amt2' size='1' />";
cell2.innerHTML="<select name='Measure' id='Measure'> <option>Cup</option>
<option>Ounce</option></select>";
cell3.innerHTML="<input type='text' name='Ing' id='Ing' size='40' />";
}
</script>
And here is my attempted/partial mysqli insert statement (checkInput is a self-created validation function I have used before without issue):
$amt1 = checkInput($_POST['Amt1']);
$amt2 = checkInput($_POST['Amt2']);
$measure = checkInput($_POST['Measure']);
$ing = checkInput($_POST['Ing']);
$ingAddQuery ="INSERT INTO Ingredient (Amt1, Amt2, Measure, Ing)
VALUES ({$amt1}, {$amt2}, {$measure}, {$ing})";
mysqli_query($mysqli,$ingAddQuery);
if (!mysqli_query($mysqli,$ingAddQuery))
{
die('Error: ' . mysqli_error());
}
What I don't understand is how to incorporate a foreach loop in terms of how to increment for a certain number of rows; I understand the concept but not the application in this case.
Thank you for your assistance!
change the name from amt1 to amt1[] and etcetera...
so
<input type="text" name="Amt1[]" id="Amt1[]" size="1" />
When you submit the form,
$_POST['Amt1']; //is an array
will be an array, so you could do
$Amt1Array = $_POST['Amt1'];
foreach($Amt1Array => $Amt1){
//do stuff here
}
Better yet you can index the array using the js to create names like...
<input type="text" name="Amt1[0]" id="Amt1[0]" size="1" />
<input type="text" name="Amt2[0]" id="Amt2[0]" size="1" />
<input type="text" name="Amt1[1]" id="Amt1[1]" size="1" />
<input type="text" name="Amt2[1]" id="Amt2[1]" size="1" />
Then in the PHP you could
$Amt1Array = $_POST['Amt1'];
$Amt2Array = $_POST['Amt2'];
foreach($Amt1Array as $key => $Amt1Value){
$Amt2Value = $Amt2Array[$key];
//do stuff with rows
}
I have a suggestion.
Why don't u create a hidden input type.
<input type="hidden" name="cellCount" value="0" />
Each time u insert a new row, change value of this hidden field. This will not show on browser. This hidden element will also get submit when u submit form. this can be retrieved on server by $_POST["cellCount"].
Hope this will help u.

how to remove a table row by a button?

I have created a form in which we can add multiple rows by click on a button. but I dont't know how to delete that rows.
The Code:
<form>
<table>
<?php
for ($i=0; $i<100; $i++) {
$inst_no = $i+1;
// Display only the first line
if ($nbr_ligne == 0) $nbr_ligne = 1;
if ($i >= $nbr_ligne) $display = 'style="display:none"';
echo '
<tr id="cell'.$i.'" '.$display.'>
<td align="center"><b>'.$inst_no.'</b><br> </td>
<td><input type="text" name="total_balance[]" id="total_balance'.$i.'" class="payment_text_box" /><br>
<input type="button" value="Add Installment" class="installment_button" onclick="javascript:document.getElementById(\'cell'.($i+1).'\').style.display=\'table-row\'; this.style.display=\'none\'" />
</td>
<td><input type="text" name="installment_amount[]" id="installment_amount'.$i.'" class="payment_text_box"/><br> </td>
<td><input type="text" name="remaining_balance[]" id="remaining_balance'.$i.'" class="payment_text_box" /><br>
<input name="submit" type="button" value="Remove Installment" class="installment_button" onclick="remove();" />
</td>
<td><input type="text" name="installment_date[]" id="installment_date'.$i.'" class="payment_text_box"/><br> </td>
</tr>';
}
?>
</table>
</form>
Please tell me what is the way to remove that rows?
ithink this is use for how to add and remove table row by button.
http://www.mredkj.com/tutorials/tableaddrow.html
In your JavaScript remove() function add the something like the following (untested):
thisItem.parentNode.parentNode.parentNode.removeChild(thisItem.parentNode.parentNode);

Check if the radio button is pushed

I have a form that has a foreach loop and inside the loop I have an input type radio, so every time it has a new value like the codes below:
<?php
$aircrafts = AircraftPermit::getaircraft();
foreach($aircrafts as $aircraft)
{
$pacas = AircraftPermit::getrouteaircraft($aircraft->name ,$pilotid);
if($pacas)
{
?>
<tr>
<td>
<input type="radio" id="ac" value="" disabled="disabled"><?php echo $aircraft->name ;?><br />
</td>
<td align="center">
<input name="submit" title="Give permission to selected aircraft" type="submit" value="Give Permit" disabled="disabled">
</td>
<td align="center">
<font color="green">Granted</font>
</td>
</tr>
<?php
}
else
{
?>
<tr>
<td>
<input type="radio" id="ac" value="<?php echo $aircraft->name ;?>"><?php echo $aircraft->name ;?><br />
</td>
<td align="center">
<input name="id" type="hidden" value="<?php echo $pilotid ;?>">
<input name="submit" title="Give permission to selected aircraft" type="submit" value="Give Permit">
</td>
<td align="center">
<font color="red">Restricted</font>
</td>
</tr>
<?php
}
}
?>
Now at the end I have a script to check if the radio button is selected like below:
<script type="text/javascript">
function radio_is_on() {
var elementId = document.getElementById('ac');
if (elementId.checked==false)
{
alert('Please select the aircraft first!');
return false;
}
if (elementId.value)
{
return true;
}
}
</script>
When the radio button is not pushed the message pops up okay but when it's pushed it returns no value and the form send null value. Please tell me where I'm wrong.
Thanks
You cannot have multiple id tags, which is probably why it's not working. Try changing it to a class instead. See below for example in jquery.
HTML:
<input type="radio" class="ac" value="<?php echo $aircraft->name ;?>"><?php echo $aircraft->name ;?><br />
JAVASCRIPT:
function radio_is_on() {
if ($('.ac:checked').val()) {
return true;
}
else {
alert('Please select the aircraft first!');
return false;
}
}
You have same id for all radio buttons - that is not correct.
And only one button is considered all the time.
You can number them, eg. ac1, ac2 and so on.
as far as I can remember a radio button is "pushed" when it has the checked attribute, and it is not pushed when it doesn't have it.
...
if (elementId.hasOwnProperty('checked'))
...
instead of
...
if (elementId.checked==false)
...

Enable/Disable Submit Button based on radio buttons

I have a form layed out like this:
<form action="join-head-2-head.php" method="POST" enctype="application/x-www-form-urlencoded">
<table border="0" cellspacing="4" cellpadding="4">
<tr>
<td colspan="2"><input name="player1rules" type="radio" id="tandcy" value="y" />
<label for="tandcy">I Have Reviewed The Rules And The Terms & Conditions And Agree To Abide By Them</label></td>
</tr>
<tr>
<td colspan="2"><input name="player1rules" type="radio" id="tandcn" value="n" checked="checked" /><label for="tandcn">I Do Not Agree To The Terms And Condtions And/Or Have Not Read The Rules</label></td>
</tr>
<tr>
<td width="100"><input name="player1" type="hidden" value="<? $session->username; ?>" /></td>
<td><input type="submit" name="join" id="join" value="Take Available Slot" /></td>
</tr>
</table>
</form>
What I am hoping to do is disable the submit button if id="tandcn" is selected, and enable it when id="tandcy". Is there an easy way to do that using javascript?
example http://jsfiddle.net/sWLDf/
$(function () {
var $join = $("input[name=join]");
var processJoin = function (element) {
if(element.id == "tandcn") {
$join.attr("disabled", "disabled");
}
else {
$join.removeAttr("disabled")
}
};
$(":radio[name=player1rules]").click(function () {
processJoin(this);
}).filter(":checked").each(function () {
processJoin(this);
});
});
$(":radio[name='player1rules']").click(function() {
var value = $(this).val();
if (value === "n") {
$("#join").attr("disabled", "disabled");
return;
}
$("#join").removeAttr("disabled");
});
Example on jsfiddle
Lots answers based on jquery (which I recommended to use). Here your form with javascript
<script type="text/javascript">
function disable(id){
document.getElementById(id).disabled = 'disabled';
}
function enable(id){
document.getElementById(id).disabled = '';
}
</script>
<form action="join-head-2-head.php" method="POST" enctype="application/x-www-form-urlencoded">
<table border="0" cellspacing="4" cellpadding="4">
<tr>
<td colspan="2"><input name="player1rules" type="radio" id="tandcy" value="y" onclick='enable("join")' />
<label for="tandcy">I Have Reviewed The Rules And The Terms & Conditions And Agree To Abide By Them</label></td>
</tr>
<tr>
<td colspan="2"><input name="player1rules" onclick='disable("join")' type="radio" id="tandcn" value="n" checked="checked" /><label for="tandcn">I Do Not Agree To The Terms And Condtions And/Or Have Not Read The Rules</label></td>
</tr>
<tr>
<td width="100"><input name="player1" type="hidden" value="<? $session->username; ?>" /></td>
<td><input type="submit" DISABLED name="join" id="join" value="Take Available Slot" /></td>
</tr>
</table>
</form>
However more elegant way is use jquery.
$(document).ready(function(){
if($('#tandcy').is(':checked')){
$('#join').attr('disabled','disabled');
}
if($('#tandcn').is(':checked')){
$('#join').removeAttr('disabled','disabled');
}
$('#tandcn').click(function(){
$('#join').attr('disabled','disabled');
});
$('#tandcy').click(function(){
$('#join').removeAttr('disabled','disabled');
})
});
Try this....
you need jquery for this,....
$("#tandcn #tandcy").livequery('change', function(event){
if ($('#tandcn').is(":checked"))
{
$('#join').hide();
//or
$('#join').attr("disabled", false);
}
else($('#tandcy').is(":checked"))
{
$('#join').show();
//or
$('#join').attr("disabled", false);
}
});

Categories