jquery variable in php echo - php

I have this jquery script (slider):
<script>
$( function() {
$( "#slider-range-max" ).slider({
range: "max",
min: 1,
max: 25,
value: 1,
slide: function(event, ui) {
$("#amount").val(ui.value);
}
});
$( "#amount" ).val( $("#slider-range-max").slider("value") );
});
</script>
This is my html outout:
<input type="text" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;">
<div id="slider-range-max"></div>
I want the value of my slider stored as a php variable.

To call upon a php code that might have some logic in it, you need to have your html and javascript in a file, then your php code in a different file, like so :
html_file.php (contains only html template and javascript).
html part:
<input type="text" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;">
<div id="slider-range-max"></div>
Javascript part:
<script>
$( function() {
$( "#slider-range-max" ).slider({
range: "max",
min: 1,
max: 25,
value: 1,
slide: function( event, ui ) {
$( "#amount" ).val( ui.value );
},
change: function(event, ui) {
$.post('php_file.php', {slider_val: ui.value}).done(
function(result) {
//your php code sends result back here.
//do something with result.
console.log(result);
}
);
}
});
$( "#amount" ).val( $( "#slider-range-max" ).slider( "value" ) );
});
</script>
php_file.php (contains the php code and logic).
you then retrieve the value in your php code like so:
<?php
//$slider_val contains the value of your slider
$slider_val = $_POST['slider_val'];
//you can output the value back to your javascript like so
echo $slider_val;

You can send the input value to php like this
HTML
<input type="text" placeholder="Amount" onkeyup="send(this.value)">
SCRIPT
<script>
function send(str) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("value").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "send.php?value=" + str, true);
xmlhttp.send();
}
</script>
PHP
$value = $_REQUEST["value"];
echo $value;
Try this. Hope it helps to solve your problem.

Related

Pass php variable to window.open in ui-dialog

I am using a ui-dialog and want to pass a php variable to a url if the Delete button is clicked. I have looked at a large number of other answers to similar questions but nothing seems to quite fit what I am trying to do.
The code below works correctly for the link shown, but doesn't work when I try to pass the php variable to the url.
This code works fine
$( function() {
$( "#dialog-message" ).dialog({
modal: true,
buttons: {
"Delete": function() {
window.open('https://example.com/page.php?id=');
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});
I have tried using this but it doesn't work
window.open('https://example.com/page.php?id=<?php echo $id?>')
Try add ";" at the end.
<?php echo $id;?>
Assuming your above code is in .php file, try below code:
<script>
$( function() {
$( "#dialog-message" ).dialog({
modal: true,
buttons: {
"Delete": function() {
window.open("https://example.com/page.php?id=<?php echo $id; ?>");
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});
</script>
You can assign pageid in input hidden fields like below.
After you need to get this fields value using val() function.
<input type="hidden" value="<?php $id; ?>" id="pageId" />
$( function() {
var PageId = $('#pageId').val();
$( "#dialog-message" ).dialog({
modal: true,
buttons: {
"Delete": function() {
window.open('https://example.com/page.php?id='+PageId);
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});

how to use select method with auto complete?

code:
<script>
$(function() {
$( "#college_name" ).autocomplete({
source: 'search.php',
minLength:2,
select: function(event, ui) {
var x = ui.item.value;
$("#college_name").attr('value',x);
}
});
});
</script>
<script>
$(document).ready(function(){
$("#college_name").select(function(){
alert("hi");
});
});
</script>
html code:
<input type="text" id = "college_name" name = "college_name" value="" placeholder = "College Name" autocomplete="off">
<input type='text' name='college_id' id='college_id'>
In this code when I wrote any college name in college_name input field autocomplete search i.e. search.php are working all college are showing. But when I select any college from autocomplete it does not show any message like (hi) or any thing. So, how can I fix this problem.
Thank You
Add alert message in Select event of autocomplete.
<script>
$(function() {
$( "#college_name" ).autocomplete({
source: 'search.php',
minLength:2,
select: function(event, ui) {
var x = ui.item.value;
$("#college_name").attr('value',x);
alert("hi");
}
});
});
</script>

how to get string data from jquery autocomplete box in php using mysql

<script>
$(document).ready(function(){
var spinner = $( "#spinner" ).spinner();
$('#spinner').autocomplete({
source:'name.php'
});
$( "#getvalue" ).on( "click", function() {
alert( spinner.spinner( "value" ) );
});
});
</script>
<body>
<label for="spinner">To search name type here:=</label>
<input id="spinner" name="value"/>
<p>
<button id="getvalue">Get value</button>
</p>
</body>[1]
Above is the snapshot of the output and i want to get the particular value from this dropdown on get value button clicked and above is jQuery code and html code.
Value from the spinner field can be extracted like this:
$( "#getvalue" ).on( "click", function() {
.
.
value = $("#spinner").val();
alert(value);
});

JQUERY autocomplete get id instead of value

I have a mysql database where I would like to use the autocomplete one.
So far I am able to receive the results of the query.
For example this is the result: [{"LNAME":"Napoleon","id":"1"}]
But I want the id of the customer in this case to be filled in another input field of my form. (so in Napoleons case I want the "1" to be filled in to my clienteID field of the form)
At the moment the field does not get updated.
Here's the HTML part:
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.autocomplete.js"></script>
<script>
$(document).ready(function(){
$("#tag").autocomplete("autocomplete.php", {
minLength: 3,
select: function(event, ui) {
$('#clienteId').val(ui.item.id);
},
});
});
</script>
<form name='form_update_details' id='form_update_details' method='post' action=''>
<input type='text' name='clienteName' id='tag'>
<input type='text' name='clienteId' id='clienteId'>
</form>
Then the php part:
<?php
include("include/db.php");
$q=$_GET['q'];
$my_data=mysql_real_escape_string($q);
$sql="SELECT ID, CONTACTS_LNAME, CONTACTS_FNAME FROM CRM_CONTACTS WHERE CONTACTS_LNAME LIKE '%$my_data%' or CONTACTS_FNAME LIKE '%$my_data%' ORDER BY CONTACTS_LNAME";
$result = mysqli_query($coni,$sql) or die(mysqli_error());
if($result)
{
while($row=mysqli_fetch_array($result))
{
$data[] = array(
'LNAME' => $row['CONTACTS_LNAME'],
'id' => $row['ID']
);
}
echo json_encode($data);
flush();
}
?>
Try with a sample data:use label and value as keys instead of LNAME and id. Use event.preventDefault(); to set the label in #tag field otherwise by default it will set the value.
var data = [
{
value: "1",
label: "jQuery"
},
{
value: "2",
label: "jQuery UI"
},
{
value: "3",
label: "Sizzle JS"
}
];
$(document).ready(function(){
$("#tag").autocomplete( {
source: data,
minLength: 3,
select: function(event, ui) {
event.preventDefault();
$("#tag").val(ui.item.label);
$('#clienteId').val(ui.item.value);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<form name='form_update_details' id='form_update_details' method='post' action=''>
<input type='text' name='clienteName' id='tag'>
<input type='text' name='clienteId' id='clienteId'>
</form>
Try this:
$( function() {
var projects = [
{
value: "1",
label: "jQuery"
},
{
value: "2",
label: "jQuery UI"
},
{
value: "3",
label: "Sizzle JS"
}
];
$( "#project" ).autocomplete({
minLength: 0,
source: projects,
focus: function( event, ui ) {
$( "#project" ).val( ui.item.label );
return false;
},
select: function( event, ui ) {
$( "#project" ).val( ui.item.label );
$( "#project-id" ).val( ui.item.value );
return false;
}
})
.autocomplete( "instance" )._renderItem = function( ul, item ) {
return $( "<li>" )
.append( "<div>" + item.label + "</div>" )
.appendTo( ul );
};
});
Html:
<div id="project-label">Select a project (type "j" for a start):</div>
<input id="project">
<input type="text" id="project-id">
Demo Fiddle

.submit() without reloading page

I am trying do some file upload using jquery. The upload box have to be in the dialog box.
Now I just want to know after I click on the dialog's upload button. How can I submit yet the page do not load.
I tried using this
$('#uploadForm').submit().preventDefault();
It has not action at all.
html
<script>
$( "#dialog-upload" ).dialog({
autoOpen: false,
height: 200,
width: 350,
modal: true,
buttons: {
"Upload": function() {
var bValid = true;
bValid = bValid && checkCSV()
if(bValid){
**$('#uploadForm').submit().preventDefault();**
}
$( "#dialog-upload" ).dialog( "close" );
},
Cancel: function() {
allFields.val( "" ).removeClass( "ui-state-error" );
$( this ).dialog( "close" );
}
},
close: function() {
allFields.val( "" ).removeClass( "ui-state-error" );
$( this ).dialog( "close" );
}
});
</script>
<div id="dialog-upload" title="Upload csv file">
<form>
<fieldset>
<form action='ajax.php' id = 'uploadForm' method='post' enctype='multipart/form-data'>
<input type="text" name="file" id="fileUpload" size = 30 class="text ui-corner-all" />
</form>
</fieldset>
</form>
</div>
php
if (isset($_FILES['file'])) {
echo "success";
}
Use the EVENT delegation for your form submit
$('#uploadForm').submit(function( e ){
e.preventDefault(); // browser 'submit' event behavior prevented
});
Than do:
"Upload": function() {
var bValid = true;
bValid = bValid && checkCSV()
if(bValid){
$('#uploadForm').submit();
}
// ......
You can bind something to the submit event prior to submitting.
$(function() {
$('#uploadForm').submit(function(evt) {
evt.preventDefault();
});
});
Then just call .submit() without the preventDefault() in your original location.

Categories