简体   繁体   English

在多个表单ajax中提交一种表单

[英]Submitting one form amongst multiple forms ajax

I've been a lurker for some time, but this is my first post (and motivated me to sign up), I've searched countless posts, but can't find a solution that best works. 我已经潜伏了一段时间,但这是我的第一篇博文(并激励我注册),我搜索了无数博文,但找不到最有效的解决方案。 This is part of a project where users search a database and have the option to add returned results to a saved location (posting the id, resid, to a separate mysql table). 这是项目的一部分,用户可以在该项目中搜索数据库,并可以选择将返回的结果添加到保存的位置(将id,resid发送到单独的mysql表中)。

The search function is in a separate php file, the relevant parts of the code are below (I've stripped out a lot of the formatting): 搜索功能在一个单独的php文件中,代码的相关部分在下面(我已经删除了很多格式):

// Output HTML formats
    $html ='<tr >
      <form id="myForm" method="post">
        <input type="hidden" id="resid" name="resid" value="ther3sidd">
        <input type="hidden" id="lesid" name="lesid" value="liD1">
        <input type="button" id="submitFormData" onclick="SubmitFormData()" value="Submit" />
      </form>
         </tr>';

The values "ther3sidd" and "liD1" get replaced with values unique to the item returned by the search, and are numerical values. 值“ ther3sidd”和“ liD1”被替换为搜索返回的项目所独有的值,并且是数值。 There will also be multiple of the forms returned on the user facing page all with the same id of myForm (but different lesid and resid). 在面向用户的页面上还将返回多个具有相同myForm id(但不同的lesid和resid)的表单。

The search results are outputted to the page the user is on and a button can be pressed to add the result to a mysql table using the script below (stored in the page the user is on). 搜索结果将输出到用户所在的页面,并且可以使用以下脚本(存储在用户所在的页面中)将其按下按钮以将结果添加到mysql表中。

function SubmitFormData() {       
    var lesid = $('input[name="lesid"]').val();
    var resid = $('input[name="resid"]').val(); 
    var dataString = {lesid: lesid, resid: resid}

    $.ajax({
        type: "POST",
        url: 'lesresadd.php',
        data: dataString, 
    })} 

Below is the php file lesresadd.php (without the connection info) 以下是php文件lesresadd.php(无连接信息)

$lessonid = $_POST['lesid'];
$resourceid = $_POST['resid'];

$sql = "INSERT INTO lessons_resources (lesson_id, resource_id)
VALUES ('$lessonid', '$resourceid')";

$result = $conn->query($sql)

The code works fine and can submit data to the mysql table, however, the main issue is that the values for resid and lesid are for the first result from the search function (and not the one selected with the button click). 该代码可以正常工作,并且可以将数据提交到mysql表,但是,主要问题是resid和lesid的值是搜索功能的第一个结果(而不是单击按钮选择的结果)。 I know that I need to use 'this' somewhere on the variable, however, I am not sure of the correct syntax. 我知道我需要在变量的某处使用“ this”,但是,我不确定语法是否正确。 For example, I have tried the following (and many variants on it): 例如,我尝试了以下方法(及其上的许多变体):

var lesid = $this().('input[name="lesid"]').val();

But this does not work. 但这是行不通的。 Maybe, there is more I need to do to the code, and it isn't as simple as just adding 'this' somewhere. 也许,我还需要对代码做更多的事情,它并不只是在某处添加“ this”那么简单。 I am reasonably new to AJAX and entirely self taught from visiting sites just like stack overflow. 我对AJAX相当陌生,并且像堆栈溢出一样完全是从访问站点自学的。 Any advice would be greatly appreciated. 任何建议将不胜感激。

It's not the neatest solution, but here goes: 这不是最整洁的解决方案,但这里有:

$html = "";
$i = 1;
while($r = mysql_fetch_object($res))
{
    $html .='<tr>
        <input type="hidden" id="resid'.$i.'" name="resid" value="ther3sidd">
        <input type="hidden" id="lesid'.$i.'" name="lesid" value="liD1">
        <input type="button" class="submitFormData" onclick="SubmitFormData('.$i.')" value="Submit" />
         </tr>';
    $i++;
}

Then in your JavaScript: 然后在您的JavaScript中:

function SubmitFormData(id) {       
    var lesid = $('#lesid'+id).val();
    var resid = $('#resid'+id).val(); 
    var dataString = {lesid: lesid, resid: resid}

    $.ajax({
        type: "POST",
        url: 'lesresadd.php',
        data: dataString, 
    })} 

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

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