jQuery validate name array, required name array - php

I'm running into a bit of trouble with the jQuery validate plugin.
I have a form with some name array checkboxes and corresponding name array radio inputs.
What I'm after is to only validate the action[] inputs when the (not required) check[] input has been checked.
$(document).ready(function(){
$("#testForm").validate({
rules: {
'action[]': {
required: function () {
return $("'check[]':checked");
}
}
}
});
});
<form id="testForm" name="testForm" method="post" action="" >
<table cellpadding="1" cellspacing="1" border="1">
<tr>
<th> </th>
<th>Include</th>
<th>Action</th>
</tr>
<tr>
<th>Row[0]:</th>
<td><input type="checkbox" name="check[0]" value="Y" /></td>
<td>
<input type="radio" name="action[0]" class="required" value="ON" />ON<br/>
<input type="radio" name="action[0]" class="required" value="OFF" />OFF<br/>
<label class="error" for="action[0]" generated="true"></label>
</td>
</tr>
<tr>
<th>Row[1]:</th>
<td><input type="checkbox" name="check[1]" value="Y" /></td>
<td>
<input type="radio" name="action[1]" class="required" value="ON" />ON<br/>
<input type="radio" name="action[1]" class="required" value="OFF" />OFF<br/>
<label class="error" for="action[1]" generated="true"></label>
</td>
</tr>
<tr>
<td colspan="3"><input type="submit" value="Submit Form" /></td>
</tr>
</table>
</form>
I have set up a jsfiddle to hopefully explain the problem in more detail:
http://jsfiddle.net/h2dt4t30/2/
Any help or guidance would be appreciated.
Thanks in advance

You could try something like
jQuery.validator.addClassRules("action-required", {
required: function(el){
return $(el).closest('tr').find('input[name^="check"]').is(':checked')
}
});
$("#testForm").validate({});
//since snippet doesn't allow form submit
$(':button').click(function(){
console.log($("#testForm").valid())
})
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/jquery.validate.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/additional-methods.js"></script>
<form id="testForm" name="testForm" method="post" action="" >
<table cellpadding="1" cellspacing="1" border="1">
<tr>
<th> </th>
<th>Include</th>
<th>Action</th>
</tr>
<tr>
<th>Row[0]:</th>
<td><input type="checkbox" name="check[0]" value="Y" /></td>
<td>
<input type="radio" name="action[0]" class="action-required" value="ON" />ON<br/>
<input type="radio" name="action[0]" class="action-required" value="OFF" />OFF<br/>
<label class="error" for="action[0]" generated="true"></label>
</td>
</tr>
<tr>
<th>Row[1]:</th>
<td><input type="checkbox" name="check[1]" value="Y" /></td>
<td>
<input type="radio" name="action[1]" class="action-required" value="ON" />ON<br/>
<input type="radio" name="action[1]" class="action-required" value="OFF" />OFF<br/>
<label class="error" for="action[1]" generated="true"></label>
</td>
</tr>
<tr>
<td colspan="3"><input type="button" value="Submit Form" /></td>
</tr>
</table>
</form>

jsfiddle seems to be down for me at the moment, so I will update the answer as soon as it's back online
The validation plugin provides the depends option.
Please try like this:
$("#testForm").validate({
rules: {
'action[]': {
depends: function () {
return $("check[]").is(":checked"); //here you need to target the needed checkbox, not all of them
}
}
}
});

Related

Transform value of PHP form on submit

i'm setting up donate option on a website using przelewy24.pl. They have this startup template to send values by $_GET method to their website.
Everything works fine exept the amount field. Przelewy24 needs a gr amount (like cents) and i would like for the donor to type in integer in full zł (like $).
If the upper is not clear - when i type 100 in the field, it sends it to przelewy24 as 100 gr, whitch will be 1 zł.
I need to know how could i format the amount sent to them as in simple calculation - when 100 is typed, get sends 10000. (x*100)
The form used is shown below. The quick-start guide is avaliable here, but only in polish
<form method="get" action="https://sklep.przelewy24.pl/zakup.php">
<input type="hidden" name="z24_id_sprzedawcy" value="TWOJ_ID">
<input type="hidden" name="z24_crc" value="KLUCZ_ZAKUPU">
<input type="hidden" name="z24_return_url" value="TWOJASTRONA.PL">
<input type="hidden" name="z24_language" value="pl">
<table>
<tr>
<td align="right">Nazwa produktu:</td>
<td>
<input type="text" name="z24_nazwa" value="Opłata za rezerwację NR: 04/234/A3953">
</td>
</tr>
<tr>
<td align="right">Dodatkowy opis:</td>
<td>
<textarea name="z24_opis" style="width:250px">Dodatkowe informacje...
</textarea>
</td>
</tr>
<tr>
<td align="right">Do zapłaty:</td>
<td><input type="text" name="z24_kwota"></td><!--KWOTA W GROSZACH-->
</tr>
</table>
<input type="submit" value="zapłać z przelewy24.pl">
</form>
You can do it with a simple Javascript code.
You need to capture the value from input, transform it, and put the value on input hidden:
function formatMoney(e) {
document.getElementById('z24_kwota').value = (!isNaN(e.target.value) ? e.target.value : 0) * 100
// just to debug.. you can remove this line:
document.getElementById('final_value').innerHTML = document.getElementById('z24_kwota').value
}
<form method="get" action="https://sklep.przelewy24.pl/zakup.php">
<input type="hidden" name="z24_id_sprzedawcy" value="TWOJ_ID">
<input type="hidden" name="z24_crc" value="KLUCZ_ZAKUPU">
<input type="hidden" name="z24_return_url" value="TWOJASTRONA.PL">
<input type="hidden" name="z24_language" value="pl">
<table>
<tr>
<td align="right">Nazwa produktu:</td>
<td>
<input type="text" name="z24_nazwa" value="Opłata za rezerwację NR: 04/234/A3953">
</td>
</tr>
<tr>
<td align="right">Dodatkowy opis:</td>
<td>
<textarea name="z24_opis" style="width:250px">Dodatkowe informacje...
</textarea>
</td>
</tr>
<tr>
<td align="right">Do zapłaty:</td>
<td>
<input type="hidden" name="z24_kwota" id="z24_kwota">
<input type="text" onkeyup="formatMoney(event)"></td><!--KWOTA W GROSZACH-->
</tr>
</table>
<input type="submit" value="zapłać z przelewy24.pl">
</form>
<!-- you can remove this line: -->
Final Value: <span id="final_value"></span>
Try changing the value before submitting the form like below,
<form method="get" id="myform" action="https://sklep.przelewy24.pl/zakup.php">
<input type="hidden" name="z24_id_sprzedawcy" value="TWOJ_ID">
<input type="hidden" name="z24_crc" value="KLUCZ_ZAKUPU">
<input type="hidden" name="z24_return_url" value="TWOJASTRONA.PL">
<input type="hidden" name="z24_language" value="pl">
<table>
<tr>
<td align="right">Nazwa produktu:</td>
<td>
<input type="text" name="z24_nazwa" value="Opłata za rezerwację NR: 04/234/A3953">
</td>
</tr>
<tr>
<td align="right">Dodatkowy opis:</td>
<td>
<textarea name="z24_opis" style="width:250px">Dodatkowe informacje...
</textarea>
</td>
</tr>
<tr>
<td align="right">Do zapłaty:</td>
<td><input type="text" name="z24_kwota"></td><!--KWOTA W GROSZACH-->
</tr>
</table>
<input type="submit" value="zapłać z przelewy24.pl">
</form>
<script
src="http://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script type="text/javascript">
var firstSubmit = false;
$('#myform').on('submit',function(e){
if(!firstSubmit){
e.preventDefault();
firstSubmit = true;
var amount = parseInt($('input[name=z24_kwota]').val());
$('input[name=z24_kwota]').val(amount*100);
$('#myform').trigger('submit');
}
})
</script>
Note: I have given an id to the form as myform

I need the (Submit this form )button to be clicked automatically when the page loads (WORDPRESS)

iam customizing a page in wordpress , i dont need the user to see this page when it loads , so i need the Sumnit this form button to be clicked automatically the moment this page loads-for security reasons-...the user doesnt need to fill anything on this form.
this is the button code:
<td colspan="2" align="center"><input name="submitBtn" type="submit"value="Submit This Form" /></td>
and this is the complete code:
[insert_php]
date_default_timezone_set('Asia/Kolkata');
$dateTime= date('Y:m:d-H:i:s');
function getDateTime() {
global $dateTime;
return $dateTime;
}
function createHash($chargetotal,$Currency) {
$storeId = "330995001";
$sharedSecret = "sharedsecret";
$dateTime= date('Y:m:d-H:i:s');
$stringToHash = $storeId . $dateTime . $chargetotal . $Currency .
$sharedSecret;
$ascii = bin2hex($stringToHash);
return sha1($ascii);
}
[/insert_php]
IPG Connect Sample for PHP(hashing)
<h1>Order Form</h1>
[insert_php]
$responseSuccessURL = "http://127.0.0.1:8006/wordpress/?page_id=27";
$responseFailURL = "http://127.0.0.1:8006/wordpress/?page_id=31";
[/insert_php]
<form action="https://test.ipg-online.com/connect/gateway/processing"
method="post"><input name="timezone" type="hidden" value="IST" />
<input name="authenticateTransaction" type="hidden" value="true" />
<table>
<tbody>
<tr>
<td>Transaction Type</td>
<td><input name="txntype" size="50" type="text" value="sale" /></td>
</tr>
<tr>
<td>Transaction Date Time</td>
<td><input name="txndatetime" size="50" type="text" value="[insert_php] echo
$dateTime; [/insert_php]" /></td>
</tr>
<tr>
<td>Calculated HASH</td>
<td><input name="hash" size="50" type="text" value="[insert_php] echo
createHash("1","356"); [/insert_php]" /></td>
</tr>
<tr>
<td>Currency</td>
<td><input name="currency" size="50" type="text" value="356" /></td>
</tr>
<tr>
<td>Payment Mode</td>
<td><input name="mode" size="50" type="text" value="payonly" /></td>
</tr>
<tr>
<td>Store Id</td>
<td><input name="storename" size="50" type="text" value="330995001" /></td>
</tr>
<tr>
<td>Chargetotal</td>
<td><input name="chargetotal" size="50" type="text" value="1" /></td>
</tr>
<tr>
<td>Shared Secret</td>
<td><input name="sharedsecret" size="50" type="text" value="sharedsecret" />
</td>
</tr>
<tr>
<td>Language</td>
<td><select name="language">
<option value="de_DE">Deutsch</option>
<option selected="selected" value="en_EN">English</option>
</select></td>
</tr>
<input type="hidden" name="responseSuccessURL" value="[insert_php] echo
$responseSuccessURL; [/insert_php]"/>
<input type="hidden" name="responseFailURL" value="[insert_php] echo
$responseFailURL; [/insert_php]"/>
<tr>
<td colspan="2" align="center"><input name="submitBtn" type="submit"
value="Submit This Form" /></td>
</tr>
<input type="hidden" name="hash_algorithm" value="SHA1"/>
</tbody>
</table>
</form>
[/insert_php]
with Javascript, you can do it like
window.onload = function() {
document.getElementById("formId").submit();
};
You can do it with JQuery like this:
<script type="text/javascript">
$(document).ready(function(){
$( "#formId" ).submit(
});
</script>
I am submitting form directly, you can click on submit button as well that will do that submit operation.
You can use trigger after document is ready,
<script type="text/javascript">
$(document).ready(function(){
$( "#foo" ).trigger( "click" );
});
</script>
Just replace the foo with your submit button ID

input required depending on button of form

To enter data into my database I created this form. The user can enter data add by clicking the add button the information is entered into the database. I am using required to force the user to enter text.
Now I added the edit and delete button. Therefore the user shall select a record with the radio buttons provided. But with the required in the input elements it is only possible to do edit or delete if text is entered.
Is it somehow possible to assign the required to a certain button?
<form>
<table>
<tr>
<td>
<input type="radio" name="aName" />
</td>
<td>a</td>
<td>record</td>
</tr>
<td>
<input type="radio" name="aName" />
</td>
<td>another</td>
<td>record</td>
</tr>
<tr>
<td></td>
<td>
<input type="text" placeholder="enter" required >
</td>
<td>
<input type="text" placeholder="something" required>
</td>
</tr>
</table>
<input type="submit" value="add" formaction="form1.php" formmethod="post"/>
<input type="submit" value="delete" formaction="form2.php" formmethod="post"/>
<input type="submit" value="edit" formaction="form3.php" formmethod="post"/>
</form>
A way to do this, is without using 'required', but reproducing this html5 effect using event handlers, focus() and alert().
event.preventDefault() prevents the form to submit.
Check it:
<form method="POST">
<table>
<tr>
<td>
<input type="radio" name="aName" />
</td>
<td>a</td>
<td>record</td>
</tr>
<td>
<input type="radio" name="aName" />
</td>
<td>another</td>
<td>record</td>
</tr>
<tr>
<td></td>
<td>
<input id="enter" type="text" placeholder="enter" >
</td>
<td>
<input id="something" type="text" placeholder="something" >
</td>
</tr>
</table>
<input id="add" formaction="form1.php" formmethod="post"
onclick="
if(document.getElementById('enter').value===''){
event.preventDefault();
document.getElementById('enter').focus();
alert('Please,fill out this field');
} else if(document.getElementById('something').value===''){
event.preventDefault();
document.getElementById('something').focus();
alert('Please,fill out this field');
}"
type="submit" value="add"/>
<input id="delete" type="submit" formaction="form2.php" formmethod="post value="delete"/>
<input id="edit" type="submit" formaction="form3.php" formmethod="post value="edit"/>
</form>

How to use associate arrays to get the id from the checkbox and appropreate value from the text field

Hi everyone, i'm new to programming. I need to develop a rating system with check boxes and text-fields where user clicks the subjects from the list and add his rating/experience in the text field. All these subjects and ratings are added to the database with the subject id and the rating. So the issue is, I don't know how to write the associate array to get the selected subjects and their appropriate rating to insert into the database. Can anyone please provide me some codes or samples similar to this. So, I can get some idea how to do this. Thanks in advance :)
This is the HTML sample code
<form action="" method="post">
<table width="372" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="24"><input type="checkbox" name="subid1" value="1" id="subid1"></td>
<td width="203">Maths</td>`enter code here`
<td width="145"><input type="text" name="sub1" id="sub1"></td>
</tr>
<tr>
<td><input type="checkbox" name="subid2" value="2" id="subid2" /></td>
<td>Physics</td>
<td><input type="text" name="subid2" id="subid2" /></td>
</tr>
<tr>
<td><input type="checkbox" name="subid3" value="3" id="subid3" /></td>
<td>Computing</td>
<td><input type="text" name="subid3" id="subid3" /></td>
</tr>
<tr>
<td><input type="checkbox" name="subid4" value="4" id="subid4" /></td>
<td>Chemistry</td>
<td><input type="text" name="subid4" id="subid4" /></td>
</tr>
<tr>
<td><input type="checkbox" name="subid5" value="5" id="subid5" /></td>
<td>Biology</td>
<td><input type="text" name="subid5" id="subid5" /></td>
</tr>
<tr>
<td><input type="checkbox" name="subid7" value="6" id="subid7" /></td>
<td>Human Biology</td>
<td><input type="text" name="subid6" id="subid6" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="button" id="button" value="Submit" /></td>
<td> </td>
</tr>
</table>
</form>
This will do the job on client side:
<input type="checkbox" name="cb[myID1]" value="1" />
<input type="text" name="t[myID1]" value="" />
<input type="checkbox" name="cb[myID2]" value="1" />
<input type="text" name="t[myID2]" value="" />
and this can be used on server side:
$usedTexts = array();
if ( array_key_exists("t", $_POST) && is_array($_POST["t"])
&& array_key_exists("cb", $_POST) && is_array($_POST["cb"])
)
{
$usedTexts = array_intersect_key($_POST["t"], $_POST["cb"]);
}
see manual for server side: http://us3.php.net/array_intersect_key
edit: fixed to POST; added array_key_exists() and is_array()

if the field empty JQUERY

I wrote a simple page with HTML and PHP. So, before PHP I would like to check empty fields with Jquery, but I do not learn the Jquery yet, so I would appreciated if someone helped me.
<?php
if(isset($_POST[add]))
{
if(empty($_POST[name]) || empty($_POST[surname])) {echo 'All form fields are required';}
}
?>
<form action="" method=post>
<table border=0 cellspacing=10 cellpadding=5>
<tr>
<td>Name:</td>
<td><input type="text" name="name" size="10" value=""></td>
</tr>
<tr>
<td>Surname:</td>
<td><input type="text" name="surname" size="10" value=""></td>
</tr>
<tr>
<td colspan=2><input type="submit" name="add" value="Add"></td>
</tr>
</table>
</form>
You can add validation by adding jquery.
$('input[name="add"]').click(function() {
if($('input[name="name"]').val() == '' || $('input[name="surname"]').val() == '') {
$('.error').html('');
if($('input[name="name"]').val() == '') {
$('.error').append('<p>Please enter your name</p>');
}
if ($('input[name="surname"]').val() == '') {
$('.error').append('<p>Please enter your surname</p>');
}
$('.error').show();
return false;
}
return true;
})
html code
<div class="error" style="display:none"></div>
<form action="" method=post>
<table border=0 cellspacing=10 cellpadding=5>
<tr>
<td>Name:</td>
<td><input type="text" name="name" size="10" value=""></td>
</tr>
<tr>
<td>Surname:</td>
<td><input type="text" name="surname" size="10" value=""</td>
</tr>
<tr>
<td colspan=2><input type="submit" name="add" value="Add"></td>
</tr>
</table>
</form>
Add a javascript function on submit let say validate() and add this jquery code in that
function validate() {
var name = $("#name").val();
if (name == "") {
alert('Name is required');
return false;
}
}
<input class="field-name" type="text" name="name" size="10" value="">
i added a class to this input field so i can use a jquery selector
var name = $('.field-name').val();
if(name="") {
console.log('name is empty');
//your code here to do something when the field is empty
}
Following code can give you some idea. I would suggest to read tutorial on jquery at
http://www.w3schools.com/jquery/default.asp
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<script>
$(document).ready(function(){
$("#add").submit(function(){
if ($('#name').length == ""){
alert("ERROR in name!");
}
});
});
</script>
<form action="" method=post>
<table border=0 cellspacing=10 cellpadding=5>
<tr>
<td>Name:</td>
<td><input type="text" name="name" id="name" size="10" value=""></td>
</tr>
<tr>
<td>Surname:</td>
<td><input type="text" name="surname" id="surname" size="10" value=""</td>
</tr>
<tr>
<td colspan=2><input type="submit" name="add" id="add" value="Add"></td>
</tr>
</table>
</form>
</html>

Categories