jquery .submit() not passing value to action page - php

i am trying to load the content of the php page in the div "content", using .submit in jquery. i am facing a problem that when i press the submit btn the values dont get passed to the action page. here is the form code
<form id="f1" action="attandace/students/take_attandance.php" method="post" enctype="multipart/form-data" >
<table>
<tr>
<td><label id="sb">Subject Name</label></td>
<td>
<select id="s_b" name="subject_name">
<option value="0">Select</option>
<?php echo $option; ?>
</select>
</td>
<td><label id="cn">Class Name</label></td>
<td>
<select id="c_n" name="class_name">
<option value="0">Select</option>
<?php echo $option2; ?>
</select>
</td>
</tr>
<tr>
<td colspan="4"><div align="center"><input id="btn" type="submit" name="tk" value="Take Attandance" />
</div></td>
</tr>
</table>
</form>
jquery script:
$(document).ready(function(e)
{
$('form').submit(function(e)
{
e.preventDefault();
$('#content').load($(this).attr('action'));
});
});

You need to pass the values along using the data argument.
You can make use of .serializeArray() for it.
$(document).ready(function (e) {
$('form').submit(function (e) {
var $form = $(this);
e.preventDefault();
$('#content').load($form.attr('action'), $form.serializeArray());
});
});

Related

Is it possible to use post and get function in one php form?

<html>
<body>
<div class="content">
<fieldset>
<div>
<h1>Student Details</h1>
</div>
<form method="">
<table>
<tr>
<td><label>Student ID:</label></td>
<td><input type="text" name="StuID"></td>
<td><input type="submit" name="find" value="Find"></td>
</tr>
<tr><td></td></tr>
<tr>
<td><label>Student IC:</label></td>
<td><input type="text" name="StuIC"></td>
</tr>
<tr>
<td><label>School ID:</label></td>
<td><input type="text" name="SchID"></td>
</tr>
<tr>
<td><label>Student Name:</label></td>
<td><input type="text" name="StuNAME"></td>
</tr>
<tr>
<tr>
<td><label>Gender:</label></td>
<td>
<select name="Gender">
<option>--Gender--</option>
<option>Male</option>
<option>Female</option>
</select>
</td>
</tr>
<tr>
<td><label>Nationality:</label></td>
<td><input type="text" name="Nationality"></td>
</tr>
<tr>
<td><label>Race:</label></td>
<td>
<select name="Race">
<option>--Race--</option>
<option>Malay</option>
<option>Indian</option>
<option>Chinese</option>
<option>Other</option>
</select>
</td>
</tr>
<tr>
<td><label>Religion:</label></td>
<td>
<select name="Religion">
<option>--Religion--</option>
<option>Muslim</option>
<option>Buddist</option>
<option>Hindu</option>
<option>Christian</option>
<option>Other</option>
</select>
</td>
</tr>
<tr>
<td><label>Telephone Number:</label></td>
<td><input type="text" name="TelNo"></td>
</tr>
</tr>
<tr>
<td><input type="submit" name="add" value="Add"></td>
<td><input type="submit" name="update" value="Update"></td>
<td><input type="submit" name="delete" value="Delete"></td>
</tr>
</table>
</form>
</fieldset>
</div>
</body>
</html>
This is my form, the user can find the data by insert the ID and click "Find" and the data will display in the form. With the same form, user can update the data with just change data in the form and click "Update" and the data will update in database.
Is it possible for me to build a form that can do Insert, Update and Delete using just one php form.
You can create one form, and with JS/jQuery (or any other lib) submit that form via Ajax depending on button clicked:
<form id="my-form">
...
<tr>
<td><label>Student ID:</label></td>
<td><input type="text" name="StuID" id="stu-id"></td>
<td><input type="submit" name="find" value="Find" onClick="sendPost()"></td>
</tr>
<tr>
<td><input type="button" name="add" value="Add" onClick="sendPost()"></td>
<td><input type="button" name="update" value="Update" onClick="sendPut()"></td>
<td><input type="button" name="delete" value="Delete" onClick="sendDelete()"></td>
</tr>
</form>
function sendFind() {
$.ajax({
url: 'example.com/my-action?StuID=' + $('#stu-id').val(),
method: 'GET',
success: function () {}
...
});
}
function sendPut() {
$.ajax({
url: 'example.com/my-action',
method: 'PUT',
dataType: 'json',
data: $("#my-form").serialize(),
success: function () {}
...
});
}
function sendDelete() {
$.ajax({
url: 'example.com/my-action',
method: 'DELETE',
dataType: 'json',
data: $("#my-form").serialize(),
success: function () {}
...
});
}
As you can see there even can be same URL and act differently depending on request type.
Its not possible though you can use $_REQUEST in php to get any posted data whether with POST or GET.
For your case you can name your input type submit with different names then check in your php
<?php
if(isset($_POST['insert'])){ //insert functionality }
if(isset($_POST['update'])){ //update functionality }
if(isset($_POST['delete'])){ //delete functionality }
?>

jquery-select for additional rows

I am trying to use jquery-select2 in my select dropdown form. The form is in a table row and I can add new rows with the Add Button. When I click the Add Button it will make more rows for me and I have data that is generated from the database.
The problem is that the jquery-select2 only works for the select dropdown in the first row, but it does not function for the new rows I create. I have tried the $('#idone').select2(); , but it didn’t work. Below is my select form and code.
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<div class="container">
<div class="row">
<form class="col-md-12">
<table class="table table-bordered" width="100%" id="dataTable" cellspacing="0">
<tr>
<td colspan="4" align="left"><button id="alertred" type="button" class="btn btn-sm btn-danger btn-flat pull-left delete_row" >Delete Row</button></td>
<td colspan="3" align="right"><button id="button2" type="button" class="btn btn-sm btn-success btn-flat pull-right add_row"> Add Row</button></td>
</tr>
<tr>
<td ></td>
<td>Dropdown One</td>
<td>Dropdown Two</td>
<td>details</td>
<td>Date</td>
</tr>
<tbody>
<tr valign="baseline" id="main_tr" class="main_tr" >
<td><input type="checkbox" name="deleterow" class="deleterow" ></td>
<td><select name="idone[]" class="onesub form-control" id="idone" required>
<option value="0">Select Account</option>
<option value="1">Laravel</option>
<option value="2">Jquery</option>
<option value="3">PHP</option>
<option value="4">React</option>
<option value="5">Jquery ui</option>
<option value="6">Android</option>
</select>
<select name="subone[]" class="suboneid" type="hidden"><option></option></select>
</td>
<td><select name="idtwo[]" class="twosub form-control" id="idtwo" required>
<option value="0">Select Account</option>
<option value="1">Laravel</option>
<option value="2">Jquery</option>
<option value="3">PHP</option>
<option value="4">React</option>
<option value="5">Jquery ui</option>
<option value="6">Android</option>
</select>
</td>
<td><input type="text" name="details[]" id="details" class="form-control"></td>
<td><input type="date" name="dateofentry[]" class="form-control"></td>
</tr>
<tr >
<td colspan="7" align="center"><input type="submit" id="submit" class="btn btn-info" value="Submit"></td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
Below is the script to add more rows
<script type="text/javascript">
$(function() {
$(".add_row").click(function(){
var main_tr = $('#main_tr').clone();
var ml = $(".main_tr").length;
main_tr.find("label").remove();
main_tr.find("input").val("");
main_tr.find("select").val("");
main_tr.find("#idone").attr("id","idone"+ml );
main_tr.find("#idtwo").attr("id","idtwo"+ml );
main_tr.find("#details").attr("id","details"+ml );
main_tr.find("#date").attr("id","date"+ml );
$(main_tr).insertAfter('.main_tr:last');
});
$(".delete_row").click(function(){
$(".deleterow:checked").each(function () {
$(this).parents('.main_tr').remove();
});
});
});
$(function() {
$( "#datepicker" ).datepicker({
changeMonth: true,
changeYear: true,
});
});
$(function() {
$('#idone').select2();
$('#idtwo').select2();
});
$(document).ready(function()
{
$("body").on("change",".onesub",function(){
var headid=$(this).val();
var that = $(this);
var post_id = 'headid='+ headid;
$.ajax
({
type: "POST",
url: "ajaxpostone.php",
data: post_id,
cache: false,
success: function(suboneid)
{
$(that).parents("tr").find(".suboneid").html(suboneid);
}
});
});
});
</script>
How do I make this jquery-select2 work for the select dropdown in first row and every other new row I add to my form table.

How to use jquery function and isset() together via a submit button?

First I have a row and a submit button on a table like this:
<tr id="date">
<td class="left" valign="top">
<label>Some content: </label>
</td>
<td class="right">
<? if ($akis_kademe == "ekle") { ?>
<input type="submit" name="submit5" class="btn" value="Tarih ekle" />
<? } ?>
</td>
</tr>
And isset method...
if(isset($submit5)){
?>
<tr id="ek_takvim">
<td class="left" valign="top">
</td>
<td class="right">
</td>
</tr>
<? }?>
Jquery function...
$(document).ready(function(){
$("submit5").click(function(){
$(ek_takvim).hide();
});
});
All I wanna do that when I click the button I wanna add isset section on my table and also hide the button at the same time.
Try the following
<?php $takvimDisplay = isset($submit5)?"block":"none"; ?>
<tr id="ek_takvim" style="display:<?php echo $takvimDisplay ?>">
<td class="left" valign="top">
</td>
<td class="right">
</td>
</tr>
$(document).ready(function(){
$("input[name=submit5]‌").click(function(){
$("#ek_takvim").show();
$(this).hide();
});
});
I assume you add the row ek_takvim right after the button row and hide them by css or onLoad-Event. Then this will do the rest of the work.
$(document).ready(function(){
$("input[name=submit5]‌").click(function(){
$("#ek_takvim").show();
$(this).hide();
});
});
I like this approach more:
HTML
<input type="submit" name="submit5" onclick="goodFunctionName()" class="btn" value="Tarih ekle" />
JS
function goodFunctionName(){
$("#ek_takvim").show();
$("input[name=submit5]").hide();
}
JsFiddle: https://jsfiddle.net/c0f7bn96/
Talk to me: https://jsfiddle.net/c0f7bn96/#&togetherjs=5BxtqPPlE1

Unable to post values on a page using jquery ajax post method in wordpress custom plugin

I am new to wordpress and i have just created my first ever plugin. This plugin contains a form. Whenever i make jQuery AJAX Post Request to submit the form, i get this error in alert box.
Call to undefined function add_shortcode() in <b>J:\xampp\htdocs\wordpress\wp-content\plugins\auc_result_fetcher\includes\search.php</b> on line <b>22</b><br />
I am using Wordpress 3.8.1. The add_shortcode() function successfully displays the form wherever i want to be displayed. But whenever i make jQuery AJAX Post Request, i get the above error message.
EDIT
My Code Snippet.
add_action('init', function() {
add_shortcode('search_form', 'print_search_form');
});
add_action('init', 'fetch_grades');
add_action('init', 'fetch_search_options');
function print_search_form(){
?>
<div class="auc-search-form">
<div id="error">
<div id="select-class-error"></div>
<div id="select-search-by-error"></div>
<div id="search-field-error"></div>
</div>
<form id="searchForm" method="POST" action="">
<table>
<tr>
<td style="text-align:right;"> <span>Class:</span> </td>
<td><select id="select-class" name="search-class">
<option>Select Class</option>
<?php
$grades = fetch_grades();
foreach($grades AS $v){
echo "<option>".$v->grade_title."</option>";
}
?>
</select>
</td>
<td id="class-select"></td>
</tr>
<tr id="previous-row">
<td style="text-align:right;"> <span>Search By:</span> </td>
<td>
<select id="search-by" name="search-by">
<option>Select Choice</option>
<?php
$grades = fetch_search_options();
foreach($grades AS $v){
echo "<option>".$v->search_title."</option>";
}
?>
</select>
</td>
</tr>
<tr id="search-row">
<td id="search-by-option-label" style="text-align:right;"><span></span></td>
<td id="search-by-field"><input type="text" name="searchBy" id="search" /></td>
</tr>
<tr >
<td ></td>
<td>
<input type="hidden" id="url" name="url" value="<?php echo SEARCH_RESULTS; ?>">
<input type="button" id="search-button" value="Search" />
<input type="reset" name="searchBy" id="reset" value="Cancel"/>
</td>
</tr>
</table>
</form>
</div>
<?php
}
Please help me out. Thanks.

Getting input of form for usage in php page

I'm using the below jquery code for creating new entry in my form upon click on a button.
<script type="text/javascript">
$(document).ready(function() {
$('#btnAdd').click(function() {
var num = $('.clonedInput').length;
var newNum = new Number(num + 1);
var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);
newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);
$('#input' + num).after(newElem);
$('#btnDel').attr('disabled','');
if (newNum == 3)
$('#btnAdd').attr('disabled','disabled');
});
$('#btnDel').click(function() {
var num = $('.clonedInput').length;
$('#input' + num).remove();
$('#btnAdd').attr('disabled','');
if (num-1 == 1)
$('#btnDel').attr('disabled','disabled');
});
$('#btnDel').attr('disabled','disabled');
});
</script>
my html code is
<form id="entry" method="post" action="collect.php">
<div id="input1" style="border: solid 2px #000; width: 80%; margin-bottom:4px;" class="clonedInput">
<table>
<tr>
<td><img src="arrow.gif"/></td>
<td>
<table>
<tr>
<td><b>I/P: </b></td>
<td><input type="text" name="ipname" id="name1" /></td>
<td><b>Console I/P: </b></td>
<td><input type="text" name="consolename" id="name1" /></td>
<td><b>Console Port: </b></td>
<td><input type="text" name="portname" id="name1" /></td>
<td><b>Rack Number: </b></td>
<td><input type="text" name="rackname" id="name1" /></td>
</tr>
<tr>
<td><b>Serial Number: </b></td>
<td><input type="text" name="serialname" id="name1" /></td>
<td><b>Login Machine: (must get dropdown from db)</b></td>
<td><select name="logmacname">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select></td>
<td><b>Machine Username: </b></td>
<td><select name="usermac" >
<option value="">Select:</option>
<option value="cisco">cisco</option>
<option value="lab">lab</option>
</select></td>
<td><b>Machine Password: </b></td>
<td><select name="macpass" >
<option value="">Select:</option>
<option value="cisco">cisco</option>
<option value="lab">lab</option>
</select></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<div>
<input type="button" id="btnAdd" value="add another entry" />
<input type="button" id="btnDel" value="remove entry" />
</div>
<input type="submit" value="Click to submit!">
</form>
I now need to send this form to my collect.php for db storage. I know I can access the form values using $_POST and use isset() function for checking them but how can I access each entry from the form and use it. Only my div is getting updated with new names but input fields have same name.
Can someone help me over come this problem.
First of all you can only have one unique ID per element, while you have id="name1" for all elements, so either change the ID for each element, or remove it or change it to class="name1".
In your example there is no form tag, i don't know if it is because you omitted it from your example or because it does not exist, anyway you need a form tag around the wrapping div:
<form action="collect.php" method="post">
...
</form>
You also need a submit button:
<input type="submit" value="Submit"/>
and then once the submit button is clicked , your collect.php will be automatically called and then you can access the parameters from the post array:
$ipName = $_POST['ipname'];
$consolename = $_POST['consolename'];
...

Categories