This question already has answers here:
How to get data from one php page using ajax and pass it to another php page using ajax
(4 answers)
Pass object to PHP through AJAX
(3 answers)
How to print all information from an HTTP request to the screen, in PHP
(8 answers)
Closed 3 years ago.
I'm trying to call a function inside controller that will return a echo
but I get error as result show inside my id #ly-id-result
{"success":false,"message":"View not found [name, type, prefix]: places, raw, hpj_storemanagerView","messages":null,"data":null}
My Ajax call in : httpdocs/modules/mod_storemanager_map/tmpl/default.php
var ly_inputVal = document.getElementById("ly-val-inp").value;
jQuery.ajax({
url: "index.php?format=raw&option=com_storemanager&task=abc",
data: {text:ly_inputVal},
method: "POST", //Use post method
cache: false, //Specify no cache
success: function(result){
//console.log(ly_inputVal);
jQuery("#ly-id-result").html(result);
}
});
in my controller in : httpdocs/components/com_storemanager/controller.php
public function abc()
{
// Get Joomla's input object:
$input = JFactory::getApplication()->input;
// Get the variable
$value = $input->get('ly_inputVal');
return $value;
}
I found the error; I have to change
$value = $input->get('ly_inputVal');
to
$value = $input->get('text');
Now it working
Related
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 months ago.
The scenario is here I accept all button click that has ID starts with 'editbtn' like editbtn1/editbtn2 etc ; and get only the number to send via ajax to find data id based on the button id (number)
$(document).ready(function(){
$('[id^=editbtn]').click( function () {
noform=this.id.match(/\d+/);
event.preventDefault();
$.ajax({
'url': 'visitor_book_crud.php',
'data': {
'request': 'find_data',
'data_id': noform
},
'type': 'post',
'dataType': 'html',
'beforeSend': function () { }
})
.done( function (response) {
console.log(response);
})
.fail( function (code, status) { alert(code+' - '+status);
})
.always( function (xhr, status) { })
});
});
And In visitor_book_crud.php
if ($request=="find_data"){
$data_id = $_REQUEST['data_id'];
$mqdata = mysql_query("SELECT * FROM tb_visitorbook WHERE id_vb = '$data_id'") or die (mysql_error());
$mfadata = mysql_fetch_assoc($mqdata);
if($mfadata){
echo implode(",", $mfadata);
} else {
echo "failed";
}
}
I tried to directly send request to visitor_book.crud?request=find_data&data_id=1 and the output is like this, exactly same as what I want to be appeared in ajax response
1,2022-06-29,03:07:30,03:39:39,6,,A_NAME,3,,,SOME_NAME,,1,
But when I press edit button, it says
<br />
<b>Notice</b>: Array to string conversion in <b>C:\xampp\htdocs\security_editless\visitor_book_crud.php</b> on line <b>48</b><br /> //line 48 is in mysql_query("SELECT......
failed
I searched from many thread but still dont solve my problem, any help would be appreciated
First of all, you should do all the validation before sending the data from JS and also validate the data on the PHP side as well.
However noform = this.id.match(/\d+/); will give your an array. to get the number you'll have to use noform[0], also make sure you do the validation if any match found or not before using noform[0]
right now you're sending noform as data_id and your PHP is warning about it.
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 3 years ago.
I have several products. On click product, I send "id" of clicked product to Local Storage. To use this variable in php I send it to items.php ($.post).
Then I need use this "id" from items.php to show in the cart, but variable is emply.
var LSArray = JSON.parse(localStorage.getItem('productID')) || [];
function clickOnProduct(id) {
var newItem = {'id': id};
LSArray.push(newItem);
localStorage.setItem("productID", JSON.stringify(LSArray));
var dataLS = JSON.parse(localStorage.getItem('productID')) ;
$.ajax({
type : "POST",
url : "/wp-content/themes/twentynineteen/items.php",
data : { name : dataLS[dataLS.length-1].id },
success: function(res){
console.log(res);
}
});
}
All works fine, but how can I use "id"?
<?php incude(items.php); ?>
dosn't work.
You are sending it by post. So you can take a look to your post variable by
<?php var_dump($_POST); ?>
Then you will see the array and probably you can access your data by
$_POST['name']
This question already has answers here:
how to assign javascript variable value to php variable [duplicate]
(11 answers)
Closed 4 years ago.
i'm using a onchange function to post value to ajax.
function viewTest() {
jQuery("#empy_card").hide()
var test_type= jQuery('#test_type').val();
var myvals = jQuery("#table").html('');
jQuery.ajax({
type: "POST",
url: "test.php?smt=command",
dataType: 'json',
data: {test_type:test_type},
success: function(data){
//alert(data.date);
//alert(data.book);
<?php
$date = data.date;
$book = data.book;
?>
}
});
}
its work successfully. i got in alert form data.date
"18-Dec-10","20-Apr-11"
i got in alert form data.book
"book A","book C"
test.php
<?php
if(isset($_GET['smt'])=='command'){
$test= $_POST['book_name'];
include('connection.php');
$data=array();
$result ="SELECT * FROM books WHERE Type_of_books='$test'";
$ftc = mysqli_query($conn,$result)or die(mysqli_error($conn));
while($row = mysqli_fetch_array($ftc)) {
$data['date'][] ='"'.$row['date'].'"';
$data['book'][] = $row['name'];
}
echo json_encode($data);
}
?>
how can i get this data to php variable like $dates = data.date; And $book = data.book
Why are you using php in Ajax and you are assigning JavaScript variable to php. If you really want that response use cookies or session variables.
Remember that your PHP code is evaluated by the server, not the client. That means that you can access your $date and $year in various ways.
Server side (PHP)
You have to delete your Ajax request and write it using PHP, then you can access the variables.
Client side (JavaScript)
You already got them in data!
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 9 years ago.
I am writing the following code below, which basically takes the data from the current date to a PHP file to handle in jQuery. Until here everything well. But I'm not understand why I can not have a new value of the total variable after it picks the value coming PHP file.
day.each(function () {
var $this = $(this);
var the_index = $this.index();
var the_date = $this.find('h3.date').html().substr(0,2);
var the_day = $this.find('h3.day');
/*THIS VARIABLE*/
var total = 0;
$.get('trd/datetime.php', function (date) {
if(that.hasClass('list-'+date.day)) {
weekList.find('.item.list-'+date.day).addClass('active');
}
total = date.firstDate;
}, 'json');
console.log(total);
});
I don't know if my english helps, but, please, tell me what wrong I'm doing!
Thanks.
The .get call is asynchronous -- the stuff inside it runs after your console.log statement.
You probably want to use a callback, which you invoke from inside the .get handler:
$.get('trd/datetime.php', function (date) {
// ...
callback(total);
}, 'json');
function callback(total) {
console.log(total);
}
This question already has answers here:
how to pass these strings from php to javascript
(5 answers)
Closed 9 years ago.
I need to pass a value from PHP back to my javascript. I have tried so many things but can't get it to work. Here is my code.
<script type="text/javascript">
life = {};
life.anjaxEnter = function(finished) {
$.ajax({
dataType:"json",
type:"POST",
data:mydata,
url:"myurl.php",
async:false,
success:life.receiveResult,
});
setTimeout("location.href='myredirectionurl'", 1000); // Redirect after 1 second
}
life.receiveResult = function(result) {
alert(result);
}
</script>
I need to pass a specific value from the PHP file back to life.receiveResult and then add this to the redirection url.
The biggest problem is probably that at the time when you assign life.receiveResult to success, life.receiveResult is undefined. You need to define it before you assign it to something else.
myurl.php needs to return a JSON result when it's called. Something like this:
<?php
header("Content-Type: text/json");
$mydata = "Hello";
echo json_encode(array('ok' => true, 'mydata' => $mydata));
?>
Have a look at the json_encode docs, the header docs and some of the other answers for more information.