Jquery AJAX on A href - php

I am working on the Jquery A href (used as Button) with button name Install. I've wrote the code for the calling Jquery AJAX file , Ajax file name is update.php.
Once ajax successfully executed , I'm changing a href label using.
$(.install-blue).text('Stop Installing');
Now , I am trying to call updateStop.php. When i click on the Stop Installing (a href).
Issue is both are sharing same class name, so that it calling update.php
Is there any unique way to execute this operation ?

You could use HTML 5 data attribute to save the state like for example : jsfiddle
Html
<a class="install-blue" data-state="stopped">Start Installing</a>
<div id="msg">
</div>
Jquery
$(document).ready(function(){
$(".install-blue").click(function(){
if($(".install-blue").data("state") == "stopped"){
$(".install-blue").text("Stop Installing");
$(".install-blue").data("state", "started");
}
else{
$(".install-blue").text("Start Installing");
$(".install-blue").data("state", "stopped");
}
});
});

Use:
$('.install-blue').addClass('fistClass');
When the user clicks on install.
Then use:
$('.firstClass').text('Stop Installing');
When the user click on stop installing.

Fiddle
HTML
<input type="button" href="update.php" value="Install" class="install-blue" />
jQuery
$('.install-blue').click(function(){
var url=$(this).attr('href');
alert(url); ///call this ajax url
$(this).val('Stop Installing'); // add this on ajax success
$(this).attr('href','updateStop.php'); // add this on ajax success
});

I am not sure if this example is what you want to do.
$(document).ready(function(){
$('.selector').click(function(e){
e.preventDefault();
if($(this).text() == 'Stop Installing'){
//DO STOP ISTALLING STUFF
}else{
//DO INSTALL STUFF
$(this).text('Stop Installing');
}
});
});

Related

laravel how to route to a route on a javascript?

I have this jquery
$(".waitingTime .button").click(function () {
alert("Ema");
});
I have a a tag like this:
Can I do the same href action in the jquery function?
Many Thanks
yes this is possible and has nothing to do with Laravel.
There are different possibilities. If your query is embedded within the same laravel view, you put the URL directly in your jQuery code, for example like this:
$(".waitingTime .button").click(function () {
window.location.href = "{{URL::to('restaurants/20')}}"
});
But I think the best option is to add the URL on your button tag as a data attribute and then let jquery go to that URL. That way you can make your buttons more dynamic and have more capsulated code.
One example might be:
<div class="waitingTime">
<button class="button link-button" data-href="{{URL::to('restaurants/20')}}">
Click me
</button>
</div>
$(".link-button").click(function () {
window.location.href = $(this).data('href');
});
That way you can always give a button with the class link-button a data-href attribute with the URL you want to open when the button is clicked and don't have to add additional jquery.
Just output php in your javascript
$(".waitingTime .button").click(function () {
window.location.href = "<?php echo URL::to('restaurants/20'); ?>";
});
if need variable from js can use this
if(data.cookies == 0){
location.replace("{{ route('delivery') }}?id="+id);
}

Reload div using jquery load function working only once and how to pass a variable

I have very limited knowledge with scripts so I hope you guys can help me with a simple solution to a small problem that I have...
I'm using the following jquery function to refresh a div with new content when a link is clicked
<script>
$(function() {
$("#myButton").click(function() {
$("#loaddiv").fadeOut('slow').load("reload.php").fadeIn("slow");
});
});
</script>
My problem is, I need to send 2 variables to the reload.php page to use in a mysql query (I have no idea how to accomplish that), also I need to make multiple links work with this function, at the moment I have multiples links with the same id and only the first link works so I guess I must associate different ids to the function in order for this to work, how can I do that?
here's the page where i'm using this: http://www.emulegion.info/teste/games/game.php
You may want to use document ready instead of function on your first line as this will make sure the code is not executed until the full page (and all elements) have loaded.
You can then use the callback functions of the fade and load to perform actions in a timely manner.
additional variables you can add after the .php, these can then be read in your reload.php file as $var1 = $_GET['var1'];
Do make sure to sanitize these though for security.
<script type="text/javascript">
// execute when document is ready
$(document).ready(function() {
// add click handler to your button
$("#myButton").click(function() {
// fade div out
$("#loaddiv").fadeOut('slow',function(){
// load new content
$("#loaddiv").load("reload.php?var1=foo&var2=bar",function(){
// content has finished loading, fade div in.
$("#loaddiv").fadeIn('slow');
}); // end load content
}); // end fade div out
}); // end add click to button
}); // end document ready
</script>
For different variables you could add a HTML5 style variable to your button.
<input type="button" id="myButton" data-var1="foo" data-var2="bar" />
You can retrieve this when the button is clicked:
// add click handler to your button
$("#myButton").click(function() {
// get vars to use
var var1 = $(this).data('var1');
var var2 = $(this).data('var2');
...
load("reload.php?var1="+var1+"&var2="+var2
if you have multiple buttons/links I would use class instead of id "myButton". that way you can apply the function to all buttons with the above script. Just replace "#myButton" for ".myButton"
First, you should use .on('click', function() or .live('click', function() to resolve your one click issue.
You'll want to do something like:
<script>
$(function() {
$("#myButton").on('click', function() {
var a = 'somthing';
var b = 'something_else';
$.post('url.php', {param1: a, param2: b}, function(data) {
//data = url.php response
if(data != '') {
$("#loaddiv").fadeOut('slow').html(data).fadeIn("slow");
}
});
});
});
</script>
Then you can just put var_dump($_POST); in url.php to find out what data is being sent.
Try creating a function that would accept parameters that you want.
Like:
$(document).ready(function(){
$('.link').click(function(){
reload(p1,p2);
});
});
function reload(param1, param2){
$("#loaddiv").fadeOut('slow').load("reload.php?param1="+param1+"&param2="+param2).fadeIn("slow");
}
But by doing the above code your reload.php should be using $GET. Also you need to use class names for your links instead of id.
<script type="text/javascript">
// execute when document is ready
**$(document).ready(function() {**
**$("#myButton").click(function() {**
**$("#loaddiv").fadeOut('slow',function(){**
**$("#loaddiv").load("reload.php?var1=foo&var2=bar",function(){**
// content has finished loading, fade div in.
$("#loaddiv").fadeIn('slow');
});
});
});
});
</script>
$("#myButton").click(function() {
// get vars to use
var var1 = $(this).data('var1');
var var2 = $(this).data('var2');

Jquery val() does not recheck / reevaluate the value

short description
I'm using jquery, jqueryUI and ajax.
I use val() to get value from a field the first time it does it perfectly
but if I change the value in the field the value stay the same.
Long descriptionn
I'm developing a big project I have ~2 years on this project
My head is pumping! ;)
I have something like this :
this is the html for the div :
<div id="add_task_diag" style="display: none">
<label for="task">Task : </label><input id="taskfrm" label="Task" value="task" name="taskval" placeholder="e.g fill in tax form" />
<button value="submit" class="btn btn-large add_task_frm">Add task</button> <button class="btn btn-large cancel_add_task">Cancel</button>
</div>
<button class="btn btn-primary" id="add_task"><i class="icon-file icon-white"></i> Add Todo</button>
this is the script :
<script>
$(".add_task_frm").click(function() {
function taskgen() {
var data = "task=" + $("#taskfrm").val();
alert(data); // only for debuging
var task_data = data;
alert(task_data); // only for debuging
$.post("whereitneedtogo.php?task=action80", task_data), //this is the ajax part
$("#add_task_diag").dialog("destroy"); // I had here close I changed it to "destroy"
task_data = undefined; //I tried to unset the variable
}
taskgen();
});​
</script>
When I run it everything work fine! when I resend with another value the variable stays the same. e.g if I put test123 in the field in the seccond test it will stay test123
I need to reload the page to reset the variable.
I don't want to use location.reload()
this file is 1700+ lines long but this is the part where the problem is.
I tried alot of things like :
- setting my script after Dom is ready
- setting my variables to undefined or null
- destroy the jquery tabs
- the only part seems to be working for me is location.reload but my application is full ajax.
Thank for helping I hope this is stupid or easy one .
Why are you trying to declare and call the function every time you click.. Try moving it outside the click event handler..
<script>
$(".add_task_frm").click(function() {
taskgen();
});​
function taskgen() {
var data = "task=" + $("#taskfrm").val();
alert(data); // only for debuging
var task_data = data;
alert(task_data); // only for debuging
$.post("whereitneedtogo.php?task=action80", task_data), //this is the ajax part
$("#add_task_diag").dialog("destroy"); // I had here close I changed it to "destroy"
task_data = undefined; //I tried to unset the variable
}
</script>
You might also encounter scoping issues when you want to call the taskgen() from some other place..
It's because you define your closure inside the click handler. Move the taskgen function outside of the click handler, wrap everything in $(document).ready() and you should be good to go! See below:
$(document).ready(function() {
$(".add_task_frm").click(function() {
taskgen();
});
function taskgen() {
var data = "task=" + $("#taskfrm").val();
alert(data); // only for debuging
var task_data = data;
alert(task_data); // only for debuging
$.post("whereitneedtogo.php?task=action80", task_data), //this is the ajax part
$("#add_task_diag").dialog("close").dialog("destroy"); // I usually chain 'close' and 'destroy'
}
});

Can the PHP $_GET be used to get a variable in the URL using Hashchange?

Click Me
<script src="https://raw.github.com/cowboy/jquery-hashchange/v1.3/jquery.ba- hashchange.js"></script>
<script type="text/javascript">
$(window).hashchange(function () {
//
});
</script>
When Click Me is clicked the URL looks like this "www.mydomain.com/#create=1".
What I am trying to do is us the $_GET in PHP to bring down the parameter. Ex:
<?php echo $_GET['create'];?>
Using the
Click Me
works, but it reloads the page and that is what I am trying to avoid. Any help would be greatly appreciated.
PHP runs on the server. A request has to be sent to the server for PHP to know what is in the query string. You don't need to reload the whole page but you will need to send something to the server, e.g. in an AJAX request and do something with the result.
//java script code
$("#clickme").click(function(event) {
var arr = $(this).attr('href').split('#');
var arr=(arr[1]);
event.preventDefault();
$("#content").load("data.php?"+ arr);
});
//html code
<a id="clickme" href="#create=1">Click Me</a>
<div id="content"></div>
// php code
data.php
There is a new future for that:
window.history.pushState("Remember me!", "Changing the get Parameter...", "?create=1");
You can apply this to all links by using this:
$("a").click(function() {
if ($(this).attr("href").substr(0, 1) != "#") {
$("body").load($(this).attr("href"));
window.history.pushState("Remember me!", "Nothing special", $(this).attr("href"));
return false;
}
});

how to pass a "a href click" into jquery

i am trying to put a pagination page into jquery. example.html?id=foo&get=23
i would like to pass the get= into the jquery script so that i can change the page within the div instead of sending the link to the having the user see example.html?id=foo&get=23, they should only see example.html?id=foo. the rest is done in jquery.
<script >
$(document).ready(function()
{
$("#data").load("page.php?ht=<?php print $id; ?>&p=1" );
$("#next").click(function(){
var pageNum = this.id;
$("#content").load("page.php?ht=<?php print $id; ?>&p=" + pageNum, Hide_Load());
});
});
<div id="data">
page1
</div>
Why not modify the anchor
$('#next').attr("href","example.html?id=45");
After every click on the page?
you need something like this:
$(document).ready(function(){
$("#next").click(function(){
$.post('your_script_to_get_values.php',
{ page: "1" },
function(data) {
$('#content').html(data);
});
});
});
Make sure you use a live click handler, and not just a click handler, because you are creating the #next element after the page loads.
$("#next").live('click', function(){ //etc.
Documentation for .live()
It matches the current selector now and in the future (i.e. if the elements are created dynamically).
The simplest way would be with a session variable.

Categories