使用AJAX将PHP表单提交到MYSQL数据库

时间:2022-09-25 19:05:46

I have a PHP page that displays records from a MYSQL database in a table. I want to be able to edit the record in the table cell by clicking on the cell. The process I picture would go like this:

我有一个PHP页面,它在表格中显示来自MYSQL数据库的记录。我希望能够通过单击单元格来编辑表格单元格中的记录。我想象的过程如下:

1) onclick() event on table cell triggers ajaxfunction()
2) ajaxfunction() sends cell data to php page which querys the database and displays drop down list of options.
3) user selects option from drop down list
4) onchange() event submits form to database
5) table cell or page is refreshed to show the current table cell data. (not currently in my existing code)

1)表格单元格上的onclick()事件触发ajaxfunction()2)ajaxfunction()将单元格数据发送到查询数据库并显示选项下拉列表的php页面。 3)用户从下拉列表中选择选项4)onchange()事件将表单提交给数据库5)表格单元或页面被刷新以显示当前表格单元格数据。 (目前不在我现有的代码中)

Right now I've got two PHP pages ac_sched.php and ModSched.php.

现在我有两个PHP页面ac_sched.php和ModSched.php。

Here is the structure of each page:

以下是每个页面的结构:

ac_sched.php:

  • PHP created table displaying MYSQL records
  • PHP创建的表显示MYSQL记录

  • ajaxfunction to query the database for available cell options
  • ajax函数查询数据库中的可用单元格选项

ModSched.php:

  • PHP code to select the available cell options from the database and create dropdown.
  • PHP代码,用于从数据库中选择可用的单元格选项并创建下拉列表。

  • PHP code to update the database with the selected option from the dropdown.
  • 使用下拉列表中的选定选项更新数据库的PHP代码。

Here are the relevant sections of code from each page:

以下是每个页面的相关代码部分:

ac_sched.php:

<script>
function ACSchedQuery(reg)
    {
    if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
        }
    xmlhttp.onreadystatechange=function()
        {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
            document.getElementById(reg).innerHTML=xmlhttp.responseText;
            }
        }
    xmlhttp.open("GET","../process/ModSched.php?ModSched_Reg="+reg,true);
    xmlhttp.send();
    }
</script>

<table>

<tr>
<td id="ABC-Day-3" onclick="ACSchedQuery(this.id)"></td>
</tr>

......rest of table here......
</table>


ModSched.php:

// This is to request the options from the database  
if(isset($_GET['ModSched_Reg']))
    {
    $ModSched_Array = explode("-",$_GET["ModSched_Reg"]);
    $ModSched_Reg = $ModSched_Array[0];
    $ModSched_Day = $ModSched_Array[1];
    $ModSched_ID = $ModSched_Array[2];

    $sql4 = ("SELECT * FROM aircraft_sched_options WHERE SchedOpt_ID != 0");

    if(!$result_sql4 = $mysqli->query($sql4))
        {
        echo QueryCheck("getting the schedule options ","from the aircraft sched options",$mysqli);
        }
    ?>
    <form action="../process/ModSched.php" method="post">// I did try: action=""
        <table cellpadding="0" cellspacing="0" border="0">

        <tr>
        <td>
    <?php
    echo "<select name=\"ModSched_ID\">";

    while($SchedOpt = $result_sql4->fetch_assoc())
        {
        echo "<option value=\"" . $ModSched_Reg . "-" . $ModSched_Day .  "-" . $SchedOpt['SchedOpt_ID'] . "\" onchange=\"formSubmit(this.form)\"";
        if($SchedOpt['SchedOpt_ID'] == $ModSched_ID)
            {
            echo " selected=\"selected\"";
            }
        echo ">" . $SchedOpt['SchedOpt_Name'] . "</option>";
        }

    echo "</select>";
    ?>
        </td>
        </tr>

        </table>
    </form>
    <?php
    }  

// This is to submit the selected option to the database
if(isset($_POST['ModSched_ID']))
    {
    $ModSched_Array = explode("-",$_POST["ModSched_ID"]);
    $ModSched_Reg = $ModSched_Array[0];
    $ModSched_Day = $ModSched_Array[1];
    $ModSched_ID = $ModSched_Array[1];

    $sql4 = ("UPDATE aircraft_status SET $ModSched_Day = $ModSched_ID WHERE AC_Reg = $ModSched_Reg");

    if(!$result_sql4 = $mysqli->query($sql4))
        {
        echo QueryCheck("updating the aircraft schedule ","into the aircraft sched",$mysqli);
        }

    }

I know that is a lot of code, but I think it's all needed in order to show the whole story.
I think if someone just takes a look at the code structure that I have, that will tell me if I'm on the right track or not, that would be a big help. Here are the questions I have regarding my code:

我知道这是很多代码,但我认为只需要显示整个故事。我想如果有人只是看看我的代码结构,那将告诉我,如果我在正确的轨道上,那将是一个很大的帮助。以下是我对我的代码的问题:

1) Is the form to submit the changes in the right place? - I previously had a second ajaxfunction() to submit the selected option to the form submit page. This didn't work, but it could have been my code and not the structure itself.

1)表格是否在正确的地方提交更改? - 我之前有第二个ajaxfunction()将选定的选项提交到表单提交页面。这不起作用,但它可能是我的代码,而不是结构本身。

2) Should I have the form submit code on its own page?

2)我应该在自己的页面上提供表单提交代码吗?

I have just started to use AJAX this week, so I could be way off the mark here. Any help is greatly appreciated.

我本周刚刚开始使用AJAX,所以我可能会在这里脱颖而出。任何帮助是极大的赞赏。

1 个解决方案

#1


0  

You can implement some framework like cakephp or symphony, this framework can help you in the process to learn this language and good practices.

您可以实现像cakephp或symphony这样的框架,这个框架可以帮助您在此过程中学习这种语言和良好实践。

#1


0  

You can implement some framework like cakephp or symphony, this framework can help you in the process to learn this language and good practices.

您可以实现像cakephp或symphony这样的框架,这个框架可以帮助您在此过程中学习这种语言和良好实践。