I've this code
$(document).on('click', '#button', function(){
var my_data = 'Test Test Tes';
$.ajax({
type: "POST",
url: "preview.php",
data: "my_data=" + my_data,
success: function() {
window.open('preview.php');
},
});
});
Above codes open the preview.php
but I'm getting this error -
Notice: Undefined index: my_data in C:\\xampp\\htdocs\\tb-builder\\preview.php on line 3
This is what I've in preview.php
-
<?php
$data= $_POST['my_data'];
echo $data;
?>
Can anybody help me to solve this please? Thanks
Main error is ,
window.open('preview.php');
loads the pagepreview.php
,
You are not sending any data withpost/get
request while loading it usingwindow.open('preview.php');
and you are expecting value at$_POST['my_data']
$(document).on('click', '#button', function(){
var my_data = 'Test Test Tes';
$.ajax({
type: "POST",
url: "preview.php",
data: "my_data=" + my_data,
});
});
preview.php
<?php
if(isset($_POST['my_data']){
$data= $_POST['my_data'];
header('location:preview.php?my_data=$data'); //reload preview.php with the `my_data`
}
if(isset($_GET['my_data'])){
$data=$_GET['my_data'];
echo $data; //display `my_data` as you requested
}
?>
With out using ajax ,to achieve what you required.
$(document).on('click', '#button', function(){
var my_data = 'Test Test Tes';
window.open('preview.php?my_data='+my_data);
});
preview.php
<?php
if(isset($_GET['my_data'])){
$data=$_GET['my_data'];
echo $data;
}
?>
Actually you don't need
ajax
at all , because you are going to another page with the data .
to avoid get
request
Wrap the
button
inside aform
with hiddeninput
having valuemy_data
and setaction="preview.php"
like below
<form action="preview.php" method="post">
<input type="hidden" id="my_data" value="Test Test Tes">
<button type="submit" id="button">post</button>
</form>
window.open
just create a new window
object. That window will have no $_POST
globals. Use window.open('preview.php?my_data=' + my_data);
and use $_GET['my_data']
in that PHP.
The post is related to the ajax not for the new window.
EDIT
Ok guys, I see here is a little confusion.
Above codes open the preview.php but I'm getting this error -
So what happens?
OP is sending a data called my_data
to the preview.php
. This data is travelling through $_POST
to the preview.php
. At this point, there is $_POST['my_data'];
When it finish with success, the ajax call, then the window.open
opens a new window , what has no $_POST
data, this is why OP get this error message.
If he want to use the same data what he posted in my_data
then he need to add it to the URL and use $_GET
.
I think the misunderstood was what @HoangHieu wrote and me pointed:
yes. in new window – Xahed Kamal
And if you want to use response, then use
success: function(reponse) {
window.open('preview.php?my_data=' + response);
},
and then use $_GET['my_data']
in new window.
use
if (isset($_POST['my_data']))
{
$data= $_POST['my_data'];
echo $data;
}
Please change your preview.php code with the following :-
<?php
if (isset($_POST['my_data']))
{
$data= $_POST['my_data'];
echo $data;
}
?>
It may help you.
It was completed question in
Is your error in new window ?? – HoangHieu 1 min ago
yes. in new window – Xahed Kamal
First Ajax post it completed when success: function called... In new window you doesn't have any post data, I don't know any solution post data by javascript to new window.. only one you can send data by GET request ..
Your construct:
data: "my_data=" + my_data,
Is wrong. It should be:
data: {my_data: $my_data},
And I changed your JS variabale to $my_data
, so it's less confusing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.