$("a.star").click(function(e){
e.preventDefault();
var dataID = $(this).data('id');
$.ajax({
type: "POST",
url: "/engine/save.php",
data: "id=dataID"
success: {
alert("FFS WORK " + data);
}
});
return false;
});
<a href="javascript:void(0)" data-id="7" class="star">test</a>
How can I send data-id to save.php (/engine/save.php?id=7) successfully? Tried just about everything and no luck.
asked Jun 5, 2012 at 15:43
ditto
6,41711 gold badges58 silver badges92 bronze badges
3 Answers 3
just with
data: { id : dataID },
the benefit of using an object (instead of a string concatenation) is that you don't need to worry to escape the value passed along with the ajax call
answered Jun 5, 2012 at 15:44
Fabrizio Calderan
124k26 gold badges172 silver badges183 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
ditto
I changed the data to this but it's still not processing anything to /engine/save.php. I wonder if there is an error in my syntax?
Fabrizio Calderan
you should also add a comma
, before success: (look at your js console)ditto
Found it just after posting. Tar. :P
url: "/engine/save.php?id=" + dataID
answered Jun 5, 2012 at 15:44
asawyer
17.8k8 gold badges63 silver badges92 bronze badges
Comments
First of all you should understand how to concatenate a string with a js variable. You should use + operator to concatenate a string and a js variable.
Use this
data: "id=" + dataID;
You can also send it as an object jQuery will take care of attaching it to the request.
data: { id: dataID }
answered Jun 5, 2012 at 15:44
ShankarSangoli
69.9k13 gold badges96 silver badges124 bronze badges
1 Comment
ditto
I see. This wasn't the actual problem with my code though. Even when changed to these the data will not be passed to /engine/save.php. :( Is there a problem with my syntax at all?
lang-js