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 9 years ago.
Improve this question
My question to save time:
What is the simplest way to count clicks of a button, save that value to a Wordpress MySQL db and retrieve that updated total number into a div with Ajax, so the page doesn't need to up reloaded to see it?
I'm just learning how to dive into more UX glory with added mysql db support and Ajax updates with jQuery as the driving force.
My knowledge of working with databases in general is... green to say the least.
I have another question going, but I thought I'd open this up as a more general question. I see lots of tutorials online, but not a lot of elegant, minimal code like I know I've learnt here on SO already.
OP is here: Counting clicks with jQuery and displaying with Ajax
You can bind to the button's click event and then tell your server-side logic to increment the count in the database, like this:
$("#YourButton").click(function() {
$.ajax({
url: '/PathToServerSide',
type: 'POST',
data: {increment: true},
success: function(data) {
alert('Server click count updated!')
}
});
}
Note: Never trust the client-side to tell the server-side what the count value is, but instead send a command (increment) to tell the server-side logic to increment the value in the database.
To display the amount of clicks, use the data returned from the AJAX success callback and the jQuery .html() function, like this:
$('#YourCounter').html('The button has been clicked ' + data + ' times`);
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
This is going to be my first question on stackoverflow so I hope I'm doing it right =)
I want to insert a MySQL record when the page loads.
I have a form and a database. I want to be able to see in my database if the user has visited the form page( obviously without submitting the form ).
I know how to do it on page submit, but how can I fill up a MySQL row on page load?
Example of my wish:
example
I'm very curious how this can be done . Thanks in advance!
You need to use AJAX for this kind of stuff..
update.php
<?php
mysqli_query("INSERT INTO bla (column1, column2) VALUES (value1, value2)") or die(mysqli_error());
?>
jQuery:
$(document).ready(function(){
$.ajax({
type: "GET",
url: "update.php"
success: function(data) {
// success
}
});
});
This is a really basic way, you could use "$_GET" to send data in your AJAX request etc.
-
Edit: #David had a fair point in the comments. Try that first.
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 8 years ago.
Improve this question
i am reading names from xml and displaying on php page.how to send this names to another page when user clicks on any name,and how can i save that name in another page till user clicks other value???
You can call ajax.
<script type="text/javascript">
jQuery(function($){
var field1 = $('#field1');
$('#send').click(function(){
/* Enter your code */
jQuery.ajax({
url: 'somepage.php',
type: 'post',
data: 'field1value=' + field1.val(),
success: function(results){
alert(results);
}
});
});
});
</script>
this seems like a task for AJAX. But this means you need to program at least in JavaScript or JQuery for that.
Take a look here at this example: http://www.w3schools.com/jquery/jquery_ajax_get_post.asp
Greetz
For 1st, you can pass the name in url by using any variable.
For example: www.xyx.com?name=your_selected_name
For 2nd, You can save the clicked value in session variable. when other value is clicked, unset the previous value.
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 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 5 years ago.
Improve this question
I want to create a dashboard for a bespoke web service we have so that we can monitor usage on a real-time basis. Something similar to the couchbase dashboard if you have seen that. If not, its basically a line graph that scrolls right to left with time along the bottom of the graph and usage on the y-axis.
To minimise data bandwidth I am assuming there is a way to pass just the changed data to update a graph rather than sending a new image/vector each time. And this would get drawn with a javascript charting library.
What I am unclear on is how to get the data from the server. Is the normal approach to have the javascript code poll the server using AJAX every second (if you are after second by second updates) or is some other method used?
I used PHP for the back end and HTML/JS for the client end.
And does anyone have any examples or links that could be helpful.
For getting the data from the backend, I think you can call it using Ajax, reload each second using setInterval(), making something looks like:
setInterval(function() {
$.ajax({
url: url of your backend server,
type: 'GET',
success: function (data) {
//data returned from the server, use it to draw your chart
}
});
}, 1000); // recall ajax every second
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 7 years ago.
Improve this question
Is it possible to create a auction site in PHP using AJAX to refresh the page once a user has entered a higher amount of money.
I can handle the PHP side of it okay but I was wondering if AJAX can really be used so that it refreshes often without putting a lot of strain on server resources?
I also plan to use JQuery to implement AJAX as this makes the job a whole lot easier. Anybody have any code examples that you think could be used?
Any help would be appreciated/
Thanks!
var currentHighestBid=0;
setTimeOut(getHighestBid,5000); //5000ms wait before polling for a better price
function getHighestBid()
{
$.ajax(
url: url, // ur php end point,
type: "GET"
data: {} //json data if you want to send anything as a querystring parameter to your servre
dataType:"json"
success: function(response)
{
if(response.currentMaxBid>currentHighestBid)
{
currentHighestBid=response.currentMaxBid;
//code to update your markup
}
}
});
Hope that made sense..
Read abt json in php
Polling would work as "zzzz" mentioned. Comet (Push based instead of Polling) would be a nicer/better solution for this use case. However PHP is not really good at this with high traffic sites. Node.JS with Socket.IO would be a good solution for you :)