Simple php ajax html issue - php

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
}
});
}
)

Related

Always back to index page using tinymce with ajax

I want to add data using textarea tinymce with ajax post. but when I click the button and I see in firebug I look at the network tab after calling the ajax page to save the data and then automatically call to the index page. But this problem only happens when I publish to hosting with web domain. everything went smoothly while on localhost. please check my code below and give me explanation and code improvement. Thank you very much.
add.php
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<textarea class="tinymce" id="contentblog" name="contentblog"></textarea><br/>
<button type="button" id="AjaxSubmit" name="submit">Submit</button>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="plugin/tinymce/tinymce.min.js"></script>
<script type="text/javascript">
tinymce.init({
selector: "textarea.tinymce"
});
$(document).ready(function () {
$('#AjaxSubmit').click(function (){
var contentblog = tinymce.get("contentblog").getContent();
var form_data = new FormData();
form_data.append('contentblog', contentblog);
$.ajax({
url: 'add-ajax.php',
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function(data){
alert("Success !");
return true;
}
});
});
});
</script>
</body>
</html>
add-ajax.php
<?php
include_once('connection.php');
$contentblog = $_POST["contentblog"];
$data = array(
"contentblog" => $contentblog
);
$jsondata1 = json_encode($data);
$jsondata2 = json_decode($jsondata1, true);
$contentblog = $jsondata2['contentblog'];
$GetContentBlog = addslashes($contentblog);
$result = mysqli_query($mysqli, "INSERT INTO t_test(iscontents) VALUES('$GetContentBlog')");
if($result){
echo 'is success';
}
?>
You need to block the normal form submit of the html form:
$('#AjaxSubmit').click(function (event ){
event.preventDefault();
Without this the html form is submitting the form via normal http post and gets redirected!
See https://api.jquery.com/event.preventdefault/ for more information!

Very simple AJAX-Call does not work

This very simple AJAX-Call does not work on my localhost. I do have a Windows 10 Machine with XAMPP running. I tracked the packages, and the AJAX-Reqauest is not even sent to handle.php. What am i doing wrong here?
ajaxTest.php
<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js">
$(document).ready(function()
{
$.ajax(
{
type: 'post',
url: 'inc/handle.php',
success: function(data)
{
alert("Done!");
}
});
});
</script>
</head>
</html>
handle.php
<?php
echo "Test!";
?>
The problem is: include jquery on script tag and your code into another script tag
<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
$(document).ready(function()
{
$.ajax(
{
type: 'post',
url: 'inc/handle.php',
success: function(data)
{
alert("Done!");
}
});
});
</script>
</head>
</html>

.ajax() not sending data to php file

I am having a problem is retrieving data sent to a php file through .ajax() via jquery
Following is my html:
<!-- Jquery tute no 94 onwards -->
<html lang="en">
<head>
<meta charse="utf-8">
<title> jquery4 </title>
<link rel="stylesheet" type="text/css" href="jquery4.css"/>
</head>
<body>
<input id="lo" type="text"> </input>
<input id="ton" type="button" value="Load"> </input>
<div id="content"> </div>
<script type="text/javascript" src="jquery.js"> </script>
<script type="text/javascript" src="jquery4.js"> </script>
</body>
</html>
My jquery4.js is:
$(document).ready(function()
{
$('#ton').click(function()
{
var nm= $('#lo').val();
$.ajax({url: 'page.php', data1: 'name='+nm, success: function(data2)
{
$('#content').html(data2);
}
});
});
});
My page.php is:
<?php
if(isset($_GET['data1']))
{
echo $namer= $_GET['data1'];
}
?>
All the above files are in the same folder, and I have xampp installed.
I guess the error is somewhere in the jquery file where I call the
ajax() function
jQuery ajax doesn't take a data1 parameter. It takes a data parameter, which should be an object of name-value pairs.
$.ajax({
url: 'page.php',
data: {
data1: 'name=' + nm,
},
success: function(data2) {
$('#content').html(data2);
}
});
$.ajax({
type: "GET",
url: "page.php",
data: {
data1: 'name=' + nm,
}
,
success: function(data2) {
$('#content').html(data2);
}
});
Try this:
$(document).ready(function() {
$('#ton').click(function() {
var nm= $('#lo').val();
$.ajax({
url: 'page.php?name=' +nm,
success: function(data2) {
$('#content').html(data2);
}
});
});
});
You don't have to tell jQuery to use GET, as it defaults to that, if nothing else is specified.
So the ajax function does not take an argument called data1, but 'data', this is mostly used for other methods as POST, PUT and DELETE.
I prefer also sending GET requests with a normal query string, like the above example.
You can then check for get GET parameter with PHP, using $_GET['name']

jQuery AJAX method not working

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";

Pass javascript variable to php with ajax and the result doesn't show anything

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.

Categories