I am trying to send a variable from one PHP file to another PHP file and echo it:
<script>
$.ajax({
type: "POST",
url: 'AjaxCallTest.php',
data: ({Imgname:"13"}),
success: function() {
alert('form was submitted');
}
});
<script>
And then in the receiving PHP file (AjaxCallTest.php):
<?php
$temp = $_POST['Imgname'];
echo $temp;
?>
but AjaxCallTest file doesn't echo anything? I need to get this variable and echo it. Note that I've included jQuery library in my code but I didn't include it in AjaxCallTest.php.
replace this line data: ({Imgname:"13"}), with data: {"Imgname":"13"}
I tried this and it worked! Notice the closing <script> tag
I also added the return result to check the echo in 'AjaxCallTest.php'
<script>
$.ajax({
type: "POST",
url: 'AjaxCallTest.php',
data: ({Imgname:"13"}),
success: function(result) {
alert(result + ' form was submitted');
}
});
</script>
Your code worked fine for me. One error I could find is that you didnt closed the script tag correctly. close the script tag.
<script>
$.ajax({
type: "POST",
url: 'AjaxCallTest.php',
data: ({Imgname:"13"}),
success: function() {
alert('form was submitted');
}
});
<script>
Related
Main.php
<script>
$(document).ready(function(){
searchUser();
});
function searchUser() {
alert("aaaaaaaa");
var data = $("#user-search-form").serialize();
$.ajax({
type: "POST",
url: "test.php",
data: data,
success: function(response) {
alert("bbbbbbbb");
}
});
alert("cccccccc");
return false;
}
test.php
<?php echo "testing 1234" ?>
Directory:
phpturtorial/admin/main.php
phptutorial/admin/test.php
I am calling php function using ajax but not working. My code able to alert "aaaaaaaa" and "cccccccc" but cannot alert "bbbbbbbb". Any idea ? is it related to my incorrect path ?
phpturtorial/admin/main.php and phptutorial/admin/test.php are in two different directories.
Hence why it is not able to locate test.php.
Change url: "test.php" to url: "/phptutorial/admin/test.php"
With the Given Information:
Case 1:
You need to modify the ajax request url slightly.
$(document).ready(function(){
searchUser();
});
function searchUser() {
alert("aaaaaaaa");
var data = $("#user-search-form").serialize();
$.ajax({
type: "POST",
url: "phptutorial/admin/test.php", //or the path to test.php
data: data,
success: function(response) {
alert("bbbbbbbb");
}
});
alert("cccccccc");
return false;
}
Case 2:
Provided path could be wrong as they are identical.
I have a Javascript that calls a PHP script using AJAX get method. If i run the PHP script externally it works fine and creates the file connections.txt. But with JS it is not working
$(document).on("click", "#target2", function(){
var arr = ["one","two","three"];
$.ajax({
type: 'POST',
url: 'hello.php',
data: { name: "saurabh" },
success : function(msg) {
// here is the code that will run on client side after running clear.php on server
// function below reloads current page
alert(msg);
}
});
});
PHP script :
<?php
$fp = fopen('/Users/saurabh/Desktop/connections.txt', 'w');
echo "Saving file";
fwrite($fp, "hello");
//echo $_POST['yourarray']);
fclose($fp);
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.ajax({
type: 'POST',
url: 'hello.php',
data: { name: "saurabh" },
success : function(msg) {
alert(msg);
}
});
});
</script>
I just ran that on my localhost, and it works fine. You must be making some mistake with the onclick function. Do it something like this...
$(document).ready(function(){
$("#someButton").click(function(){
$.ajax({
type: 'POST',
url: 'hello.php',
data: { name: "saurabh" },
success : function(msg) {
alert(msg);
}
});
});
});
I know this has been asked before, but I can't get it working.
I'm loading a file on a page whose path and name are a variable contained in a PHP script.
My script.php is outputing a variable $filename which contains the path to a file that has to be open in an ajax request.
So, the file can be for example:
'../path/to/file/filea.json' or
'../another/path/fileb.json'
I tried this in my jQuery:
$.ajax({
url:'script.php',
success:$.ajax({
url: ??? // This ($filename) is what I'm trying to get from the 1st Ajax call
sucess: function(data){
//other code here
}
})
);
I know $filename is wrong in the second Ajax call but how do I get the value of that variable?
$.ajax({
url: "script.php",
success: function(data){
$.ajax({
var someParam=data; //response Data from 1st Ajax Call
type: "post",
url: "example.php",
data: 'page='+someParam,
success: function(data){
//Do More Here!
});
});
Try this:
inside script.php
echo json_encode($filename); //somewhere you need to have this echo.
jQuery
$.ajax({
url: 'script.php',
success: function (data) {
alert(data); //or console.log() and doublecheck you have the right info
$.ajax({
url: JSON.parse(data),
success : function (data) {
//other code here
}
})
}
}); // added a extra } to close the ajax
This solved the problem:
$.ajax({
async:false,
url: 'script.php',
success: function(data){
$.ajax({
url: data,
success: //code here
})
})
#Sergio: script.php wasn't echoeing the output of $filename... Thanks for reminding!
$(document).ready(function(){
$("#S1").change(function()
{
var IDCat=this.value;
$.ajax({
type: "GET",
url: 'product_modify.php',
data: {IDCat:IDCat},
success: function(data)
{
$("#p1").text(data);
$("#tempForm").load('tv_form.php');
}
});
});
});
It is my code , in load() function when I call 'tv_form.html' file it works, but when i call 'tv_form.php' it doesn't work , error is 403 forbidden
where is the problem ? .html works, but .php doesn't work.
Add URL instead of file name.
$(document).ready(function(){
$("#S1").change(function()
{
var IDCat=this.value;
$.ajax({
type: "GET",
url: 'product_modify.php',
data: {IDCat:IDCat},
success: function(data)
{
$("#p1").text(data);
$("#tempForm").load('URL to tv_form.php'); // www.example.com/file.php
}
});
});
});
I'm trying to execute a jquery code from a php page called by ajax.
Change html of div.error
Here's the code(If there is an easier way just tell/post):-no $.load
HTML/jQuery: jquery.js already linked
<input type='text' id='test'>
<div id='error' style='display:inline-block;'>CHANGE HERE</div><bR>
<input type='submit' value='run' id='button'>
<script type='text/javascript'>
$('#button').click(function(){
$.ajax({
url: "test.php",
type: "POST",
data: "test="+ test,
success: function(jqXHR){
},
error: function() {
alert('error');
}
});
});
</script>
test.php
<script type='text/javascript'>
$('#error').html('working');
</script>
The test variable that you are using doesn't seem to be declared anywhere. Try hardcoding first. Also the {key:value} syntax is preferred as it ensures to properly url encode the value when sending to the server:
data: { test: 'some value' }
and if you want to use a variable, make sure you have declared it:
$('#button').click(function() {
var test = 'foo bar';
$.ajax({
url: 'test.php',
type: 'POST',
data: { test: test },
success: function(data) {
},
error: function() {
alert('error');
}
});
});
Also if you want the javascript to execute in your php script remove the <script> tags:
$('#error').html('working');
and specify the dataType parameter to script in your $.ajax call:
dataType: 'script'
Another possibility is to have your test.php script simply return the value:
working
and then inside the success callback refresh the DOM:
success: function(data) {
$('#error').html(data);
},
The JavaScript from inside test.php won't run because you've never told it to. It's not just gonna run because you loaded it. It needs to be in the DOM to run.
Try this:
$.ajax({
url: "test.php",
type: "POST",
data: "test="+ test,
success: function(data){
$(data).filter('script').appendTo('head');
},
error: function() {
alert('error');
}
});
Your test.php script should output what you want to display.
it should look something like:
echo "<html>";
echo "<div>working</div>";
echo "</html>";
so that it returns something. Right now, you are returning only javascript.