Menu
Hi,
I am trying to learn php and ajax. Cannot figure out why this code not working?This message is only example.
I wish to run php script with ajax using button1 click.If someone have enough time to show me right way.Thank you.
[code]$(function(){
$(“#button1”).click(function(){
$.ajax({
url: ‘index.php’,
type: ‘POST’,
data: { action: “1”},
success: function(response) { console.log(response); }
});
});
});
[code]
<?php
if (isset($_POST[‘action’ == “1”])) {
$message = “Hello world”;
echo “<script type=’text/javascript’>alert(‘$message’);</script>”;
}
?>
>@Ahmo#1615369 if (isset($_POST['action' == "1"])) {
<i>
</i>if(isset($_POST['action']) and $_POST['action'] == "1") {
if (isset($_POST['action']) && $_POST['action'] == "1") {
$message = "Hello world";
echo "<script type='text/javascript'>alert('$message');</script>";
}
> However echoing javascript and executing it on the client may be the reason of confusion and issues. I recommend not to do so.
<?php<br/>
//<br/>
$dir = './osaasto1/sunnuntai/';<br/>
// add directory as a data parameter to the select<br/>
echo '<select name="listBox1" id="listBox1" class="listBox" multiple data-dir="' . $dir . '" >';<br/>
// add filesnames from directory in to select (listBox1)<br/>
$files = glob('./osaasto1/sunnuntai/<EM>*.*</EM>');<br/>
foreach ($files as $file) {<br/>
echo "<option>".pathinfo($file, PATHINFO_BASENAME)."</option>";
}<br/>
?><br/>
</select>
<select id="listBox1" name="listBox1"></select>
<?php
//
$dir = './osaasto1/sunnuntai/';
// add directory as a data parameter to the select
// echo '<select name="listBox1" id="listBox1" class="listBox" multiple data-dir="' . $dir . '" >';
// echo filesnames from directory as options
$files = glob('./osaasto1/sunnuntai/.');
foreach ($files as $file) {
echo "<option>".pathinfo($file, PATHINFO_BASENAME)."</option>";
}
?>
$.ajax({
url: 'index.php',
type: 'POST',
data: { action: "1"},
success: function(response) {
console.log(response);
// add response (options) to listbox:
$('#listBox1').append(response);
}
});
echo '<select name="listBox1" id="listBox1" class="listBox" multiple data-dir="' . $dir . '" >';
$(function(){
<br/>
canvas = document.getElementById("canvas")<br/>
context = canvas.getContext("2d")<br/>
document.getElementById('listBox1').addEventListener('change', function(e) {<br/>
context.clearRect(0, 0, context.canvas.width, context.canvas.height);<br/>
img = new Image();<br/>
// compose relative path of the image and assign it to the src attribute<br/>
img.src = this.getAttribute('data-dir') + this.options[this.selectedIndex].value; // Hire is 'data-dir'<br/>
img.onload = () => {<br/>
var scale = Math.min(canvas.width / img.width, canvas.height / img.height);<br/>
// get the top left position of the image<br/>
var x = (canvas.width / 2) - (img.width / 2) * scale;<br/>
var y = (canvas.height / 2) - (img.height / 2) * scale;<br/>
context.drawImage(img, x, y, img.width * scale, img.height * scale);
// unselect rest of listBoxes<br/>
$("#listBox6").val([]);<br/>
$("#listBox5").val([]);<br/>
$("#listBox4").val([]);<br/>
$("#listBox3").val([]);<br/>
$("#listBox2").val([]);<br/>
$("#listBox7").val([]);
} <br/><br/>
e.preventDefault(); <br/><br/>
});
});
<?php
//
$dir = './osaasto1/sunnuntai/';
// add directory as a data parameter to the select
// echo '<select name="listBox1" id="listBox1" class="listBox" multiple data-dir="' . $dir . '" >';
// echo filesnames from directory as options
$files = glob('./osaasto1/sunnuntai/.');
foreach ($files as $file) {
echo '<option data-dir="' . $dir . '">'.pathinfo($file, PATHINFO_BASENAME).'</option>';
}
?>
img = new Image();
// compose relative path of the image and assign it to the src attribute
img.src = this.options[this.selectedIndex].getAttribute('data-dir') + this.options[this.selectedIndex].value; // Hire is 'data-dir'
[code]$files = glob($dir .'*');[]
<?php
<br/>
<i> </i><CODE>if (isset($_POST['action']) && $_POST['action'] == "1") {
<i> </i> $dir = './osaasto1/sunnuntai/';
<i> </i> $files = glob($dir .'*');
<i> </i> //$files = glob('./osaasto1/sunnuntai/*.*');
<i> </i> foreach ($files as $file) {
<i> </i> echo '<option data-dir="' . $dir . '">'.pathinfo($file, PATHINFO_BASENAME).'</option>';</CODE>
}<br/>
}<br/>
?>
$(function(){
<br/>
canvas = document.getElementById("canvas")<br/>
context = canvas.getContext("2d")<br/>
document.getElementById('listBox1').addEventListener('change', function(e) {<br/>
context.clearRect(0, 0, context.canvas.width, context.canvas.height);<br/>
img = new Image();<br/>
// compose relative path of the image and assign it to the src attribute<br/>
img.src = this.options[this.selectedIndex].getAttribute('data-dir') + this.options[this.selectedIndex].value; // Hire is 'data-dir'<br/>
img.onload = () => {<br/>
var scale = Math.min(canvas.width / img.width, canvas.height / img.height);<br/>
// get the top left position of the image<br/>
var x = (canvas.width / 2) - (img.width / 2) * scale;<br/>
var y = (canvas.height / 2) - (img.height / 2) * scale;<br/>
context.drawImage(img, x, y, img.width * scale, img.height * scale);
<br/>
<br/>
//strech image<br/>
//context.drawImage(img, (canvas.width / 2) - (img.width / 2), 0, img.width, canvas.height);
<br/>
// unselect rest of listBoxes<br/>
$("#listBox6").val([]);<br/>
$("#listBox5").val([]);<br/>
$("#listBox4").val([]);<br/>
$("#listBox3").val([]);<br/>
$("#listBox2").val([]);<br/>
$("#listBox7").val([]);
} <br/><br/>
e.preventDefault(); <br/><br/>
});
...
<select id="listBox1"></select>
<canvas id="canvas"></canvas>
<script>
$.ajax({
// adjust this according your filename:
url: 'thread319-ajax-webdev-2.php',
type: 'POST',
data: { action: "1" },
success: function (response) {
console.log(response);
// add response (options) to listbox:
$('#listBox1').append(response);
}
});
</script>
<script>
$(function () {
canvas = document.getElementById("canvas")
context = canvas.getContext("2d")
document.getElementById('listBox1').addEventListener('change', function (e) {
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
img = new Image();
// compose relative path of the image and assign it to the src attribute
img.src = this.options[this.selectedIndex].getAttribute('data-dir') + this.options[this.selectedIndex].value; // Hire is 'data-dir'
img.onload = () => {
var scale = Math.min(canvas.width / img.width, canvas.height / img.height);
// get the top left position of the image
var x = (canvas.width / 2) - (img.width / 2) * scale;
var y = (canvas.height / 2) - (img.height / 2) * scale;
context.drawImage(img, x, y, img.width * scale, img.height * scale);
<i> </i> // unselect rest of listBoxes
<i> </i> // $("#listBox6").val([]);
<i> </i> // $("#listBox5").val([]);
<i> </i> // $("#listBox4").val([]);
<i> </i> // $("#listBox3").val([]);
<i> </i> // $("#listBox2").val([]);
<i> </i> // $("#listBox7").val([]);
<i> </i> }
<i> </i> e.preventDefault();
<i> </i> });
<i> </i> });
<?php
// adjust this according to your path:
// $dir = './osaasto1/sunnuntai/';
$dir = './images/';
$files = glob($dir . '*');
foreach ($files as $file) {
echo '<option data-dir="' . $dir . '">' . pathinfo($file, PATHINFO_BASENAME) . '</option>';
}
?>
Note that you have to adjust the directory $dir in the PHP script and the filename of your PHP script in the ajax call.[code] $(function(){
canvas = document.getElementById("canvas")
context = canvas.getContext("2d")
document.getElementById('listBox1').addEventListener('change', function(e) {
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
img = new Image();
// compose relative path of the image and assign it to the src attribute
img.src = this.options[this.selectedIndex].getAttribute('data-dir') + this.options[this.selectedIndex].value; // Hire is 'data-dir'
img.onload = () => {
var scale = Math.min(canvas.width / img.width, canvas.height / img.height);
// get the top left position of the image
var x = (canvas.width / 2) - (img.width / 2) * scale;
var y = (canvas.height / 2) - (img.height / 2) * scale;
context.drawImage(img, x, y, img.width * scale, img.height * scale);
//strech image
//context.drawImage(img, (canvas.width / 2) - (img.width / 2), 0, img.width, canvas.height);
// unselect rest of listBoxes
$("#listBox6").val([]);
$("#listBox5").val([]);
$("#listBox4").val([]);
$("#listBox3").val([]);
$("#listBox2").val([]);
$("#listBox7").val([]);
}
e.preventDefault();
});`[code]
[code]
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<select id="listBox1" name="listBox1"></select>
<canvas id="canvas" style="border-style: double"></canvas>
<input type="submit" id="button1">
<?php
// adjust this according to your path:
$dir = './images/';
$files = glob($dir . '*');
foreach ($files as $file) {
echo '<option data-dir="' . $dir . '">' . pathinfo($file, PATHINFO_BASENAME) . '</option>';
}
?>
<script>
$(function(){
$("#button1").click(function(e){
$.ajax({
// adjust this according your filename:
url: 'index.php',
type: 'POST',
data: { action: "1" },
success: function (response) {
console.log(response);
// add response (options) to listbox:
$('#listBox1').append(response);
}
});
e.preventDefault();
});
});
</script>
<script>
$(function () {
canvas = document.getElementById("canvas")
context = canvas.getContext("2d")
document.getElementById('listBox1').addEventListener('change', function (e) {
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
img = new Image();
// compose relative path of the image and assign it to the src attribute
img.src = this.options[this.selectedIndex].getAttribute('data-dir') + this.options[this.selectedIndex].value; // Hire is 'data-dir'
img.onload = () => {
var scale = Math.min(canvas.width / img.width, canvas.height / img.height);
// get the top left position of the image
var x = (canvas.width / 2) - (img.width / 2) * scale;
var y = (canvas.height / 2) - (img.height / 2) * scale;
context.drawImage(img, x, y, img.width * scale, img.height * scale);
// unselect rest of listBoxes
// $("#listBox6").val([]);
// $("#listBox5").val([]);
// $("#listBox4").val([]);
// $("#listBox3").val([]);
// $("#listBox2").val([]);
// $("#listBox7").val([]);
}
e.preventDefault();
});
});
[code]
$dir = './images/';
</C><br/>
Previously you used a different one:<br/>
<C>
$dir = './osaasto1/sunnuntai/';
`<select>...</select>
<canvas id="canvas" style="border-style: double"></canvas>
<input type="submit" id="button1">
<?php
if (isset($_POST['action']) && $_POST['action'] == "1") {
$dir = './images/';
$files = glob($dir . '*');
foreach ($files as $file) {
echo '<option data-dir="' . $dir . '">' . pathinfo($file, PATHINFO_BASENAME) . '</option>';
}
}
?>
<select id="listBox1" name="listBox1"></select>
$(function(){
$("#button1").click(function(e){
$("#listBox1").val([]);
$.ajax({
url: 'index.php',
type: 'POST',
data: { action: "1"},
success: function(response) {
console.log(response);
// add response (options) to listbox:
$('#listBox1').append(response);
}
});
e.preventDefault();
});
});
$(function(){
<br/>
canvas = document.getElementById("canvas")
context = canvas.getContext("2d")
document.getElementById('listBox1').addEventListener('change', function(e) {
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
img = new Image();
// compose relative path of the image and assign it to the src attribute
img.src = this.options[this.selectedIndex].getAttribute('data-dir') + this.options[this.selectedIndex].value;
img.onload = () => {
var scale = Math.min(canvas.width / img.width, canvas.height / img.height);
// get the top left position of the image
var x = (canvas.width / 2) - (img.width / 2) * scale;
var y = (canvas.height / 2) - (img.height / 2) * scale;
context.drawImage(img, x, y, img.width * scale, img.height * scale);
<br/>
} <br/>
e.preventDefault(); <br/>
});
0.1.9 — BETA 5.10