简体   繁体   English

单击调用JavaScript的函数,然后重定向到php页面

[英]Click function calling javascript then redirecting to php page

I'd like to make an edit function for my posts. 我想对我的帖子进行编辑。 So far You can Post and See your post(s) from the admin page, which is operated by mySQL but not so long ago I wanted to make an edit button. 到目前为止,您可以从管理页面发布并查看您的帖子,该页面由mySQL操作,但是不久前我想创建一个编辑按钮。 The theory would be, when you click on the edit button javascript gets the id of the post element, then after running a mysql query where the id = PostID then it would redirect to a page, where you could edit the post. 从理论上讲,当您单击“编辑”按钮时,javascript将获取post元素的ID,然后在运行mysql查询(其中id = PostID)后,它将重定向至页面,您可以在其中编辑帖子。 The html inputs value's would be the datas read out from mysql, but the javascript just doesn't want to fire up. html输入值是从mysql读取的数据,但是javascript不想启动。

The post's php/html looks like this: 帖子的php / html看起来像这样:

    while($row = mysqli_fetch_array($run)) {
echo '<article id="post" data-postid="'.$row["id"].'">
    <ul>
        <li class="id">'.$row["id"].'</li>
        <li class="title">'.$row["title"].'</li>
        <li class="postedBy">'.$row["postedBy"].'</li>
        <li class="postedBy">'.$row["dDate"].'</li>
        <li class="prio">'.$row["priority"].'</li>
        <li class="icons"><div class="do" data-action="edit"><i class="fa fa-pencil-square-o"></i></div><div class="do" data-action="delete"><i class="fa fa-times"></i></div></li>
    </ul>
</article>';

Javascript looks like this ( it is called admin_edit.js ): Javascript看起来像这样(它称为admin_edit.js):

    $('#post').on('click', '.do', function(){
        var self = $(this); // cache $this
        var action = self.data('action'); // grab action data edit/delete
        var postid = parent.data('postid'); // grab post id from data-postid

        // edit action
        if (action == 'edit') {
            // send js to php
            $.post("../admin/post_edit.php", { postId: postid } );
        }
        // delete action
        else if (action == 'delete'){
            // send ajax request
            // Something.
        };

    });

To understand my folder structure: We are in the root/admin folder and on the root/admin/admin.php page. 要了解我的文件夹结构,请执行以下操作:我们位于root / admin文件夹中,并且位于root / admin / admin.php页面上。 The javascript is in the root/js folder and is called root/js/admin_edit.js. javascript位于root / js文件夹中,称为root / js / admin_edit.js。

Why doesn't the js want to fire up ? 为什么JS不想要启动?

Thanks in Advance ! 提前致谢 !

Your #post does not exist when you define its click event. 您的#post定义click事件时不存在。 You need to define the event when it already exists. 您需要在事件已经存在时对其进行定义。 You can register the event for #main , but you can also register the event in the callback of your request. 您可以为#main注册事件,但也可以在请求的回调中注册事件。 It is better to register it in the callback if possible. 如果可能的话,最好在回调中注册它。

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

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