Editing Javascript to Call MySQL [duplicate] - javascript

This question already has answers here:
Can JavaScript connect with MySQL?
(16 answers)
Closed 5 years ago.
I have this function that fires when a checkbox form is submitted:
function report_build() {
var checked_count = 0;
var where = ""
$('.peer:checked').each(function (i) {
checked_count++;
var label = $(this).parent().next().html();
where += "'" + label + "', "
});
$('#tabs-1').html(where);
$('#ui-id-6').click();
}
I want to send the where variable to a PHP script to use in the where clause of a select list. What's the easiest way to do that?
Thanks!

You will have to use AJAX for that.
A simple post should do the job:
$.post('handle-sql.php', {where: where}, function(data) {
// In the handle-sql.php script, use $_POST['where'];
// Callback function: called after php script is completed
// Where 'data' is the echo'ed data from the PHP script
});

You should try to POST or GET it with an HTML <form>. They send to PHP, but otherwise javascript is run client-side.

I'm not quite sure about what you're asking but if I'm not mistaken you want to send a variable from javascript to php. You can easily do that with ajax. Here's an example;
$.ajax(
{
type: "POST",
url: "your_php_name.php",
data: {
'where' : where
},
dataType: "html",
success: function(answer)
{
//fires when php finishes it job.
}
});
And on php side, you can read the variable with $_POST['where'];

Related

how to use key/value pairs in ajax call for jquery data option

Ive been checking out how to add variables to a ajax request which I can use in my server side script. I checked this stackoverflow post here and checked the jquery api docs here for a ajax request. I am getting error variable in my code is not defined.
I have this line in my code
return $.ajax({
type: 'GET',
url: '/users/show',
data: {'currentusershow': variable},
});
I was wanting it to do something like this with the results so I can keep all my different script in the one file.
if ($.get("currentusershow")) {
// do something here
}
else if...
i am not sure how to add the value to my code?
Also my url does not work going to the show.js.erb where my code is kept.
You need to declare and assign some value to the variable before the request.
Also you need to change the method type from GET to POST.
var variable = 'some data';
/*$.ajax({
type: 'POST',
url: '/users/show',
data: {currentusershow: variable},
success: function (response) {
// Do something with respsone
},
error: function () {
alert("error");
}
});*/
$.get( "/users/show", {currentusershow: variable} )
.done(function( response ) {
//do something with the response here.
});
Mamun was kind of right here as I did not explain myself very well in my question but I thought I would post this and clarify my question with what I was trying to do. The ajax call should be
return $.ajax({
type: 'GET',
url: '/users/show',
data: { currentusershow: 'variable'},
});
where the key is currentusershow and the value variable is a string and leave out defining the variable else where in the code. That way the url comes through correctly to the server being /users/show?currentusershow=variable. And in my destination file add my ruby code there to use the variables. In my question that code was more a php type code as I did not know what I was doing at the time.

I have an input text field for mysql. So how can i output the json table from database through ajax? [duplicate]

This question already has answers here:
Using Jquery Ajax to retrieve data from Mysql
(5 answers)
Closed 6 years ago.
$.ajax({
type:'GET',
url:'index.php',
data: {val1},
dataType: 'json',
success: function(response) {
for(i=0;i<response.length;i++) {
var test = response[i];
$("#demo").append(test);
console.log(test);
//$("demo").append(test[i]);
}
console.log(response);
}
});
In ur site, if u call "index.php", you get your json array correctly?
if yes, in the "index.php" page did you set the header to output a json type text, like that
header("Content-type: application/json; charset=utf-8'");
?
Otherwise the result page will be a normal text/html page, so your val(1) wouldn't be recognized like a json string.
If this doesn't work please try to post your "index.php" page

Passing objects from tomcat Listener to Javascript function [duplicate]

This question already has answers here:
How should I use servlets and Ajax?
(7 answers)
Closed 6 years ago.
I'm trying to send objects from a Java Listener (not the Javascript eventListener!) to a Javascript application without success.
The requirement is: a Javascript application running in the browser should ask to a Listener an object (such as an array) at the launch and the Listener responses giving the array. There are many ways to do this. What are the most relatively secure and efficient ones?
I will try now to do an example. The Javascript function asks thorough jQuery the array directly a JSON file (by HTTP GET request) and storing in this way its content in the 'data' variable, as follows:
$.get("./myJSONfile.json", function( data ){
// Stuff to do
var myArray = data;
...
}
and this without asking the intervention of any Servlet or Listener. What I have to do I want to use a Listener to pass the content of the JSON file to the Javascript function?
Not sure what exactly you mean, but one usual way to communicate between javascript and server is to write an ajax call to get the data you want, just like below :
var myArray = [];
$.ajax({
url: "someUrltoServer",
async: false,
type: "GET",
error: function (jqXHR, textStatus, errorThrown) {
alert(jqXHR + "-" + textStatus + "-" + errorThrown);
},
success: function (data) {
myArray = data;
}
});

how to put variable from javascript to php variable? [duplicate]

This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 8 years ago.
This is my codes using script, its a part of my codes, how can i put the var x into my VALUES in php, in the specified 'NULL' value?
enter code here
var x = document.getElementById("name_stop");
<?php require_once('dbconnect.php'); $result2=mysql_query("INSERT INTO log (status, code)VALUES('login','NULL')"); ?>
You can't move JavaScript variables to PHP unless you refresh the page because while JavaScript is client-side, PHP is server-side. When the page is loaded, PHP's job is done.
You should have a look on how to use ajax.
Moreover, for security of your apps, always check the data which are coming from client-side.
You are looking for AJAX. You will have to send a AJAX request to your phpscript.
The easiest way to send an AJAX request is using the jQuery library.
$.ajax({
type: "POST",
url: "insert.php",
data: { name: x }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});

html form submission via an ajax request? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
jQuery AJAX submit form
I have a form on a page A, and instead of me posting to a new page, is there a way that i can do onSubmit ajax request it instead? I want to make the page to be able to load in the result into a div.
I know how to write the script for the call sort of, but i wasn't sure if i had to hand write it or it if was possible to take all the form information and send it without having to code it all into the attributes. i was thinking I could just do some sort of thing like: onClick, post form but return results into a div.
Thoughts? Ideas?
Try using JQuery's serialize (API ref here)
EDIT: something like this:
$("input.submit").click( function() {
$.ajax( {
type: "POST",
url: $("form").attr( 'action' ),
data: $("form").serialize(),
success: function( response ) {
//Code to process the response
}
});
return false;
} );
$.ajax({
url: 'mypage.html',
success: function(data){
$('.result').html(data);
},
error: function(){
alert('failure');
}
});
Then you have a div:
<div class="result"> </div>
This will automatically populate with the result of your ajax post.
http://api.jquery.com/jQuery.post/
Also see TONS of related questions: Jquery checking success of ajax post

Categories

Resources