Ethan Allen
Ethan Allen

Reputation: 14835

How do I pop a simple alert box via AJAX?

Here's my code in the tag:

<script>
function saveQuantity(quantity) 
{
  $.ajax({

  alert(quantity);

  })
}
</script>

and here's my HTML:

<form name="frmQuantity">
 <select name="owned" id="owned" onChange="saveQuantity(this.value);">
  <option selected="selected" value="0">0</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
  <option value="8">8</option>
  <option value="9+">9+</option>
 </select>
</form>

Why isn't this working? What am I doing wrong?

Upvotes: 0

Views: 18132

Answers (5)

KanhuP2012
KanhuP2012

Reputation: 407

Though you don't need Ajax for the same you are doing but still if you are researching then you can use it like.

$.ajax({
url: "http://yoururl.com",
beforeSend: function( xhr ) {
alert("if you want to call alert before request put it here and can skip your URL");
}
})
.done(function( data ) {
   alert("if you find alert after success response");
  }
 });

Upvotes: 1

yugi
yugi

Reputation: 874

write like this it will work in ajax method

<script>
function saveQuantity(quantity)  
{
 var that=this;
 $.ajax({
 alert(that.quantity);

})
} 
</script>

Upvotes: 0

user2957047
user2957047

Reputation: 1

AJAX is for asynchronous HTTP request (Jquery AJAX). It doesn't look like AJAX is what you need.

If you're trying to display a simple alert do this...

function saveQuantity(quantity) 
{
    window.alert(quantity);
}

Upvotes: 0

baao
baao

Reputation: 73241

For what you are writing you don't need ajax, but I guess you want to do an ajax call and on success alert.

If you only want to alert, this will do the trick:

<script>
function saveQuantity(quantity){
alert(quantity);
}
</script>

To do an ajax call and alert on success, do it like this:

<script>
function saveQuantity(quantity) 
{
  $.ajax({
  url:"/path/to/executing_script.php",
  type:"POST",
  data:{quantity : quantity}
  success:function(data){
  alert(data);
  }

  });
}
</script>

You need to echo the updated quantity on the executing_script.php to show it in your alert

Upvotes: 3

ekad
ekad

Reputation: 14614

You don't need ajax for a simple alert box. Change your script as below

<script>
    function saveQuantity(quantity) 
    {
        alert(quantity);
    }
</script>

Working demo: http://jsfiddle.net/jo8bmqv2/

Upvotes: 3

Related Questions