Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I have a form where users can click a selection of a dropdown-box.
Depending on that selection a group of names must be shown.
I believe step 1 is best to be done in AJAX but what about step 2.
After reading a lot on the internet I believe the best way is using a JSON-object. Is this the best way?
Can somebody explain me how make this work?
You can send a POST request with the input parameters via AJAX, then return an array of JSON objects from the PHP function your AJAX request calls (JSON is very easy to parse in JavaScript).
Something like (assuming using jQuery):
$.post('somepage.php',{'age':'18'},function(data,status){
if (data instanceof Array) {
// Clear the display div
$('#displayDiv').html('');
// Append all the items to the div
for (var i = 0; i < data.length; i++) {
$('#displayDiv').append('<p>'+data[i].firstName+' '+data[i].lastName+'</p>');
}
} else {
return false;
}
});
So if your list of names is on a file in your server, you have a .php file that accepts requests and you use AJAX to submit the right GET.
In your javascript using jquery, do something like
$.getJSON("names.php", {n:choice})
and in your php, do
$n=strtolower($_GET["n"]);
do whatever you need in your code, then eventually
echo json_encode($result);
and then you do whatever you want with the JSON object, which can be handled like any other js array.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Sorry in advance if what I am going to ask is silly, I am having trouble understanding the asynchronous HTTP responses. I am trying to use jQuery .ajax() with PHP and MySQL in the following context:
when page loads I will query and return all the applications for a particular id, and then I will use a while loop to output them;
also there is a button that allows the user to add new application for that particular id -> on click it:
shows a hidden form and gets the values in the inputs;
sends the data to PHP using the .ajax() method;
performs the insert.
and I am stuck at this point
I need to output the newly added application above the existing ones, but the query and while will do it only when an synchronous HTTP request is sent.
I tried to use the .success() but the HTML I need to output inside is really long and I am afraid it will be hard to maintain having outputs from both PHP while loop and jQuery.
Can you please help me understand an efficient way to do this? I have never dealt with asynchronous HTTP request before. Also, I am can't use any JavaScript templating libraries.
I would really appreciate your help!
An ajax postback typically implies some sort of JSON-based web api behind it. Examples of this on the internet abound.
https://www.lennu.net/jquery-ajax-example-with-json-response/
http://www.9lessons.info/2012/05/create-restful-services-api-in-php.html
Your REST endpoint should return the minimal amount of JSON data you need. At which point you can use that data to update/bind to elements in your DOM inside your "done" or "success" callback.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i am trying to clean up my code..
is there a way to run this code with Ajax or jquery rather than placing the code in the html page itself..thanks in advance
<?php $acct = new acct; echo $acct->accountProfile("accountphone"); ?>
You can have js execute an ajax function (via $.post) on a load event like $( document ).ready(function() {//code}, using something like $("#div").html(data); to populate data into a container.
That said, I don't think that's necessarily more organized than putting it into your HTML code itself. You don't want to process LOGIC too much in your view, but the nature of web development is that it is "messy" (IMO) and that you have to often just insert PHP all over the place, to do singular functions/echos as you seem to be doing.
Yes, you could put all your PHP code in a separated PHP file, eg.
PrintAccount.php
$acct = new acct;
echo $acct->accountProfile("accountphone");
And at your HTML , via AJAX (I'm using ajax with jquery in this example) you "print" php file response in your html
index.html
<div id="accountphone"></div>
<script>
$.ajax({
type: "POST",
url: "PrintAccount.php",
success: function(data){
$("#accountphone").html(data);
}
});
</script>
Now, my personal opinion: This is good? depends, if you have a lot of PHP code is actually good doing this, you could improve it as real services like REST, it'll be awesome for clean code at HTML files. You have also another option, using MVC, so most of code will be written at Controller, and just a few at View. Now, about your given example, with just a small amount of PHP code this is totally unnecessary, you are complicating too much a simple task.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So I currently have a PHP script that is being called from an HTML link. The script is set up such that it needs to accept non-user input from the page (static data that already exists). The line that calls the script looks like this.
<div class="Name">Text</div>
I need to pass the values in "Name" and Text to the script, as well as another value from an earlier line.
What would be the best way to accomplish this? All of my research points to using forms and GET/POST, but as you can see, there is no place for the user to input any of the data. Is there any way to do this using hidden forms or AJAX?
If you're the author of the web page, you'd use javascript and an onclick event to capture the div class and the anchor's text, and send it via ajax (or directly if your script provides some sort of user output or redirect back to the calling page) to your script as a post event. the data could be conveniently formatted as a json structure to simplify the script's processing.
By using the GET method :
<div class="Name">Text</div>
Maybe this code well help
<div class="Name"><a id="link" href="some/script.php">Text</a></div>
<input id="name" onblur="modify(this.value)">
<script type="text/javascript">
var link = document.getElementById("link").href;
var modify = function(name){
var a = document.getElementById("link");
a.href = link + "?name="+name;
}
</script>
In the href attribute for the link, do something like this:
href="some/script?name=George&text=All This Stuff"
This will make the information available in the $_GET[] array in the PHP script.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Sorry I'm a newbie in web programming. I have a problem to send array from Javascript / JS array to my function in controller and load new view.
I try to use ajax and I can read that array but ajax not load new view, how to do that?
Please tell me the correct way to send javascript array to controller and load new view.
Please help.
Here's my javascript:
$.ajax({
type: "POST",
url: "<?= base_url() ?>index.php/test/coba",
data: {
test: "test"
}
});
and my controller test.php
function coba() {
$data['test'] = $this->input->post('test');
$this->load->view('newview',$data);
}
Please help
AJAX is meant to send or receive data from the client side to the server side. You can't display a view by using an AJAX method, what you could do, however, is receive values that could then be displayed by using Javascript code.
But AJAX is mainly used to give the client the possibility to interact with the database, which doesn't seem to be what you are trying to do, are you sure you need to use AJAX?
If you need to display something depending on user behaviour, without requiring database calls, then Javascript should be all you need. You could add the HTML code with PHP, and hide it, and when the user does the specific action, you can fill what needs to be filled and display your content.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am storing some functions in an object, and these functions need to be accessed in PHP via the onclick attribute of the dynamic button. Normally, this would be very simple if the functions were not stored in an object, but unfortunately, this is how these functions need to be created:
var arrayLength = <?php echo $arrayLength; ?>;
var click = {};
for (var num=1;num<=arrayLength;num++) {
var newClick = "click_" + num;
click[newClick] = function() {
// some contents when this button is clicked
};
}
To call them in javascript, I'd do something like:
click['click_' + someID]();
However, since I'm working with PHP, I am not so sure how I will be able to call this in the onclick attribute. It is something I really need to do, so if you know a solution or can think of a workaround, let me know.
First: What is so special about your function that you have to write it's body in JS and call it in PHP?
Second: Why not just write that function in PHP and pass data from JS via AJAX?
Third: make double sure you know difference between http, html, php, javascript and AJAX and understand what these are, not just some wibly-wobly stuff that make web pages work. Then you will know your answer yourself.
Your objects can go from PHP to JS, but not the other way around. In order to alleviate this issue, you can use json_encode($object) and pass it to the javascript to be processed. From there, you can then access the properties and values from PHP.