I have to do a project as exam and I'm getting really troubled with this topic. My project is about a photoblog like Instagram. As in Instagram I want to get values from my DB and posting after the page has been loaded. So I've understood I need help from AJAX but every example I've found is about using PHP and I can't for my projects. Tools I can use are: HTML, CSS, XML, JQuery, AJAX, Servlet/JSP, DOM and JSON. I've found examples in PHP and I've tried to "translate" into Servlet/JSP but it (obviously) didn't work. This is my idea:
$.ajax({ type: "GET",
url: "Jspfile",
success : function()
{
// Here I would like to use the return value from db that I got by a servlet/jsp file
}
});
I'm using MySQL aS DB
You'll have to connect your Java applet to the database, then serialize the data from whatever object type your DB driver uses to JSON.
You then have to render the JSON in your "Jspfile" URL. Then, you just have to parse the JSON in your success / complete function in the jQuery request.
Related
I want to show some informations on site from database. But I know there is no way to connect to database with native javascript. And I read something about this is not a good method.
So I really curious about how developers show informations on sites from database? For example member name? It's in somewhere in the database. Site is coded with javascript. How can javascript reach member name info?
They use AJAX to send request to the back-end.
For example, javascript may ask special php script to return mebmer's name by id.
$.get( "sitename.com/script.php", {id: 125},
function( data ) {
alert( "The name is "+data );
});
You're looking for a scripting language. There is PHP, C#, ColdFusion, and many more options. You will probably want to use PHP because it's free, and it is included on most hosting services, but check with your host to see what they support.
You are correct connecting to a database from client-side JavaScript (I say client-side JavaScript because we now have node.js that is server-sider JavaScript) is a bad idea because you would need to send (expose) your connection information to the client; e.g. the username, password, database name, and DBMS engine would all be exposed to anyone that visited your site.
Most people that fetch information from a database in JavaScript code do so by sending AJAX (asynchronous GET, POST, etc. HTTP requests) to some server-side code that handles CRUD with the database. You can expose server-side code as a web-service, API, or have POST/GET methods in your server-side project that return information (usually in JSON or XML).
You can use an ajax call to a file written on php for example that exist on you server, for example look at the following code
<script src="jquery-1.11.1.min.js"></script>
<script>
//send query to bbdd (information is in the input with id="information")
//and sended when pressed the button with id="btn-send"
$(function(){
$("#btn-send").click(function() {
var url = "file_that_search.php";
$.ajax({
type: "POST",
url: url,
data: $("#information").serialize(),
success: function(data){
$("#html").html(data);//paste the data to the browser
}
})
return false;
})
})
</script>
then the file "file_that_search.php" has the retrieved data from the BBDD making something like
$search= "ID=" . $_POST['consult'];
$query = "SELECT * FROM database WHERE ".$search;
I've been stumped on this for a while. I've successfully created a MediaWiki API extension from which I'm able to extract data using an API url, but now I want to go the other way. I want to use JS to send a simple bit of data to the server for storage in a session variable (in PHP). I've experimented with something like the following:
$.ajax({
// start POST request
type: "POST",
// url to which the request is sent
url: "/",
// data to the server
data: { myvariable: 0 },
// Type of data
dataType: 'json',
// Funciton to be called if the request succeeds
success: function( data ){
console.log("POST successful with " + data);
}
})
What I don't fundamentally get is how to "pick up" the POSTed data in PHP. In my research, I came upon something saying I should look for $_POST['myvariable'] in PHP. Yet I'm not sure how or where I'd create something that would listen for such a POST from JS. It seems to me the easiest solution to this would be if I could write a method onto my API extension that simply assigns the value of the myvariable that's POSTed to the session variable whenever that thing is POSTed. I've written this method already, in fact, but it's unclear to me how I'd instruct AJAX to invoke it in PHP. I've also read that this type of thing may not be advisable for security reasons.
I've seen advice elsewhere suggesting I should do something in JS like:
var api = new mw.Api();
...and then use the Api object's methods to execute Ajax GET and POST requests. Well, I tried creating an instance of this object, and it throws errors on the console that say it's not a recognized function or something of that nature.
I'm rather new at all this, but I'm at my wit's end trying to figure out something that in theory ought to be very simple. Any suggestions?
I am new to java script. I have execute some API from server-side java script and stored their status in a array now i want to create a file at desired location on same machine and write output to it.
You must have to use server-side language to do this js won't do that!
Lets take the example of the ASP.NET
#{
var userData = /* data stored in the array */
var dataFile = Server.MapPath("~/App_Data/data.txt"); // file location
File.AppendAllText (#dataFile, userData); // save it!
}
This way, you will be able to save the data in the file on the server-side. If you wanted to save it without reloading the page (like many developers love to) then you can try using ajax request. For that, you will be using jQuery AJAX but still to write the data to a file you will require the server-side. Here
$.ajax({
url: 'url/where/file_is.txt'
});
And the in page, you will have the server-side code which will
Get the array data
Split it relatively
Write it to the file.
This way you will have the user's data written to the file.
For more: http://www.asp.net/web-pages/tutorials/files,-images,-and-media/working-with-files
If you're using PHP, you can do a Google search for that!
i'm using phonegap in order to make an application, then i'm using CodeIgniter as an API.
To get the data, i'm using a simple $.post from jQuery to a REST URL in CodeIgniter.
$.post(DIR+trad+"/format/json", function(data){
traducciones = jQuery.parseJSON(data);
}, json);
Based upon this, i got 2 questions, because i don't know if i should do it in different post methods.
The first question is, how do i get the data in json format?
The second question is, is there any way to call the json object directly from my view (HTML) ? or i need to do it using jquery or javascript?
how do i get the data in json format?
Searching for codeigniter+json gives me Output Class which has this example:
$this->output
->set_content_type('application/json')
->set_output(json_encode(array('foo' => 'bar')));
is there any way to call the json object directly from my view (HTML)?
Generally speaking, if you are generating the JSON yourself and you want to use it in HTML, then you will just skip generating JSON and use the data directly.
Generating JSON from a view only really becomes useful if you want to make the raw data available over a network.
If you want to fetch the data over HTTP for your view, then that is a job for the Model. See How to send a GET request from PHP?. This is useful if you have two different applications. One providing a web service for the data and one providing the user facing application.
If you want to update the page with new data after it has loaded, then this is a good usecase for JSON, but you can only do that with JavaScript (or another client side programming language).
If you want to using JSON as response format, just do like
$.post(url, {
"dataType" : "json",
"success" : function(response) {
// process your response here, it will be treated as JSON object
}
});
I have a JavaScript function onLoad a PHP page. Inside this JavaScript function, How can I use mysql query? Please Help me..
You can't really do that. JavaScript code runs in the browser, which (presumably) doesn't have access to your database server, and it doesn't have facilities for talking to remote databases anyway.
What you'd typically do is perform the SQL query from your PHP code, running on the server, and put the results into the page that it sends to the browser. If you want to use the results in JavaScript, you can make your PHP code generate a JSON string in a <script> tag so that the JavaScript code can just access a global variable that contains the data.
Or, you can use AJAX to load the data separately. This involves another HTTP request to a PHP page on the server, which runs the SQL query and sends back the results.
$(window).load(function() {
$.ajax({
method:'POST',
url:"ajax_page.php?branchcode="+branchcode,
success:function(data){
},
error: function (error) {
alert("Failed to load.. Kindly try again!!");
}
});
});
I have solved by using this AJAX, jQuery code.