This question already has answers here:
jQuery Ajax POST example with PHP
(17 answers)
Closed 7 years ago.
The title of this question is probably misleading, I didn't know a good way to consicely ask what I need help with.
So basically, I use the following ajax to send a javacript variable on index.php to a separate php file (page2.php)
var Variable1 = '1';
$.ajax({
type: "POST",
url: 'page2.php',
data: "NewVariable=" + Variable1,
success: function() {
...Save or echo the value of $newervariable here ....
}
});
So basically above I am sending the variable Variabl1 to page2.php . Page2.php looks something like this:
<?php
if(isset($_POST['NewVariable'])) {
$NewVariable = $_POST['NewVariable'];
$NewerVariable = $NewVariable + 1;
}
?>
I know the example is kind of obsolete because you could just do add 1 using javascript without having the 2nd php page, but I just simplified it down really it boils down to me needing the 2nd php page, and also knowing how to save the values of it after success (if it's even possible)
On JS
success: function(data) {
Variable1=data;
}
On PHP:
<?php
if(isset($_POST['NewVariable'])) {
$NewVariable = $_POST['NewVariable'];
$NewerVariable = $NewVariable + 1;
echo $NewerVariable;
}
In php page echo the output :
<?php
if(isset($_POST['NewVariable'])) {
$NewVariable = $_POST['NewVariable'];
$NewerVariable = $NewVariable + 1;
echo $NewerVariable;
}
?>
And in your js :
var Variable1 = '1';
$.ajax({
type: "POST",
url: 'page2.php',
data: "NewVariable=" + Variable1,
success: function( data ) {
// data --> data you echo from server side
alert( data );
// do whatever you want here
}
});
Related
I'm a newbie around here and I've run into a dead end.
So I've created a function to draw a calendar in php. And I've been using css to set the colors of days and that stuff. The function runs well and uses 2 input variables like this:
PHP file (calendar.php):
<?php
function draw_calendar($month,$year)
{
// the code
}
echo draw_calendar($current_month,$current_year);
?>
What I've been trying to do is use jquery/ajax (from my index file) to post/get variables to calendar.php for $current_month and $current_year to use. And then echo the function to my index somehow. For the life of my I can't manage to do that. I could only find how to return simple strings and alert them to my index.
I am not sure what code you have written up, but her is the general idea of how you can use ajax to send over your needed variables.
On your index file:
var month = "11"; //your month
var year = "2019"; //your year
$.ajax({
type: "POST",
url: "calendar.php",
data: {month: month, year: year},
dataType: "json",
success: function(response){
//if it is success
},
error: function(){
//if it results in error
}
});
On your calendar file:
$month = $_POST['month'];
$year = $_POST['year'];
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 4 years ago.
I have php page which contains two javascript functions like this:
function PregledDokumenta(id)
{
var info = id.split('-');
var vrdok = info[0];
var brdok = info[1];
$.ajax({
type: "POST",
url: "../Php/Dokument/Pregled.php",
data: { vrDok: vrdok, brDok: brdok },
success: function(returnedData)
{
document.getElementById('Content').innerHTML = returnedData;
<?php
$_SESSION['vrDok'] = vrdok;
$_SESSION['brDok'] = brdok;
?>
<?php
$a = $_SESSION['vrDok'];
echo("alert($a);");
?>
}
});
}
function UnosNoveStavke()
{
var vrdok = <?php echo($_SESSION['vrDok']);?>;
var brdok = <?php echo($_SESSION['brDok']);?>;
<?php
$a = $_SESSION['vrDok'];
echo("alert($a);");
?>
$.ajax({
type: "POST",
url: "../Php/Dokument/IzborRobe.php",
data: {vrDok: vrdok, brDok: brdok},
success: function(returnedData)
{
document.getElementById('Content').innerHTML = returnedData;
}
})
}
So when i load page and press button i run PregledDokumenta(id) function. In there i pass some values from id of element and then i echo back some other text and buttons as you can see i alert $_SESSION['vrDok'] to see if it is assigned and it returns value. Then when i click button (about 10 sec after running first function) that i echoed back from first function i run second UnosNoveStavke function. There you can see I again alert to see if $_SESSION return value but it return undefined. Why is that happening?
You are very confused regarding how JavaScript and PHP communicate with each other!
For example, with the following code, you are effectively trying to assign JavaScript variables, which you get from an AJAX request, into PHP sections:
<?php
$_SESSION['vrDok'] = vrdok;
$_SESSION['brDok'] = brdok;
?>
This can't work, because all PHP expressions are evaluated before the page even loads.
If you need to save these values in PHP sessions, you have to do it either in the file the AJAX request is sent, in your case Pregled.php, or a PHP file that you include in it.
Assume I have 2 textbox, that's serial_no10 and serial_no12. That 2 textbox appear not simultaneously depends on case
1 PHP file for checking the SN.
1 DIV status to display the data.
jQuery Ajax
var serial_no10 = $("#serial_no10").val();
var serial_no12 = $("#serial_no12").val();
$.ajax(
{
type: "POST",
url: "chk_dvd_part_no.php",
data: 'serial_no10='+ serial_no10 +'&serial_no12='+ serial_no12,
success: function(msg)
{
$("#status").ajaxComplete(function(event, request, settings)
{
}
}
}
HTML
<div id="status"></div>
PHP File
if(!empty($_POST['serial_no12']))
{
echo "Serial No 12";
}
else if(!empty($_POST['serial_no10']))
{
echo "Serial No 10";
}
Now I'm facing the problem when get POST from textbox serial_no_12, the value is undefined. But if get POST from textbox serial_no_10, I got the value.
Is that something wrong with that PHP code? Or I do something that should not be.
You have to just empty the variables before filling up. As if value is not reset then last value computed would remain in variavar
serial_no10 = $("#serial_no10").val();
var serial_no12 = $("#serial_no12").val();ble
change it with
var serial_no10='';
var serial_no12='';
serial_no10 = $("#serial_no10").val();
serial_no12 = $("#serial_no12").val();
Noww do things it will all good
Give your form tag an id if it has no anyone. and than do something like this.
var form = $("#form_id").serialize();
$.ajax({
type: "POST",
url: "chk_dvd_part_no.php",
data: form,
success:function(msg)
{
$("#status").ajaxComplete(function(event, request, settings)
{
//do your stuff
});
}
});
and in php file get your post variable by its name, suppose you have 2 inputs name serial_no10 and serial_no12
now do your php code like this.
if( isset($_POST['serial_no10']) && $_POST['serial_no10'] != '' ){
echo 'Serial No 10';
}
if( isset($_POST['serial_no12']) && $_POST['serial_no12'] != '' ){
echo 'Serial No 12';
}
This question already has answers here:
Javascript and PHP functions
(7 answers)
Closed 9 years ago.
In my index.php file, I have a php function "InsertTheRecord" which gets a parameter and insert it into the database. It return 1 if that parameter is inserted successfully otherwise 0.
I have following JavaScript function "InsertRecord" in same index.php from where I want to call php InsertTheRecord function. How can I call php function from JavaScript function?
My JavaScript function:
function InsertRecord() {
var myParameter = 40;
var result = ////call InsertTheRecord(myParameter) //I don't know how to do this?
if result == 1 { //do something}
else { //show error message}
}
php server side scripting language and javascript is client side scripting language
you can do this by ajax call(Jquery)
<div id="yourdiv"></div>
var data ="hello world";
var data2="hello all";
function run(){
$.ajax({ url: 'myscript.php',
data: {'q': data,'z':data2},
type: 'post',
success: function(output) {
alert(output);
document.getElementById("yourdiv").innerHTML += output; //add output to div
}
});
}
myscript.php
<?php
myfun();
function myfun(){
$myvar2 = $_POST['z'];
$myvar = $_POST['q']."how are you?";
echo $myvar."\n";
echo $myvar2;
}
?>
this alert "hello world how are you?"
Try
var result = <?php echo InsertTheRecord(myParameter); ?>
updated after OP's comment
$.ajax
function InsertRecord() {
$.ajax({
type: "POST",
url: "your_php_page.php",
data: "arg1=id&arg2=name",
success: function (data) {
var myParameter = 40;
var result = data;
if result == 1 {} else {}
}
});
}
PHP is serverside,
JS is clientside,
So the PHP works first, after that the JS works dynamically in the browser.
You cannot call the PHP function in runtime. But you can use AJAX which can do that. Check this out: http://www.w3schools.com/ajax/ajax_aspphp.asp
What if you echod the myParameter to a hidden input field and then grabbed it with javascript:
HTML/PHP file:
<input type="hidden" id="myParameter" value="<?php echo InsertTheRecord(myParameter); ?>"/>
In Javascript:
function InsertRecord() {
var myParameter = 40;
var result = document.getElementById("myParameter").value();
if result == 1 { //do something}
else { //show error message}
}
It is not possible
Javascript is client side scripting language where PHP is server side scripting language
But, you can try AJAX methods to get similar results where you can pass variables same as function
as function myfunction(var1, var2 ,....){return var1*var2}
using ajax, you can run a php script externally on button click
$.ajax({key:var},function(result){alert(result)});
http://www.w3schools.com/jquery/jquery_ajax_intro.asp
http://www.tutorialspoint.com/ajax/
This question already has answers here:
Returning JSON not working properly
(7 answers)
Closed 9 years ago.
i am returning json from my php file, the php file works fine and json is echoed properly, but the problem within the js...
the output should be {"name":"somename", "id":"someid","l":"something","sname":"somename","desc":"longdescriptionhere"}
JAVASCRIPT
function getClubData(id) {
alert(id);
$.ajax({
url: 'someurl',
crossDomain: true,
type: 'post',
data: id,
success: function (data) {
var json = jQuery.parseJSON(data);
alert(data);
},
});
};
PHP
$json = json_encode(array('name' => $name, 'id' => $id, 'l' => $l, 'sname' => $sname, 'desc' => $desc));
echo $json;
i am sure the php is correct, because it echoes on the page, the correct information, its just not transferring properly back to the javascript...
EDIT / UPDATE
Now that I actually get whats going on, and what you want...
Heres my updated answer
You need to give the id a key, so that on the other side your PHP script knows where to get this id....through the $_POST variable.
So your javscript...
function getClubData(id) {
alert(id);
$.ajax({
url: 'someurl',
crossDomain: true,
type: 'post',
data: {'theid':id},
success: function (data) {
var json = jQuery.parseJSON(data);
alert(data);
},
});
};
And then your php script would get that id
$id = $_POST['theid'];
$sql = "SELECT * from YOURTABLE WHERE ID = $id";
//and rest of your query and database stuff etc etc