How to use Ajax in Smarty? - ajax

This is ajax request
<script type="text/javascript">
{literal}
$(document).ready(function(){
$("#S1").change(function()
{
var IDCat=this.value;
alert(IDCat);
$.ajax({
type: "GET",
url: 'product_modify.php',
data: {IDCat:IDCat},
success: function(data)
{
alert(data);
alert("success");
}
});
});
});
{/literal}
</script>
And this is php codes
if(isset($_GET['IDCat'])){
$idc= $_GET['IDCat'];
echo $idc;
}
there is the problem echo $idc; doesn't work ? where is the problem ?

var IDCat=this.value;
should be
var IDCat=$(this).val();

Related

Laravel Return Response From Ajax

I am trying to learn Laravel9. For this I have Created a Controller, a model and blade view file. Below is my vatprofile.blade.php file code
<script type="text/javascript" src="https://code.jquery.com/jquery-2.0.2.js"></script>
<script>
$(document).ready(function () {
$.ajax({
type: "get",
url: "/vatprofile",
dataType: "json",
success: function (response) {
alert(response);
}
});
});
</script>
And this is my Rout,
Route::get('/vatprofile',
[VatProfileController::class,'index']);
And Below is my Controller Function
public function index(){
return response()->json(['success'=>'Record is successfully added']);
}
It is not Giving Error, Not printing out any response. Only the Blank Page is Showing up. Can you Correct me Pls
Please try this:
<script type="text/javascript" src="https://code.jquery.com/jquery-2.0.2.js"></script>
<script>
$(document).ready(function () {
$.ajax({
type: "get",
url: "{{ url('vatprofile') }}",
dataType: "json",
success: function (response) {
alert(response);
}
});
});
</script>

Getting error in ajax request when calling API

Can anyone tell me that what is the error in my code? When I call my API, it could not able to get success.
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url:"https://www.erp.tpci.in/api/badge/list?authId=indusfood&authPassword=indusfood#123&page=1&company_name=&name=&code=&badge_id=141&buyer_id=1300&limit=10",
type: "GET",
dataType: "json",
success: function(data){
console.log(data);
},
error: function(error) {
console.log('Error');
}
});
});
</script>

Ajax function not triggering

Why my 'Ajax' function is not triggering when changing the 'state' please help. even the 'console' is not showing the value. When i comment Ajax function I am getting the stateID in console
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$(document).on("change", "#state", function(){
var stateID = $(this).val();
console.log('value of StateID'+stateID);
$.ajax({
url:"user/getCity"
async: false,
type: "POST",
data: {stateID,stateID},
dataType: "html",
success: function(data) {
//$('#city').html(data);
console.log(data);
}
})
});
});

$.ajax serialize() does not pass data to php file

What is incorrect in the code? Can not pass data to _autosave.php
<script type="text/javascript">
$(document).ready(function(){
autosave();
});
function autosave() {
var t = setTimeout("autosave()", 5000);
var inputValues= $('.input_form').serialize();
$.ajax( {
type: "POST",
url: "_autosave.php",
data: inputValues,
} )
.done(function(data){
alert(data);
});
...
    
Input is this
<form id="input_form" autocomplete="off" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]) ?>"
method="post">
<input type="text" name="input" id="input">
_autosave.php is this
$input = $_POST['input'];
echo $input .' input<br>';
If I enter some value in input, get input<br> instead of entered value
Update
If for someone may be necessary here is working code
$.post("_autosave.php", $("#form1").serialize(), function(data) {
$('#load').html(data);
$('#is_row_changed1').val(0)
});
You have a trailing comma here resulting in invalid javascript:
data: inputValues,
Here's how you could fix (and improve your current code):
<script type="text/javascript">
$(document).ready(autosave);
function autosave() {
window.setTimeout(autosave, 5000);
var inputValues = $('.input_form').serialize();
$.ajax({
type: "POST",
url: "_autosave.php",
data: inputValues
})
.done(function(data) {
alert(data);
});
}
</script>
or if you prefer a shorthand:
<script type="text/javascript">
$(document).ready(autosave);
function autosave() {
window.setTimeout(autosave, 5000);
var inputValues = $('.input_form').serialize();
$.post("_autosave.php", inputValues, function(data) {
alert(data);
});
}
</script>
Have you tried with serializeArray() instead?
<script type="text/javascript">
$(document).ready(autosave);
function autosave() {
window.setTimeout(autosave, 5000);
$.post("_autosave.php", $('.input_form').serializeArray(),
function(data) {
alert(data);
});
}
</script>

jQuery get returns error

Why does this simple ajax show an alert with "error"?
<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(document).ready( function(){
$.ajax({
url: "http://www.google.com",
success: function(data) { alert(data); },
error: function(req, err) { alert(err);}
});
});
</script>
You can't directly do this with javascript but there are alternative ways to do it if you are using a server.
javascript part:
<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: "http://yourserver/geturl.php?url=http://www.google.com",
// or url: "http://yourserver/geturl.aspx?url=http://www.google.com",
success: function(data) {
alert(data);
},
error: function(req, err) {
alert(err);
}
});
});
</script>
the server part (for geturl.php):
<?php
echo file_get_contents($_GET["url"]);
?>
or the same logic with asp.net.
the key part is here, that the code runs the javascript and php(aspx) should be on the same domain.

Resources