last day (14 days later) » 

11:02 AM
0
A: How to pass JS variable to PHP variable

SMLYou need to change data to: data: {text: something}, and also change the name of the variable in js, as the compiler won't know which text to take, like: var something = $('#title').text(); Please do comment if this does not work!

 
Ok so I've made the changes but I still get the same error, undefined 'something' now on $name = $_POST['something']. @SML
 
SML
the $_POST['text'] will be the variable stated at the start of your data data: {text (this one):something}.
 
Success alert is still there but same error. (with namechange obviously)
 
SML
can you please write the code you are using?
also, how are you triggering the ajax code?
 
Full code added. I just expected the ajax code to run when the 'delete.php' code was required by the previous file
 
SML
11:02 AM
I think that you need to trigger it with something like $(document).ready(function(){ajax call});
 
Added the document ready function, still the same notice saying that Notice: Undefined index: text
can i insert code here... tries
here is the delete.php with document ready added

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>

var something = $('#title').text()
$(document).ready(function(){

$.ajax({
//url: "../ref_files/response.php",
url: "WhatsNew.php",
type: "post",
cache: "false",
data: {text: something},
success: function(){
alert("success");
},
error: function(){
alert("failure");
}
});
});
</script>

<?php
echo "hello";
$name = $_POST['text'];
echo "Response: " . $name;
 
SML
ok
basically what the error is saying, is that when transferring the variable across, text is not being found!
 
Yes, I will test with a manually added variable of something like var something = "hello";
 
SML
sure
that should work
maybe jquery has not been able to find the selector given...
:/
 
var something = "hello";
alert (something);
Bring up the alert with hello, but still index undefined. I just don't get it. Maybe the page isn't reloading? I'll try add an echo to the php in the same file.
 
SML
11:11 AM
fair enough
 
yeah the php echo's execute but the $_POST is still undefined somehow
 
SML
ok
 
I'm going to try the delete.php code in a new file without all the require nonsense and see if it executes correctly
unreal, same error. Can you try this code and see if it works for you?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>

var something = "hello";
$(document).ready(function(){

$.ajax({
//url: "../ref_files/response.php",
url: "aaa.php",
type: "post",
cache: "false",
data: {text: something},
success: function(){
alert("success");
},
error: function(){
alert("failure");
}
});
});
</script>

<?php
echo "hello";
$name = $_POST['text'];
echo "Response: " . $name;
?>
 
SML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>


$(document).ready(function(){

$.ajax({
//url: "../ref_files/response.php",
url: "aaa.php",
type: "post",
cache: "false",
data: {text: something},
success: function(){
alert("success");
},
error: function(){
alert("failure");
}
});
});
</script>

<?php
echo "hello";
$name = $_POST['text'];
echo "Response: " . $name;
?>
one sec soz
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>


$(document).ready(function(){
var something = "hello";

$.ajax({
//url: "../ref_files/response.php",
url: "aaa.php",
type: "post",
cache: "false",
data: {text: something},
success: function(){
alert("success");
},
error: function(){
alert("failure");
}
});
});
</script>

<?php
echo "hello";
$name = $_POST['text'];
echo "Response: " . $name;
?>
do the second one in a file named aaa.php
 
does that work for you?
I get hello
Notice: Undefined index: text in C:\xampp\htdocs\Website\ref_files\aaa.php on line 26
Response:
 
SML
11:22 AM
remove the comment too...
testing one sec
 
comment removed, same error except line 25 now
 
SML
ok
ill test it on my server
one sec
do you have errors set anywhere???
helloResponse:
is my response
 
No I have no errors, just the notice.
im going to try isset() to see if theres anything at all inside $_POST['text'];
 
SML
fair enough
 
what. If i modify the existing php to this is works...

<?php
echo "hello";

if(isset( $_POST['text'])){
$name = $_POST['text'];
echo "Response: " . $name;
}
?>
trying with main file
 
SML
11:31 AM
fair enough
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>


$(document).ready(function(){
var something = "hello";

$.ajax({
url: "aaa.php",
type: "post",
data: {text: something},
success: function(response){
alert("success");
document.write(response);
},
error: function(){
alert("failure");
}
});
});
</script>
this is working
when this is loaded before in something like b.php
and a.php has this
soz aaa.php has this
<?php
echo "hello";
$name = $_POST['text'];
echo "<label id='1'>Response: " . $name . "</label>";
?>
 
YES
 
SML
you have to make it write your response if you want to see it..
 
Ah.
 
SML
does that help???
 
Yes very much!
Ehm, can you respond to your post with a reply of the php you used there like
<?php
echo "hello";
$name = $_POST['text'];
echo "<label id='1'>Response: " . $name . "</label>";
?>

and i'll select your answer as correct :)
 
SML
11:35 AM
YAY!!
:D
 
i luv u
a bit anyway xD
 
SML
haha
:D
 
Thanks so much ^_^
 
SML
my pleasure
contact me if u need any more help...
 
Yes definitely! Thanks again <3
 
SML
11:37 AM
seems like quite an interesting project!
:D
thanks for the ratings!
:D
:)
 
np ^_^
you deserve them -.O!!
anyhow I've been at my computer for ages, I must help around the house with chores. I'll definitely contact you again if I need more help :D
 

last day (14 days later) »