how to use jquery ajax array post json? - php

i want to send array in ajax json post ,but some code are error.how to fig this code?
HTML
<table width="200" border="1">
<tr>
<td>table</td>
<td>
<input name="table[]" type="text" id="table[]" value="5" />
</td>
</tr>
<tr>
<td>menu</td>
<td><input name="menu[]" type="text" id="menu[]" value="noodle" /></td>
</tr>
<tr>
<td>number</td>
<td><input name="number[]" type="text" id="number[]" value="1" /></td>
</tr>
<tr>
<td>note</td>
<td><input name="note[]" type="text" id="note[]" value="no " /></td>
</tr>
<tr>
<td>table</td>
<td><input name="table[]" type="text" id="table[]" value="1" /></td>
</tr>
<tr>
<td>menu</td>
<td><input name="menu[]" type="text" id="menu[]" value="beer" /></td>
</tr>
<tr>
<td>number</td>
<td><input name="number[]" type="text" id="number[]" value="2" /></td>
</tr>
<tr>
<td>note</td>
<td><input name="note[]" type="text" id="note[]" value="-" /></td>
</tr>
</table>
<button id="save-menu">save</button>
<button id="calculator">calculator</button>
Jquery : if I click button save-menu it send some data to menu/order or I click button calculator it send some data to calculator page. But not my jquery code not send all value in array ,how to fig this code? Help me please.Thank you.
<script>
$(document).ready(function(){
$('#save-menu').click(function() {
$.post('menu/order', {
table : $('select#workplace-table').val(),
'mid[]' : $('input#order-mid').val(),
'number[]' : $('input#order-number').val(),
'note[]' : $('input#order-note').val(),
});
$('#calculator').click(function() {
$.post('menu/calculator, {
table : $('select#workplace-table').val(),
'mid[]' : $('input#order-mid').val(),
'number[]' : $('input#order-number').val(),
});
});
</script>

Don't use if(clickHandler) style syntax, that will never trigger when you want it to, instead just define the click handler:
$('#save-menu').click(function() {
//do stuff on save-menu click
})

It's the way you have the click events set up that are causing you your problems.
It should look more like this:
$('#save-menu').click(function() {
$.post(... ajax stuff...);
});

Bind an event handler to the "click" JavaScript event, or trigger that event on an element.
$( "#save-menu" ).click(function() {
//Handler for .click() called.
$.post('menu/order',{...});
});
$( "#calculator" ).click(function() {
//Handler for .click() called.
$.post('menu/calculator',{...});
});

Related

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

Form Error Handleing in include file PHP

i am new in php and i am programming a simple form file that is included in a php webpage.
this is the main page
makepedido.php
<!--Informacion Personal-->
<?php include 'personalinformation.php'; ?>
personalinformation.php
<form action="scripts.php" method="post" name="personalinfo" class="form" id="personalinfo">
<table width="100%" class="tableform">
<tr>
<td>Nombre</td>
<td><input name="Nombre" type="text"></td>
</tr>
<tr>
<td>Apellido</td>
<td><input name="Apellido" type="text" ></td>
</tr>
<tr>
<td>Direccion 1</td>
<td><input name="Direccion1" type="text" ></td>
</tr>
<tr>
<td>Direccion 2</td>
<td><input name="Direccion2" type="text" ></td>
</tr>
<tr>
<td>Ciudad</td>
<td><input name="Ciudad" type="text" ></td>
</tr>
<tr>
<td>Pais</td>
<td><input name="Pais" type="text" ></td>
</tr>
<tr>
<td>Numero de Telefono</td>
<td><input name="NumerodeTelefono" type="text" required></td>
</tr>
<tr>
<td>Correo Electronico</td>
<td><input name="CorreoElectronico" type="text" required></td>
</tr>
</table>
<br />
<br />
<input value="Crear Pedido" type="submit" class="btn btn-default"> <br />
</form>
scripts.php
<?php
$email = $_POST["CorreoElectronico"];
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
echo "Correo invalido intente de nuevo";
die;
}
?>
now i just want to know how to put in personalinformation.php the error returned by scripts.php, when i click the submit button, the browser goes to script.php and show the error msg.
i want it to stay in makepedido.php and show the error msg from the personalinformation.php form returned by scripts.php
You should use ajax to submit the form and get the error to the same page, like this:
$("#formID").submit(function() {
var url = "path/to/your/scripts.php"; // the script where you handle the form input.
$.ajax({
type: "POST",
url: url,
data: $("#idForm").serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // show response from the php script.
}
});
return false; // avoid to execute the actual submit of the form.
});

Chained Selects with Jquery Ajax

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

Jquery Button to make visible a form

I want to ask about how to make a form invisible when a button clicked? So, if I have a button called "Hide" and a form with many button, textbox, etc.
Then when I click "Hide" button, it will hide all form and all things in form such as textbox, button, etc. I have google it but have no result. I accept answer with Jquery, JS, or php language because I'm using that language program.
Example my form is like this:
<form name="myform" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<table>
<tr>
<td>ID</td>
<td>:</td>
<td><input type="text" maxlength="15" name="clientid" /></td>
<td><input type="submit" name="cariclientid" value="Search" /></td>
<td width="50px"></td>
<td>ID</td>
<td>:</td>
<td><input type="text" maxlength="15" name="orderid" /></td>
<td><input type="submit" name="cariorderid" value="Search" /></td>
</tr>
<tr>
<td>No.</td>
<td>:</td>
<td><input type="text" maxlength="15" name="veh" /></td>
<td><input type="submit" name="carikendaraan" value="search" /></td>
<td></td>
<td>Nama Sopir</td>
<td>:</td>
<td><input type="text" maxlength="15" name="sopir" /></td>
<td><input type="submit" name="carisopir" value="Cari" /></td>
</tr>
<tr>
<td>Waktu Berangkat</td>
<td>:</td>
<td><input type="text" name="tglb" id="datetimepicker" /></td>
<td><input type="submit" name="cariberangkat" value="Cari" /></td>
<td></td>
<td>Waktu Pulang</td>
<td>:</td>
<td><input type="text" name="tglp" id="datetimepicker2" /></td>
<td><input type="submit" name="caripulang" value="Cari" /></td>
</tr>
</table>
</form>
maybe there's a way to make it invisible by a button?
You want something like this:
// code for only hide
$('#hide_button').on('click', function() {
$('form[name="myform"]').hide();
});
Demo for hide
and for toggle the form with a single button you can try:
$('#your_button').on('click', function() {
$('form[name="myform"]').toggle();
});
Demo for toggle
According to comment
To prevent the submission:
$('form[name="myform"]').submit(function(e) {
e.preventDefault();
// Your code
});
See here for .perventDefault()

Need some resoultion. Javascript function not being called in click event in jquery

I am trying to call my function named isUrgencyTypeValid from my javascript code but it is not working. Please check what is the problem in my code.
Alert should get displayed which is not being displayed
My javascript function is not being called.
HTML Code
<td colspan="2" align="center"><input id="btnSubmit" type="submit" value="submit" runat="server"/></td></tr>
jQuery Call function
$("#btnSubmit").bind("click",function(){
alert(); // this is running
isUrgencyTypeValid();
});
javascript implemented function
function isUrgencyTypeValid()
{
alert("asd");
var i=0;
for(i=0;i<$("radio[name='urgencyType']").length;i++)
{
if($("radio[name='urgencyType']")[i].checked)
{
alert($("radio[name='urgencyType']")[i].value);
return true;
}
return false;
}
More description about my form is here
<form runat="server" name="myPage">
<table style="width: 100%;" cellpadding="5" cellspacing="5">
<caption>
Computer Support / Service Request
</caption>
<tr><td>First Name</td>
<td><input id="txtFirstName" type="text" value="First Name" runat="server"/><span class="error"></span></td></tr>
<tr>
<td>Last Name</td>
<td><input id="txtLastName" type="text" value="Last Name" runat="server"/><span class="error"></span></td></tr>
<tr>
<td>Email Address</td>
<td><input id="txtEmailAddress" type="text" value="Email Address" runat="server"/><span class="error"></span></td></tr>
<tr>
<td>Phone No</td>
<td><input id="txtPhoneNo" type="text" value="Phone No" runat="server" /><span class="error"></span></td></tr>
<tr>
<td>Do you have text messaging</td>
<td>
<span>Yes</span><input id="rdoYes" value="Yes" type="radio" runat="server"/>
<span>No</span><input id="rdoNo" value="No" type="radio" runat="server"/><span class="error"></span>
</td></tr>
<tr>
<td>Description of request*: </td>
<td><textarea id="txtDescription" cols="50" rows="10" runat="server"></textarea><span class="error"></span><span id="lengthCount"></span></td></tr>
<tr>
<td>Urgency of this support request:</td>
<td>
<input id="rdoAnyTime" name="urgencyType" value="Anytime" type="radio" runat="server"/><span>Anytime</span><br />
<input id="rdoCplDays" name="urgencyType" value="In the next couple of days" type="radio" runat="server"/><span>In the next couple of days</span><br />
<input id="rdoToday" name="urgencyType" value="Today" type="radio" runat="server"/><span>Today</span><br />
<input id="rdoUrgent" name="urgencyType" value="This is extremely urgent...I cannot wait!" type="radio" runat="server"/><span>This is extremely urgent...I cannot wait!</span><br />
<input id="rdoTalkSometime" name="urgencyType" value="Please contact me and we'll talk about it" type="radio" runat="server"/><span>Please contact me and we'll talk about it</span><br /><span class="error"></span>
</td></tr>
<tr>
<td colspan="2" align="center">Captcha To Be implemented.</td></tr>
<tr>
<td></td>
<td><input id="chkRequestCopy" type="checkbox" runat="server"/>Please send me a copy of this service request</td></tr>
<tr>
<td colspan="2" align="center"><input id="btnSubmit" type="submit" value="submit" runat="server"/></td></tr>
</table>
</form>
I copied your code and ran into the same issue. Then I looked at it closer and noticed you are missing a closing bracket in your isUrgencyTypeValid() function. The closing bracket for your for loop is missing. Firefox Error Console will also show this.
Once I added the bracket the alert worked fine.
function isUrgencyTypeValid()
{
alert("asd");
var i=0;
for(i=0;i<$("radio[name='urgencyType']").length;i++)
{
if($("radio[name='urgencyType']")[i].checked)
{
alert($("radio[name='urgencyType']")[i].value);
return true;
}
} //this was missing
return false;
}
Your radio selector is incorrect.
There's not such thing as a radio element, but there is a :radio pseudo-class selector.
Your code should read:
function isUrgencyTypeValid()
{
alert("asd");
var i=0;
for(i=0;i<$("input[name='urgencyType']:radio").length;i++)
{
if($("input[name='urgencyType']:radio")[i].checked)
{
alert($("input[name='urgencyType']:radio")[i].value);
return true;
}
return false;
}
** EDIT **
In addition to the changes above, since the alert never occurs, this indicates that isUrgencyTypeValid is not being called.
The most likely explanation is that you're not waiting until the DOM is loaded, before you bind the your handler to the click event of the button. You should have some code somewhere that looks like this:
$(function()
{
/*
* ... other code ...
*/
$("#btnSubmit").bind("click",function(){
isUrgencyTypeValid();
});
/*
* ... other code ...
*/
}

Categories