i got a website with some buttons and a camerastream which shows a coffee machine. with php function i'm able to set the machine on/off via TCP/IP messages. Now i would like to send on/off commands over button, without refreshing the website. After reading some similar threads, i recognized that the only way is using AJAX.
After pressing Button the Page doesnt refresh but it seems that something in backround get done. But my "Test" String doesnt get show.
Does anyone know whats wrong with my code?
Thanks in advance!
ajax.php
<?php
if($_POST['action'] == 'call_this') {
echo 'TEST';
}
?>
buttonClick.js
function myAjax() {
$.ajax({
type: "POST",
url: 'ajax.php',
data:{action:'call_this'},
success:function(html) {
alert(html);
}
});
}
index.php
<html>
<head>
<script src="jquery-1.12.0.min.js"></script>
<script language="javascript" type="text/javascript" src="buttonClick.js"></script>
</head>
<body>
....
....
<form>
<input type="submit" name="submit" value="SET ON" onClick="myAjax()">
</form>
....
....
</body>
</html>
Modified Mouad Nejjari code as below and this works
index.php
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js"></script>
</head>
<body>
<button id="btnTest">Click</button>
<script>
$(document).ready(function () {
$('#btnTest').click(function () {
$.ajax({
type: "POST",
url: 'ajax.php',
data:{action:'call_this'},
success:function(html) {
alert(html);
}
});
})});
</script>
</body>
</html>
ajax.php
<?php
if($_POST['action'] == 'call_this') {
echo 'TEST';
}
?>
You are using the HTML element input with a type set to submit, this means that on click the page assumes you want to be redirected to the handling page URL normally supplied in the Form tag. You can use the javascript method of PreventDefault() or you can change the element from a form submit to a link like this.
<html>
<head>
<script src="jquery-1.12.0.min.js"></script>
<script language="javascript" type="text/javascript" src="buttonClick.js"></script>
</head>
<body>
....
....
SET ON
....
....
</body>
</html>
A link with a javascript href will not try and redirect. Your example left the page before waiting for the php script to return.
Try This, ajax.php :
<?php
if($_POST['action'] == 'call_this') {
echo 'TEST';
}
?>
buttonClick.js :
$(document).ready(function()
{
$("#form1").submit(function (e) {
e.preventDefault();
$.ajax({
type: "POST",
url: 'ajax.php',
data:{action:'call_this'},
success:function(html) {
alert(html);
}
});
})
})
index.php :
<html>
<head>
<script src="jquery-1.12.0.min.js"></script>
<script language="javascript" type="text/javascript" src="buttonClick.js"></script>
</head>
<body>
....
....
<form id="form1">
<input type="submit" name="submit" value="SET ON">
</form>
....
....
</body>
</html>
Related
I trying to print the data which was entered by the user, and transfer that through ajax and trying to print or echo it with the help of php code, but I could not do that. enter code here
this is my code:
<html>
<head>
<title>
test ajax
</title>
<script src="jquery.js"></script>
</head>
<body>
<h2>test button</h2><br>
<form>
<input type="text" id="a"><br>
<input type="button" id="b" value="display">
</form>
<script>
$(document).ready(function() {
$('#b').click(function() {
let a = $('#a').val();
alert(a);
$.ajax({
type: "POST",
url: "same_file.php",
data: {
c: a
},
success: function() {}
});
});
});
</script>
</body>
</html>
<?php
extract($_POST);
if(isset($_POST["c"])) {
echo $_POST["c"];
}
?>
I think that, I have the problem with the php code. please give me any solution.
Try it like this
This is your Html
<html>
<head>
<title>
test ajax
</title>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<h2>test button</h2><br>
<form>
<input type = "text" id = "a" > <br>
<input type = "button" id = "b" value = "display" >
</form>
</body>
</html>
<script>
$(document).ready(function(){
$('#b').click(function() {
let a = $('#a').val();
//alert(a);
$.ajax({
type: "POST",
url: "same_file.php",
data: {c: a},
success: function(response) {
alert(response);
}
});
});
});
</script>
This is your same_file.php
<?php
//extract($_POST); code works even without this
if(isset($_POST["c"])) {
echo $_POST['c'];
}
?>
Not sure if your jquery library call is ok so I called the one on the web instead. Also your success:function() was not doing anything which is why you didn't receive anything.
Also always format your code with some indention so that it can be easier to read and comprehend by others.
You probably want to echo before the body is rendered.
But in true, unless you need to process the data the user sends, you don't need PHP at all.
And in truth, you probably don't need jQuery as well, fetch() works just fine.
$(document).ready(function() {
$('#b').click(function() {
let a = $('#a').val();
alert(a);
$.ajax({
type: "POST",
url: "same_file.php",
data: {
c: a
},
success: function() {}
});
});
});
<?php
if (isset($_POST["c"]))
{
// print/echo user data send from form.
echo $_POST["c"];
// stop the script from rendering the html below.
exit();
}
?>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<h2>test button</h2><br>
<form>
<input type="text" id="a"><br>
<input type="button" id="b" value="display">
</form>
<!-- add script element with js code here -->
</body>
</html>
I have created a button "clear all",
which sends data to 'data.php' at the click of the button via ajax
</head>
<body>
<script type="text/javascript">
function klick(id){
$.ajax({
type:"POST",
url:"data.php",
data:{id:id},
success:function(resp){
$('#w3').html(resp);
}
});
}
</script>
shop
<br><br>
<button type='button'onclick= " klick()"/>clear all</button>
</body>
</html>
and this is my data.php page,
so what i want to do is overwriting $ _SESSION ['warenkorb'] with an empty array If the variable from the call is present in the "data.php" page.
Something like this: $_SESSION['warenkorb']=array();
<html>
<head>
<title>DATA</title>
<script src="https://www.novastorm.de/js/jquery-1.11.3.min.js"></script>
</head>
<body>
<?php
include 'produkte.php';
if(!isset($_SESSION['warenkorb'])){
$_SESSION['warenkorb']=array();
}
if(isset($_POST['id'])){
foreach($a as $key =>$value){
if($value['id']==$_POST['id']){
$_SESSION['warenkorb'][]= $_POST['id'];
}
}
}
print_r(count($_SESSION['warenkorb']));
?>
</body>
</html>
Hi I am very new to JavaScript, looking for some help. I have a form, shown below. When the user gives input and clicks submit, I want to call a php file (shown below also) to be invoked and return a hello. Here is the code I have tried, though it is not working as intended.
<html>
<head>
<script type='text/javascript' src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
</head>
<body>
<form>
<input id="name-typed" type="text" />
<input id="name-submit" type="submit" value="submit" onclick="post()"/>
</form>
<div id="result"></div>
</body>
<script>
function post() {
var hname = $('input#name-typed').val();
$.post('dat.php',{name: hname},function(data) {
$('div#result').html(data);
});
}
</script>
</html>
The PHP file dat.php looks like this:
<?php echo "hello";?>
As in my first paragraph, can anyone suggest any changes I should make?
Try this:
you can change dat.php to say:
<?php
$name = $_POST['name'];
echo "Hello ".$name;
?>
And then your html file would be:
Notice that the button type is changed to a normal button so that it just execute the function rather than submitting the form.
<html>
<head>
<script type='text/javascript' src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
</head>
<body>
<form>
<input id="name-typed" type="text" />
<button type="button" onclick="post()">Submit</button>
</form>
<div id="result"></div>
</body>
<script>
function post() {
$.ajax({
type: "POST",
url: "dat.php",
data: { name: $('#name-typed').val()
},
success: function(response) {
$('#result').html(response);
}
});
}
</script>
</html>
I haven't test but something like this:
$('form').submit(function(){
post()
return false;//block the reload of the page
})
In fact I think you just forget the return false. remvove the onclick if you use my answer
I need to send some data by using the kill button and get the item back. but it not works. the button with ajax function.
4 files: index.php, script.js, actionplay.php, actionpickup.php.
index.php
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="script.js"></script>
<button type="submit" id="submit_buttonplay">Kill</button>
<div id="show"><span2></span2></div>
script.js
function button(name,url) {
$('#'+name).click(function(){
$.ajax({
url:url+'.php',
success: function(html){
$('#'+'show').after(html);
}
});
return false;
});
}
$(function(){
button('submit_buttonplay','actionplay');
});
$(function() {
$('#'+'submit_buttonpickup').click(function(){
$.ajax({
url: 'actionpickup.php',
success: function(data) {
if (data == "Picked!") {
$('button').hide();
$('span2').text('Picked!').show().fadeOut(5000);
} else {
$('#'+'show').after(data);
}
}
});
return false;
});
})
actionplay.php
<?php
echo "<button type=\"submit\" id=\"submit_buttonpickup\">ITEM</button>";
?>
actionpickup.php
<?php
echo "Picked!";
?>
they not works, so I try using test.php to do the job of index.php.
test.php
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="script.js"></script>
<button type="submit" id="submit_buttonpickup">ITEM</button>
<div id="show"><span2></span2></div>
the button works.
I don't know why.
I clicked the link in index.php, it show me the same button:
<button type="submit" id="submit_buttonpickup">ITEM</button>
but not works, test.php show me the button can work,
how can I solve this problem?
i want to replace result from readmore.php into <div id='box'> base on id_pages at index.php after click <a class=readmore> Read More </a>.
i tired to try how to get value from readmore.php using ajax.
this's my code
index.php
<html>
<head>
<link rel='stylesheet' type='text/css' href='style.css'>
<title> Home Page </title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script>
$('.readmore').click(function(){
$.ajax({
type: "POST",
url: "readmore.php",
data: dataString,
success: function(returnedData) {
$( '#daleman' ).html(returnedData);
}
});
})
</script>
</head>
<body>
<?php
error_reporting(E_ERROR | E_PARSE);
include "koneks.php";
$db = new database;
$db->connectMYSQL();
//* Memulai Tampilan Menu
echo"
<div id='menu'>
<a href='manage.php'>Manage Blog</a>
</div>
";
//* Memulai Tampilan Page
$arraypage = $db->tampilPage();
foreach($arraypage as $data) {
echo"
<input type=hidden id=idnya name='idnya' value=".$data['id_pages'].">
<div id='boxpage'>
<div id='alasatas'>
</div>
<div id='alasjudul'>
".$data['judul']."
</div>
<div>
<div id='alasfoto'>
<img height='200px' src='pict/".$data['foto']."'>
</div>
<div id='alasisi'>
<div id='daleman'>
<br>
".$data['deskripsi']."
</div>
</div>
</div>
<div id='alasketerangan'>
<center>Tanggal dibuat : ".$data['tgl_dibuat']." Label : ".$data['label']." <a class='readmore' >Read More</a> </center>
</div>
</div>
</body>
</html>
";
}
?>
this my readmore.php
<?php
include "koneks.php";
$dbi = new database;
$dbi->connectMYSQL();
echo"
$_POST[idnya]
";
?>
i need help meybe to fix my code, my thanks
There are certain things missing in the code you've posted. Lets just simplify the code a little bit with some modifications.
index.php
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script> <!-- Added jQuery Reference -->
</head>
<body> <!-- Added body tag -->
<?php
echo "<input type='hidden' id='idnya' value='value_posted_for_idnya'><div id='box'> Replace Me </div> <a class='readmore' href='javascript:void(0)'>ReadMore</a>";
?>
<script>
$(document).ready(function(){ // Wrapped the script in $(document).ready function
$('.readmore').click(function(){
$.ajax({
type: "POST",
url: "readmore.php",
data: "idnya=" + $("#idnya").val(), //say you have only one field to post for now
success: function(returnedData) {
$('#box').html(returnedData);
}
});
})});
</script>
</body>
</html>
readmore.php
Can stay the same.
Try the code above and see what you get.
EDIT
If you have multiple fields to post with ajax function you can use serializeArray() method.
Simply wrap your fields with in a form tag as follows
<form id="frm">
...form controls
</form>
This time you have to specify name attributes in order for serializeArray() to work.
Then your new ajax function becomes
$(document).ready(function(){
$('.readmore').click(function(){
$.ajax({
type: "POST",
url: "readmore.php",
data: $("#frm").serializeArray(), //get all name-value pairs within form
success: function(returnedData) {
$('#box').html(returnedData);
}
});
})});
</script>
its possible that click function is not executing beacause you dont prevent default anchor action
<script>
$('.readmore').click(function(event){
event.preventDefault();
$.ajax({
type: "POST",
url: "readmore.php",
data: dataString,
success: function(returnedData) {
$('#box').html(returnedData);
}
});
})
</script>
I have modified your code a little try doing this:
<script>
$('.readmore').click(function(){
var idnya = $('#idnya').val();
$.ajax({
type: "POST",
url: "readmore.php?idnya="+idnya,
data: dataString,
success: function(returnedData) {
$('#box').html(returnedData);
}
});
})
</script>
and in your readmore.php file use $_GET['idnya'] to get this value and use it in your query.