简体   繁体   English

在图像上单击更改mysql值

[英]on image click change mysql value

I'am new in using Jquery/Ajax/JSon and i want to make a script that changes a value in mysql if a user clicks on one of the two icons. 我是使用Jquery / Ajax / JSon的新手,并且我想制作一个脚本,如果用户单击两个图标之一,则该脚本会更改mysql中的值。

I have searched for any idea's only i don't know exactly where to start. 我一直在寻找任何想法,但我不知道该从哪里开始。 I use smarty with slim and i have already a setup a page with datatables this is working great. 我将smarty与slim配合使用,并且我已经设置了一个带有datatables的页面,效果很好。

I have made into a datatable row 2 icons and what i want if it is possible that if a user clicks on one of the two icons that without leaving the page a value is getting updated. 我已经将数据表的第2行图标制成了,如果用户单击两个图标中的一个而不离开页面,则值将被更新,这是我想要的。 I have now added aa href like index.php/upd/del/1 for marking as deleted and index.php/upd/save/1 for saving. 我现在添加了aa href,例如index.php / upd / del / 1标记为已删除,并添加了index.php / upd / save / 1进行保存。

behind upd i have a function for reading save or del and the 1 is the idea. 在upd后面,我具有读取save或del的功能,而1就是这个主意。

Can someone give me a idea or some place where i can find something like this. 有人可以给我一个主意或某个我可以找到这样的地方的地方。

i hope that i can ask this here and thank you already for helping me with this 我希望我可以在这里问这个,并已经感谢您为我提供帮助

<a id="option1"><img src="image1.jpg" /></a>
<a id="option2"><img src="image2.jpg" /></a>

<script type="text/JavaScript">
$(function() {
    $("#option1").click(function() {
        $.post("index.php/upd/del/1", function(json) {
                if (json && json.status) {
                    alert("Change made!");
                } else {
                    alert("something failed!");
                }
            }
        );
    });
    $("#option2").click(function() {
        $.post("index.php/upd/save/1", function(json) {
                if (json && json.status) {
                    alert("Change made!");
                } else {
                    alert("something failed!");
                }
            }
        );
    });
});
</script>

Now, in your PHP Script: 现在,在您的PHP脚本中:

<?php
// In order for jquery to change the data we are returning to json, we should se the headers to json. 
// Also, it is important to notice that all the ajax request will work allways with utf-8
// so if you don't want to have a lot of problems with accents and special chars, use allways utf-8
header("Content-Type:application/json; Charset=utf-8");

// Make some really cool stuff with the data at mysql
// But if something went wrong:
if ($varToSetIfErrorOcurrs === true) {
    return json_encode(array ("status" => false));
}

return json_encode(array ("status" => true));

Some last considerations: 最后一些注意事项:

  • Allways use the html elements for what they were thinked: img to show images, not to click on them. 始终使用html元素来表达他们的想法:img用于显示图像,而不是单击它们。 a (anchors) to click on them. a(锚)以单击它们。 Or buttons to make some actions 或按钮进行一些操作
  • The JS is better to be on the bottom of your page, so all the DOM is already loaded when it is executed JS最好放在页面底部,因此执行时所有DOM都已加载
  • $(function() {}); $(function(){}); executes that piece of code once the DOM is ready (only the html, document.onready is executed once everything is loaded, including images, so it is slower) DOM准备就绪后执行该代码段(一旦加载了所有内容(包括图像,则仅执行html,document.onready),因此速度较慢)

Happy coding! 编码愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM