I was asked a question by a friend and I honestly for the life of my could not come up with a sure answer. So I pose it to you instead;
If I have 2 documents (page.html and form.php), and I have included form.php in my page.html as so:
<html>
<head>
<title>Home</title>
<?php include 'form.php'; ?>
</head>
<body>
<form>
<FORM CONTENT HERE>
</form>
<div class="results">
<p><?php echo $result_variable ?></p>
</div>
</body>
<script>
!-- Ajax function here, POSTs to form.php
</script>
</html>
Which has an AJAX form, POSTing to form.php, and a field for results, that prints the PHP variable $result_variable.
Now if My PHP page takes the posted data, after the initial document load / include has been completed - then changes the value of $result_variable as a result - will the value of $result_variable be updated within the page.html without the page reloading?
For example - if form.php looked something like this;
<?php
$result_variable = 1;
if (!empty($_POST)){
$result_variable = 2;
}
?>
After the initial pageload (without form submission), $result_variable should yeild 1. Though after the ajax form submission has been completed, the $result_variable has been changed to 2. Will this reflect without reloading page.html?
Thanks.
Nope. since $result_variable is not part of the current page that includes form.php.
It will only work if you populate it using javascript.
Let's say:
ajax targets form.php, so this is what form.php should look like:
<?php
$result_variable = 1;
if(!empty($_POST)){
$result_variable = 2;
}
// print the result as an ajax response (the format here is not JSON)
echo $result_variable;
// if the response format is JSON
echo json_encode(array("result_key_name" => $result_variable));
?>
Here's an example using jQuery $.post http://api.jquery.com/jquery.post/
// on a normal php/html page
$.post("form.php", form_data, function(data){
// data is the result echoed using json_encode() from form.php
alert(data.result_key_name); // as per the server-side example above
}, "json");
Related
I am using the Scripts n Styles plugin with this code to auto-fill a logged in user's email into form on WordPress:
<script type="text/javascript">
window.onload = function () {
document.getElementsByName("Element-Name")[0].value = "<?php $current_user->user_email ?>";
}
</script>
However, when I refresh the page I get the following text (the quoted value in the code above) instead of the actual value.
<?php $current_user->user_email ?>
Any idea what I am missing here? Is this an issue with the plugin?
The first thing I see is that you don't output anything.
Try
<?php echo $current_user->user_email; ?>
But in your example it should have been just an empty value. There is something fishy with the PHP parser not detecting the PHP part.
I have this code which uses jquery and ajax to send a request to the other page i.e.
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<input type="button" id="butt" value="button11111111111111" >
</html>
<script>
$("#butt").on('click',function(e)
{
$.ajax(
{
type:'POST',
url:'testmysql.php',
data:
{
product_type:"cake";
}
});
});
</script>
When i click the above button it should send the request and data to the other file testmysql.php
<?php
session_start();
$_SESSION['type']=$_POST['product_type'];
echo $_SESSION['type'];
?>
However , when i refresh the other page after clicking the button i do not see any kind of echo and it gives me a notice stating
Undefined index: product_type
Since, i am new to ajax and jquery is there anything i am missing ?if yes,then what should i do to make this work ?
Thanks!
Note: Both of them are in the same directory.
First of all, remove the semicolon(;) from this statement,
product_type:"cake";
^
otherwise it will give you syntax error. And now comes to your issue.
when i refresh the other page after clicking the button i do not see any kind of echo and it gives me a notice stating Undefined index: product_type
That because when you refresh testmysql.php page, the $_POST array would be empty, and there would be no index named product_type in $_POST array. You can verify it using var_dump($_POST);.
On testmysql.php page you can check whether $_POST['product_type'] is set or not like this:
<?php
session_start();
if(isset($_POST['product_type'])){
$_SESSION['type']=$_POST['product_type'];
echo $_SESSION['type'];
}
?>
you added the ; at the end of the data object. remove it and try.
and you are sending a ajax request to the testmysql.php page, the $_POST['product_type'] will available when you are send the request. when you refresh the page the you are not sending any post request to testmysql.php page.
I already checked all others answers from that questions, but no one seems to work.
He's my problem, I try to reload a div where I got a php variable.
I got MVC system, so i can't call other files when i want.
My controller doesn't receive arguments from ajax call ($_POST and $_FILES are not isset).
When I try to do other stuff after ajax call, it works (example: unlink a file)
<?php
$test = 'lksd';
...
if( some conditions )
{
$test= 'piof';
unlink(../images/myImage.jpg);
}
?>
...
<div id="test">
<?php
echo $test;
?>
</div>
And my ajax call is in a jquery plugin (http://www.dropzonejs.com/)
I found where i can do my stuff, and then i call this function:
$('#test').load(window.location + "#test");
My div doesn't refresh but my file is deleted, if you have any idea of what can i do to solve this problem...
Thanks in advance
Create the PHP detail you want to stand alone. Call it something like getDetail.php:
<?php
$test = 'lksd';
...
if( some conditions ) {
$test= 'piof';
}
echo $test;
?>
In your page, you can now load that content at any time:
<html>
<body>
<div id="test">
</div>
Reload
<script>
$(document).ready(function(){
$("#test").load("getDetal.php");
$("#reloadDetail").click(function(){
$("#test").load("getDetail.php");
});
});
</script>
</body>
</html>
I want to display the content of Division in one page to another.
<div id="box-cont" class="box-content">
<?php
echo $stat;// Contains multiple images with strings
?>
</div>
Here $stat will display multiple images with few contents. And i am using jQuery AJAX to display this html in another page.
var bcont = $('#box-cont').html();
$.ajax({
type:"POST",
url:"abc.php",
success: function(data) {
document.location.href='def.php?bcont='+bcont;
}
});
And i am getting this html in def.php as
$_GET['bcont'];
This is not working for me..
Thanks in advance
A shortcut method would be to use sessions to pass the html from one page to another.
<div id="box-cont" class="box-content">
<?php
echo $stat;
$_SESSION['stat'] = $stat; // make sure session_start(); is present on this page
?>
</div>
Then, in the success handler of your ajax call
$.ajax({
type:"POST",
url:"abc.php",
success: function(data) {
window.location.href='def.php';
}
});
Finally, in def.php
session_start();
echo $_SESSION['stat'];
Note: This is not an ideal approach but will do the job for you
Ok, you want to pass the html of #box-cont to def.php, right?
You're mixing post and get. Read: http://api.jquery.com/jquery.post/
and you'll find you need to use the data-parameter.
There's something odd with the success-part. You don't reload the frame/div
where def.php is sitting. What you've done is passing data to the server
but not getting anything back, (if I read your attempts correctly).
In fact there's currently little use for a server-via here, from what you
describe it can all be done at client / JS.
I am trying to make an on-line user content which refresh it self and calls every time php function.
I used
<div class="onlineFriends">
<?php
$members = find_online_friends($_SESSION['id']);
foreach($members as $member):?>
<div class="user" href=<?php echo ($member['f_id']); ?> rel="popup_name" >
<img src=<?php
$avatars = find_avatar($member['f_id']);
foreach($avatars as $avatar)
echo ($avatar['src']) ?>
/>
</div>
<?php endforeach; ?>
</div>
<script>
$(function(){
var refreshId = setInterval(function() {
$('.onlineFriends ').load("# .onlineFriends ").fadeOut("slow", function () {
$(this).fadeIn("slow");
});
}, 50000);
});
</script>
This works good. But .load() function I think loads first all entire page and then calls .onlineFriends. I can see it with firebug on console. it returns all page source code as GET answer. My question is it will make slow down ? Because I will use this method 5 times for other div contents and each time each functions will load full page.
Also I tried to create separate .php file but I have in php code some dependences and I cant run this function in another file.
Any request to your page will result to it's full code. You can specify in php to send back only wanted part of page when specific GET or POST data is present and use $.get() or $.post() to get them.
Because I will use this method 5 times
On the same page? Then it's better to replace them from the one of $.get() and $.post() data during callback function.
Create other page friends.php
and in page index use cod java/ajax and use jquery-min
<script src="js/jquery-1.10.1.min.js"></script>
$(document).ready(function() {
$("#center").load("friends.php?var=<?php echo $member['f_id']; ?>");
var refreshId = setInterval(function() {
$("#center").load('friends.php?var=<?php echo $member['f_id']; ?>');}, 9000);
$.ajaxSetup({ cache: false });
your div html use
<div class="center"></div>
your friends.php use
<?php include"your bd here" $jab = $_GET['var'];
'select bd'
echo $resultyourneed['f_id'] ?>