简体   繁体   English

使用AJAX编辑数据库条目并返回编辑的数据

[英]Edit Database entry with AJAX and return edited data

I have the following form: 我有以下表格:

 <?php 
        echo "<strong>".$Event_data_fetched['event_subject']."</strong><br />";

        echo   '<form id="Event_Editing" action="#" method="post">
                    Event-Type:<br />
                    <select name="edited_event_type">
                        <option>'.$Event_type.'</option>
                        <option>'.$Unselected_1.'</option>
                        <option>'.$Unselected_2.'</option>
                    </select><br /><br />

                    Event-Subject:<br />
                    <input type="text" name="edited_event_subject" value="'.$Event_data_fetched['event_subject'].'"/><br /><br />

                    <input id="Edit_Event" type="submit" name="Edit_Event" value="Speichern">
                </form>';               
    ?>

The form is meant to display data, from a row in the database and offer the user the option to edit said data. 该表格旨在显示数据库中一行的数据,并为用户提供编辑所述数据的选项。 And I already have a query that handles the data and rewrites the edited data in the Database, when the form is submitted: 当提交表单时,我已经有一个查询来处理数据并重写数据库中的编辑数据:

if(isset($_POST['Edit_Event'])){
        $New_Type_Value = $_POST['edited_event_type'];
        $New_Subject_Value = $_POST['edited_event_subject'];

        if($New_Type_Value == "Meet"){
            $New_Type_Value = 1;
        }
        else if($New_Type_Value == "Clubday"){
            $New_Type_Value = 2;
        }
        else if($New_Type_Value == "Surprise-Event"){
            $New_Type_Value = 3;
        }

        $edit_data_query = "UPDATE b6vjp_event
                            SET event_type_id = $New_Type_Value,
                                event_subject = '$New_Subject_Value'
                            WHERE id = $Event_id";

        mysqli_query($GLOBALS['connect'], $edit_data_query);
    }

Now I want to put this block of code into a separate .php File. 现在,我想将此代码块放入一个单独的.php文件中。

My ultimate goal is to post the form to an AJAX script, that then sends the data to a separate file, where it edits the DB with the new data. 我的最终目标是将表单发布到AJAX脚本中,然后将数据发送到一个单独的文件中,在其中使用新数据编辑数据库。 After it has been inserted the newly edited data should be given back and written back into the form. 插入后,应将新编辑的数据返回并写回到表单中。 I also need to post a variable with the ID of the row. 我还需要发布具有该行ID的变量。 Otherwise, the query won't know where to insert the edited data. 否则,查询将不知道在何处插入编辑后的数据。

I googled a bunch and tried a lot of things, but don't seem to find anything specifically working for me. 我在Google上搜索了一堆,尝试了很多东西,但似乎找不到适合我的任何东西。 I did stumble across a code that looks like a good start but not what I want to do: 我确实偶然发现了一个看起来不错的开始但不是我想做的代码:

 <script type='text/javascript'>
    /* attach a submit handler to the form */
    $("#Event_Editing").submit(function(event) {

    /* stop form from submitting normally */
    event.preventDefault();

    /* get the action attribute from the <form action=""> element */
    var $form = $( this ),
        url = $form.attr( 'action' );

    /* Send the data using post with element id name and name2*/
    var posting = $.post( url, { name: $('#name').val(), name2: $('#name2').val() } );

    /* Alerts the results */
    posting.done(function( data ) {
        alert('success');
    });
    });
</script>

My biggest Problem is that I'm not that good at AJAX. 我最大的问题是我不太擅长AJAX。 Does anybody know how I would go about doing this? 有人知道我会怎么做吗?

If your current code works fine without any issue then, 如果您当前的代码运行正常,没有任何问题,

you can just use echo json_encode($_POST); 您可以只使用echo json_encode($_POST); at the end of your code in PHP file to return data that you have updated in your DB. 在PHP文件中的代码末尾,以返回数据库中已更新的数据。

In your javascript code try, 在您的JavaScript代码中尝试,

posting.done(function( data ) {
       //Prits data you returned on console.
        console.log(data);
    });

You can set up the AJAX call like so: 您可以像这样设置AJAX调用:

<?php 
echo "<strong>".$Event_data_fetched['event_subject']."</strong><br />";

echo   '<form id="Event_Editing" action="#" method="post">
            Event-Type:<br />
            <select name="edited_event_type">
                <option>'.$Event_type.'</option>
                <option>'.$Unselected_1.'</option>
                <option>'.$Unselected_2.'</option>
            </select><br /><br />

            Event-Subject:<br />
            <input type="text" name="edited_event_subject" value="'.$Event_data_fetched['event_subject'].'"/><br /><br />

            <input id="Edit_Event" type="submit" name="Edit_Event" value="Speichern" onclick='editData();'>
        </form>';        

?>


<script>

    function editData(){
        var value = document.getElementById('Event_Editing').value;

        $.ajax({
            url : 'your-backend-file-with-DB-Query.php',
            method : 'POST',// OR GET
            data: {value:value},
            dataType: 'json',
            success:function(data) {

            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
                alert("responseText: "+xhr.responseText);
            }

        }); 

    }

</script>

Then in your backend file you can receive the values like so: 然后,在您的后端文件中,您可以收到如下所示的值:

<?php

/*CONNECTION TO DB*/

$value = $_POST['value'];

/*Now you can use this value in your Query and update the database*/

?>

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

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