How to connect Jquery draggable UI with mysql via AJAX - php

I would like to drag elements in my horizontal list and than save the position in mysql.
The text sort=1&sort=2.... ist just for testing purpose.
this is my code in jquery
$("#sortable").sortable({
stop: function(event, ui) {
var data = $(this).sortable('serialize', {key:"sort"});
$('#tags').text(data);
$.ajax({
data: data,
type: 'POST',
url: 'listpriority.php'
});
}
});
The sortable function is working. It serializes me the list.
Now on the server side i would like to recive the serialized data.
my php code:
<?php
print_r($_POST['sort']);
?>
But the print_r function show me only the length of the list.
the full php code should be:
<?php
include = ""; // the connection to sql
if(isset($_POST['sort'])) {
$sort = $_POST['sort'];
$sql = "UPDATE tags SET position='".$sort."'";
$result = mysqli_query($conn, $sql);
}
?>

My solution for the problem:
$( "#sortable" ).sortable({
update: function(event, ui) {
var data = $(this).sortable('serialize', { key: "sort" });
$('#tags').text(data);
$.post("listpriority.php", {data});
}
});
i changed the $.ajax to $.post and than i echo out the data in php echo($_POST['data']);

Try to get and set data from hidden field
<div class="main_div">
<div class="inner">
<span>1</span>
<input type="hidden" name="original[]" value="1">
<input type="hidden" name="new[]" class="new_ord" value="1">
</div>
<div class="inner">
<span>2</span>
<input type="hidden" name="original[]" value="2">
<input type="hidden" name="new[]" class="new_ord" value="2">
</div>
<div class="inner">
<span>3</span>
<input type="hidden" name="original[]" value="3">
<input type="hidden" name="new[]" class="new_ord" value="3">
</div>
<div class="inner">
<span>4</span>
<input type="hidden" name="original[]" value="4">
<input type="hidden" name="new[]" class="new_ord" value="4">
</div>
<div class="inner">
<span>5</span>
<input type="hidden" name="original[]" value="5">
<input type="hidden" name="new[]" class="new_ord" value="5">
</div>
</div>
And php script is
if(isset($_POST['post']) == 1) {
$original = $_POST['original'];
$new_val = $_POST['new_val'];
for ($i=0; $i < count($original); $i++) {
$sql = 'UPDATE TABLE Table_name SET order="'.$new_val[$i].'" WHERE id="'.$original[$i].'"';
echo $sql."\n\r";
}
}
See jquery code here and uncomment ajax call portion
Maybe it helps you

Related

How to make GET from "field=foo&field=bar" to "field=foo,bar"

I have a html form with many fields:
<form action="" method="get">
...
<input type="checkbox" name="price" id="price-1000" value="1000">
<input type="checkbox" name="price" id="price-2000" value="2000">
<input type="checkbox" name="price" id="price-3000" value="3000">
...
<input type="submit" value="send">
</form>
On check and send I have the following query string: ?price=1000&price=2000
How make it ?price=1000,2000 in browser URL
I think I need a rewrite rule via htaccess? Or is there some other way?
Using Jquery It will be possible.
index.php
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<form action="redex.php" method="get">
<input type="checkbox" id="price-1000" value="1000" class="ckbox">
<input type="checkbox" id="price-2000" value="2000" class="ckbox">
<input type="checkbox" id="price-3000" value="3000" class="ckbox">
<input type="hidden" name="price" id="price-1000" value="1000" class="ckvalues">
<input type="submit" value="send">
</form>
<script type="text/javascript">
$(document).ready(function() {
var favorite = [];
$('.ckbox').change(function() {
$.each($("input[type='checkbox']:checked"), function(){
favorite.push($(this).val());
});
var str1 = favorite.toString()
$('.ckvalues').val(str1)
favorite.length = 0;
});
});
</script>
redex.php
<?php
echo $_GET['price'];
?>
May this help you.

How to Post values outside the form in php

I need to post values outside the form in php , How to do it
Here is my sample code
<input type="hidden" id="test" name="test" />
<input type="hidden" id="testvalue" name="testvalue"/>
<?Php for($i=0;$i<5;$i++) { ?>
<form action='some_url'>
<input type="text" value="dynamic_value" name='testname'>
<select name="testselect">
<option value="<?php echo $dynamicvalue[$i]?>"><?php echo $dynamicvalue[$i]?></option>
</select>
<button class="btn btn-primary book_now" type="submit">BookNow</button>
</form>
<?php } ?>
i need to post all input values above the form also,since i cannot use that input field inside the loop, i want id to be unique so i cant use inside
any other option available to post all data
Use jQuery:
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script>
$(document).ready(function(){
$('form').submit(function(){
var test = $('#test').val();
var testvalue = $('#testvalue').val();
$(this).append('<input type="hidden" name="test" value="'+test+'">');
$(this).append('<input type="hidden" name="testvalue" value="'+testvalue+'">');
return true
});
});
</script>
You can add input fields into for-loop
<?php for($i=0;$i<5;$i++) { ?>
<form action='some_url'>
<input type="text" value="dynamic_value" name='testname'>
<select name="testselect">
<option value="<?php echo $dynamicvalue[$i]?>"><?php echo $dynamicvalue[$i]?></option>
</select>
<button class="btn btn-primary book_now" type="submit">BookNow</button>
<input type="hidden" name="test" />
<input type="hidden" name="testvalue"/>
</form>
<?php } ?>

jquery if checkbox within parent div is checked then include php file?

QUESTION UPDATED
pqq.php file:
I have a form like so:
<form>
{General form input here, i.e. name, address}
<php include 'form_general.php'; ?>
{Specific form input here, i.e. equipment type}
<php include 'infrastructure.php'; ?>
<div id="placeholder"></div>
<submit>
</form>
So in my infrastructure.php file I have the following checkboxes contained within a couple of parent divs like so:
infrastrucure.php file:
Plant Hire Category
<div class="option_select"><p class="select">Plant Hire</p></div>
<div class="option_underlay">
<div class="align"><p class="header">Access Platforms</p>
<div class="float_column3">
<input type="checkbox" name="option" id="option" class="group1" value="same">Option
<input type="checkbox" name="option" id="option" class="group1" value="same">Option
<br>
<input type="checkbox" name="option" id="option" class="group1" value="same">Option
<input type="checkbox" name="option" id="option" class="group1" value="same">Option
</div>
</div>
</div>
Travel Category
<div class="option_select"><p class="select">Travel</p></div>
<div class="option_underlay">
<div class="align"><p class="header">Planes</p>
<div class="float_column3">
<input type="checkbox" name="option" id="option" class="group2" value="same">Option
<input type="checkbox" name="option" id="option" class="group2" value="same">Option
<br>
<input type="checkbox" name="option" id="option" class="group2" value="same">Option
<input type="checkbox" name="option" id="option" class="group2" value="same">Option
</div>
</div>
</div>
JQUERY in my pqq.php file:
<script>
$(document).ready(function () {
$('input[name=option]').change(function () {
if($(this).hasClass('group1')) {
$('#placeholder').load("safety.php");
} else if($(this).hasClass('group2')) {
$('#placeholder').load("travel.php");
}
});
});
</script>
what I am trying to do is if a user checks any checkbox from the Plant Hire category then it includes my safety.php file in the form. Otherwise if a user selects any checkbox from my travel category then my travel.php file is included in the form?
Does anyone know a way I can do this using jquery or other methods?
Thanks,
First make div, where the loaded php file will be stored, in my code it would be <div id="placeholder"></div>, then for each group of checkboxes use the same class, in my code it's class group-1 and class group-2. Then use something like this:
$(document).ready(function () {
$('input[name=option]').change(function () {
if ($(this).hasClass('group1')) {
var length = $('.group1:checked').length;
if (length) {
$('#placeholder').load("safety.php");
} else {
$('#placeholder').html("");
}
} else if ($(this).hasClass('group2')) {
var length = $('.group2:checked').length;
if (length) {
$('#placeholder').load("travel.php");
} else {
$('#placeholder').html("");
}
}
});
});
Can more checkboxes be selected? Or only one? If only one, you should probably uncheck all other checkboxes in the code.
Also you have in your sample code multiple same IDs, which is not a good practice.

Modal form Submission to PHP using Jquery .ajax

I am trying to post a modal form to a table using php, jquery .ajax but it never works.. tried debugging using firebug and i don't see any errors. i tested the form by using form action="notes_functions.php" and it worked fine.
Profile.php
<div class="modal-body">
<form class="noteform" id="notesmodal" method="post">
<fieldset>
<label>Title</label>
<input type="text" class="form-control" name="note_title" placeholder="Enter a title for your note">
<label>Note</label>
<textarea rows="4" cols="50" class="form-control" name="note_content" placeholder="note"></textarea>
<label>Note type</label>
<div class="panel-body">
<input type="tagsinput" id="teetete" class="tagsinput" value="" />
</div>
<label for="exampleInputFile">Attach a document</label>
<input type="file" id="exampleInputFile3">
<p class="help-block">PDF, DOCX and image files are supported.</p>
<div class="checkbox">
<label>
<input type="checkbox"> Check me out
<input type="label" name="note_account" value="<?php echo $acctname ?>"/>
</label>
</div>
<input type="hidden" name="note_creator" value="<?php echo $_SESSION['username'];?>"/>
</fieldset>
<button class="btn btn-default" id="submitnote" >ADD</button>
</form>
</div>
this is my js code
$(function(){
$("button#submitnote").click(function(){
$.ajax ({
type:"POST",
url:"notes_functions.php",
data: $('form.noteform').serialize(),
success: function(msg){
$("#thanks").html(msg)
$("form.noteform").modal('hide');
},
error: function(){
alert("failure");
}
});
});
});
notes_functions.php
<?php
include_once 'dbconnect.php';
if (isset($_POST['note_title'])) {
$notetitle = strip_tags($_POST['note_title']);
$noteContent = strip_tags($_POST['note_content']);
$noteAccount = strip_tags($_POST['note_account']);
$noteCreator = strip_tags($_POST['note_creator']);
mysql_query("INSERT INTO account_notes (note_title, note_contents, note_account, note_creator)
VALUES ('$notetitle','$noteContent', '$noteAccount', '$noteCreator') ");
echo "Name = ".$notetitle;
echo $noteCreator;
}
?>
You should really be using .submit() rather than click (for submit-by-enter, etc.) and returning false to block conventional submits. You also need to make sure the code to bind the events runs after the form element is created. The easiest way to do this is to put it in the document ready handler.
jQuery(document).ready(function ($) {
$("#notesmodal").submit(function () {
$.ajax({
type: "POST",
url: "notes_functions.php",
data: $('form.noteform').serialize(),
success: function (msg) {
$("#thanks").html(msg)
$("form.noteform").modal('hide');
},
error: function () {
alert("failure");
}
});
return false;
});
});
And change the ADD button to be:
<input type="submit" name="submit" class="btn btn-default" id="submitnote" value="ADD" />

Form submit button not passing name to php

Probably a very simple answer but one I am struggling with. I have a basic form which has a standard submit button. When this is clicked, it is being valued by jquery serialize and then Ajax to process php page etc. I am confused as to why the name of my submit button 'submit' is not being posted when a user clicks on it. The only reason I can think of, is that serialize only handles text,text area and select elements. If so, how can I send the name through to php for processing. I have included some sample code and would be grateful if someone could point me in the right direction. Thanks
html form
<!--- Form to add box -->
<div id="boxaddform" style="display:none;">
<div class="dialogTop_padd"></div>
<form id="BA_boxform" name="BA_boxform" method="post">
<fieldset>
<legend><span></span>Select Company</legend>
<div class="spacer"></div>
<div class="formMessage">Click again to open</div>
<div class="fld_fld">
<div>
<label for="BA_customer">Company:</label><br />
<select name="BA_customer" id="BA_customer">
<option SELECTED VALUE="">Select a Company</option>
<?php
do {
?>
<option value="<?php echo $row_Recordsetcust['customer']?>"><?php echo $row_Recordsetcust['customer']?></option>
<?php
}
while ($row_Recordsetcust = mysql_fetch_assoc($Recordsetcust));
$rows = mysql_num_rows($Recordsetcust);
if($rows > 0)
{
mysql_data_seek($Recordsetcust, 0);
$row_Recordsetcust = mysql_fetch_assoc($Recordsetcust);
}
?>
</select>
<div class="spacer"></div>
<!--- displays the address and dept from the change function -->
<div id="BA_dept"></div>
<br />
<div id="BA_address"></div>
</div>
</fieldset>
<div class="dialogTop_padd"></div>
<!--- fieldset for service level -->
<fieldset>
<legend>Service Level</legend>
<div class="spacer"></div>
<div>
<label for="BA_service">Service level:</label>
<select name="BA_service" id="BA_service">
<option SELECTED VALUE="">Select an option</option>
<option value="Standard">Standard</option>
<option value="Rapid">Rapid</option>
</select><br />
</div>
</fieldset>
<div class="dialogTop_padd"></div>
<!--- fieldset for box # -->
<fieldset>
<legend>Box Details</legend>
<div class="spacer"></div>
<div>
<label for="BA_box">Box#:</label><br />
<input id="BA_box" name="BA_box" type="text" size="32" maxlength="128" /><br />
</div>
<div>
<label for="BA_destdate">Destroy date:</label>
<input id="BA_destdate" name="BA_destdate" type="text" size="32" maxlength="128" value = "" /><br />
</div>
</fieldset>
<div class="dialogTop_padd"></div>
<!--- fieldset for authorisation -->
<fieldset>
<legend>Authorisation</legend>
<div class="spacer"></div>
<div>
<label for="BA_authorised">Requested By:</label>
<input id="BA_authorised" name="BA_authorised" type="text" value="<?php echo $_SESSION['kt_name_usr']; ?>"><br />
</div>
</fieldset>
<!--- div to show callback result from ajax via dialog -->
<br />
<div id="BA_addbox"></div>
<br />
<input class="AddBoxSubmitButton" type="submit" id="submit" name="submit" value="Add Box" />
<input class="AddBoxResetButton" type="reset" name="cancel" value="Clear Form" />
<!--- buttons to submit form and reset form to default status -->
<!-- <button id="BA_submit" class="submitbutton icon-right ui-state-default2 ui-corner-all"><span class="ui-icon ui-icon-circle-plus"></span>Add Box</button>
<button type="reset" id="BA_reset" class="resetbutton icon-right ui-state-default2 ui-corner-all"><span class="ui-icon ui-icon-circle-plus"></span>Reset</button>
--><br />
</form>
<br />
</div>
jquery code
$(function(){
$("#BA_boxform").submit(function(){
var formdata = $('#BA_boxform').serialize();
alert(formdata);
$.ajax({
type: "POST",
url: "/domain/admin/requests/boxes/boxesadd.php",
data: formdata,
dataType: 'json',
success: function(msg){
//$("#confirm_department").hide();
/*
var $dialog = $('<div id="dialog"></div>')
.html('Your intake was successfully submitted and will be viewable in the reporting area.<br /><br />Thank you.');
$dialog.dialog({
autoOpen: true,
modal: true,
title: 'Box intake submission successfull',
width: 400,
height: 200,
draggable: false,
resizable: false,
buttons: {
Close: function() {
$( this ).dialog( "close" );
}
}
});
*/
//alert('You have succesfully submitted your ' + msg.company + ' report. Thank you.');
//console.log(msg);
$("#BA_addbox").show();
//$("#formImage .col_1 li").show();
$("#BA_boxform").get(0).reset();
//$("#boxaddform").hide();
}
});
return false;
});
});
// End function to submit box intake form
cutdown php code for illustration
if (isset($_POST['submit'])) { }
jQuery won't serialize submit button:
Docs:
Note: Only "successful controls" are serialized to the string. No
submit button value is serialized since the form was not submitted
using a button.
Workaround is to check if $_POST is not empty:
if (!empty($_POST)) {
// Your code here
}
or check if specific input was set. E.g.
if (isset($_POST['BA_customer'])) {
}
Like said, submit buttons aren't serialized. A possible workaround: { not tested but as i understand it, should work }
$('#BA_boxform input:submit').on('click', function () {
var formdata = $('#BA_boxform').serialize() + '&submit=' + $(this).val();
//....
return false;
});

Categories