I'm going to create a page that user can input something in textarea (index.php). When user click on Tweet, it will get all text that user typed to page tweet.php (I used jQuery Ajax method). After that, it will redirect to page show.php
Here is my code
index.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Tweet</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
function save_tweet(text_tweet){
$.ajax
({
type: "POST",
url: "tweet.php",
data: "text="+text_tweet,
success: function(msg)
{
if(msg==1){
window.location.href="show.php";
}else{
alert("Error");
}
}
});
}
$("#tweet").bind('click', function(){
var text_tweet = $("#text_tweet").val();
if(text_tweet==""){
$("#show").html("Blank text");
return;
}else{
$("#show").html("");
save_tweet(text_tweet);
}
});
});
</script>
</head>
<body>
<center>
<textarea name="text_tweet" cols="61" rows="5" id="text_tweet"></textarea>
<br/>
<div id="show"></div>
<br/>
Tweet
</center>
</body>
</html>
tweet.php
<?php
session_start();
if(isset($_POST['text'])){
$_SESSION['text_tweet'] = $_POST['text'];
}
?>
show.php
<?php
session_start();
echo $_SESSION['text_tweet'];
?>
The problem is when i input some text in textarea and click Tweet, it will alert Error. Can anyone know what is the problem?
Thank in advance.
Why are you checking if msg is 1 ? Are you returning 1 in your response body ? Looking at the JQuery docs it the success function callback is described as:
Type: Function( PlainObject data, String textStatus, jqXHR jqXHR )
You could log the value of msg and see what is actually contains.
Try changing your code to:
$.ajax
({
type: "POST",
url: "tweet.php",
data: {text: text_tweet},
success: function(msg)
{
if(msg==1 || msg=="1"){
window.location.href="show.php";
}else{
alert("Error");
}
}
});
tweet.php
<?php
session_start();
if(isset($_POST['text'])){
$_SESSION['text_tweet'] = $_POST['text'];
echo 1;
}
?>
try
if(msg.length){
window.location.href="show.php";
}else{
alert("Error");
}
Add the dataType in your ajax:
dataType: 'text',
Add above line in your ajax code.
And change your below line:
var text_tweet = $("#text_tweet").val();
with the:
var text_tweet = $("#text_tweet").text();
And in your tweet.php file add this line:
echo "1";
Related
It is showing undefined function in PHP page.And i made every possible way it is not working for me.Please guys help me with this.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
<div id="myDiv">Welcome to stackoverflow</div>
</body>
<script>
$(document).ready(function(){
var mb = $('#myDiv').text();
$.ajax({
action : "six-cuf.php",
type: 'POST',
data: { 'mb': mb},
success:function(data)
{
alert(mb);
}
});
});
</script>
</html>
PHP CODE
<?php
$fname = $_POST['mb'];
?>
Hello
$(document).ready(function(){
var mb = $('#myDiv').text();
$.ajax({
action : "six-cuf.php",
type: 'POST',
data: { mb: mb},
success:function(data)
{
alert(mb);
}
});
});
You can also try this JQuery Post Method
Javascript Code
$.post("six-cuf.php", {'mb': mb}, function (data) {
console.log(data);
});
PHP code
<?php
$fname = $_POST['mb'];
echo $fname;
?>
simple question for most of you but for me, being a newbie in php and jquery+ajax, not really: how to replace my index.html with some other html code, requested by ajax call from a php file?
index.html
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<h1>Login page</h1>
<button id="btn_login">Login</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="javascript.js"></script>
</body>
</html>
javascript.js
$('#btn_login').click(
function(){
$.ajax({
url: "login.php",
type: "GET",
success: function(data){
// what to do here?
}
});
}
)
login.php
<?php
echo '<div>Succesful login</div>';
?>
TLDR: I want to replace the "Login page" + login button screen to "Succesful login" when clicked the button.
Thank you
you need to identify -or classify- your <h1> tag to be easily able to change
it's contents ,
<h1 id='title'>Login page</h1>
then , within your ajax call, if success you can change the content like that:
success: function(data){
// what to do here?
if (data) {
$('#title').html(data);
$('#btn_login').remove();
}
}
Take a look: ( https://www.w3schools.com/xml/tryit.asp?filename=tryajax_first : w3schools )
...
You can try this:
$('#btn_login').click(
function(){
$.ajax({
url: "login.php",
type: "GET",
success: function(data){
document.body.innerHTML = ""; // remove all content from the document
document.write(data); // write the returned div to the document
}
});
}
)
I'm trying message application. My goal is get sender id and receiver id with a click on one button.
After then post this datas with ajax or ajax(json) to php in same page.
I will use the incoming data in php with mysql_query. I tryed many examples. But never get result. My example code at below.
Little Note: Success alert comes but doesn't print any data on screen.
<html>
<head>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
<button type="button" onclick="myFunc()">Get ID</button>
<script>
function myFunc()
{
var id = 'example';
jQuery.ajax({
url:'index.php',
type: "POST",
data: {'name':id},
success: function(data)
{
alert("success");
}
});
};
</script>
<?php
if(isset($_POST['name']))
{
$value = $_POST['name'];
echo $value;
}
else
{
echo "don't work.";
}
?>
</body>
</html>
<?php
if(isset($_POST['name']))
{
echo json_encode(array(
'value' => $_POST['name']
));
exit();
}
?>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
<button type="button" onclick="myFunc()">Get ID</button>
<script>
function myFunc()
{
var id = 'example';
jQuery.ajax({
url:'index.php',
type: "POST",
data: {'name':id},
dataType : 'json',
success: function(data)
{
alert("success");
}
});
};
</script>
</body>
</html>
I want to call a javascript function from php at the form submit event. and that javascript function will access the php variables, send them to a php script on another website using ajax. The following code is just a representation.
<?php
....
.....
......
if($_POST["action"]=="xyz"){
$myname = "asim";
echo "<script type='text/javascript'>submitform();</script>";
}
.....
....
...
gotoanotherpagefinally();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function submitform(){
alert("<?php echo $myname; ?>");
$.ajax({
type: 'POST',
data: {
action: 'whatever',
fileID: "<?php echo $myname; ?>",
},
url: 'http://xyz.com/API/query.php'
});
}
</script>
</head>
<body>
<form id="myform">
------
------
<input type="submit" name="submit_details">
</form>
</body>
</html>
I need to call the javascript function from php because the php variables taken by js function are only set by php itself and not the form values or something.
You can catch the submit event using jQuery:
$("#myform").submit(function(e) {
// needed so the default action isn't called
//(in this case, regulary submit the form)
e.preventDefault();
$.ajax(...);
});
you can use :
<form id="myform" onsubmit="submitform()">
You can use jQuery to do this.
$(':input').click(function(){});
then inside this function you can use an ajax request to send the variable to php page that you have.
var variableA, variableB; // variables to be initiated by an ajax request
// ajax request to get variables from php page
$.ajax(
{
url:'php_page_path/source_page.php',
data:"message=getVars",
type: 'post',
success: function(data){
// use the data from response
var obj = JSON.parse(data);
variableA = obj.varA;
variableB = obj.varB;
}
});
// use the variables in this ajax request and do what you want
$.ajax(
{
url:'php_page_path/page.php',
data:"var1="+variableA+"&variableB="+vaiableB ,
type: 'post',
success: function(j){
// use the data from response
}
});
Please changes your code -
<?php
$myname="asim";
?>
<script type="text/javascript">
function submitform(myname){
alert(myname);
$.ajax({
type: 'POST',
data: {
action: 'whatever',
fileID: myname,
},
url: 'http://xyz.com/API/query.php'
});
}
</script>
HTML
<form id="myform" onsubmit="submitform('<?php echo $myname;?>')">
------
------
<input type="submit" name="submit_details">
</form>
This is my code and i want to pass javascript variable with ajax to php when i click submit button then the result doesn't show var_data variable from javascript What code is wrong?
This is edit order one before everybody help me
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#sub').click(function() {
var var_data = "Hello World";
$.ajax({
url: 'http://localhost/ajax/PassVariable.php',
type: 'GET',
data: { var_PHP_data: var_data },
success: function(data) {
// do something;
}
});
});
});
</script>
</head>
<body>
<input type="submit" value="Submit" id="sub"/>
<?php
$test = $_GET['var_PHP_data'];
echo $test;
?>
</body>
</html>
and this is source code now
<?php
if (isset($_GET['var_PHP_data'])) {
echo $_GET['var_PHP_data'];
} else {
?>
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
$(document).ready(function() {
$('#sub').click(function() {
var var_data = "Hello World";
$.ajax({
url: 'http://localhost/test.php',
type: 'GET',
data: { var_PHP_data: var_data },
success: function(data) {
// do something;
$('#result').html(data)
}
});
});
});
</script>
</head>
<body>
<input type="submit" value="Submit" id="sub"/>
<div id="result">
</body>
</html>
<?php } ?>
this statement if(isset($_GET['var_PHP_data'])) output false and then show Hello World What should i do to do for isset($_GET['var_PHP_data']) is true?
Your solution has PHP issues: you don't check if the data exists, and also, you don't do anything with the result. I've modified the script to do the following:
Check if the var_PHP_data var is set (in PHP, on the server).
If yes, just send a blank text response containing that data.
If no, then draw the form and everything else.
In the form, I've created a #result div.
Ajax response will be shown in this div.
Also make sure that you host the script at localhost and that it is called test.php. To make sure this is resilient, you can change the Ajax URL to
<?php echo $_SERVER['PHP_SELF'];?> to make sure that you'll hit the correct script.
<?php
if (isset($_GET['var_PHP_data'])) {
echo $_GET['var_PHP_data'];
} else {
?>
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js">
<script>
$(document).ready(function() {
$('#sub').click(function() {
var var_data = "Hello World";
$.ajax({
url: 'http://localhost/test.php',
type: 'GET',
data: { var_PHP_data: var_data },
success: function(data) {
// do something;
$('#result').html(data)
}
});
});
});
</script>
</head>
<body>
<input type="submit" value="Submit" id="sub"/>
<div id="result">
</body>
</html>
<?php } ?>
Try jQuery Form its this will help to solve many problems.
For you question: try url without domain name, add tags 'form', change event click to submit, add data type
what are the contents of PassVariable.php ? if is the same where you have they jquery bit wont work coz php will print all the page again, if the file is different try
success: function(data) {
alert('databack = '+ data);
}
Try placing your input into a form and attaching the ajax call to the form onsubmit event. The way it happens in the provided happen is when you click in the field, in which case it submits before you can write anything really.
$(document).ready(function() {
$('#brn').click(function() {
var var_data = "Hello World";
alert("click works");
$.ajax({
url: 'http://localhost/ajax/PassVariable.php',
type: 'GET',
data: { x: var_data },
success: function(data) {
alert(data);
}
});
});
});
change it to this code
then in PassVariable.php put
make button
<input type="button" id="btn" value="click me" />
it should work because it is very basic example. If it doesn't work check your console if there are any JavaScript errors and remove them.