简体   繁体   English

选自ID填充的行的值

[英]populate values of a row from the id selected

I'm using PHP to populate names into an option list based on values from a database. 我正在使用PHP根据数据库中的值将名称填充到选项列表中。 Once a name is selected I want to populate the values of a row based on whatever name has been selected. 选择名称后,我想根据选择的名称填充行的值。

I had no problem populating the option list with the name values, but once I select a name the records of that row will not display in the input boxes. 使用名称值填充选项列表没有问题,但是一旦选择名称,该行的记录将不会显示在输入框中。

Here is the code I'm working with: 这是我正在使用的代码:

$query = "SELECT name, id 
FROM artists 
ORDER BY name";

$mysql_stuff = mysql_query($query, $mysql_link);
while($row = mysql_fetch_row($mysql_stuff)) {

 $artists_name = htmlspecialchars(stripslashes($row[0]));
 $artists_id = stripslashes($row[1]);

    // we compare the id of the record returned and the one we have selected
    if ($artists_id) { 
        $selected = $artists_id;
    } else {
        $selected = "";
    }
    print("<option value=\"$artists_id\">$artists_name</option>
    ");
}

print("</select>
<input type=\"Submit\" name=\"submit\" value=\"Update\">
<input type=\"Submit\" name=\"delete\" value=\"Delete\">

<br >
<br />
Edit Biography:
</td>
</tr>
");

if (isset($_POST['submit'])) {
    print("<tr valign=\"top\">
    <td valign=\"top\" width=\"150\">Name</td>
    <td valign=\"top\">Artist Biography</td>
    </tr>

    ");


    print("<tr valign=\"top\" bgcolor=\"$colour\">
    <td valign=\"top\">
    <input type=\"text\" name=\"artists_name[$selected]\" value=\"$artists_name\" size=\"40\" />
    </td>

    <td valign=\"top\">
    <textarea name=\"artists_Biography[$selected]\"  cols=\"40\" rows=\"10\">$artists_Biography</textarea>
    </td>


    </tr>
    ");

}   


print("</table>
</form>
");

Can I please get some assistance with populating the values of the selected name into the input boxes. 在将所选名称的值填充到输入框中时,我能获得一些帮助吗?

So your submitting artist ID to the post. 因此,您向帖子提交的艺术家ID。 The post would then refresh or change the page. 然后,该帖子将刷新或更改页面。 Assuming the action is that page you have the ID in your post. 假设操作是该页面,则您的帖子中有ID。 How do you get Artist Name from the ID? 您如何从ID获取艺术家姓名? Do you run another sql query on the id to find the name? 您是否在ID上运行另一个SQL查询以查找名称? Might need some clarification on the order of your code to help. 可能需要对代码的顺序进行一些说明才能提供帮助。 Thanks 谢谢

I'm not sure how to interpret this code. 我不确定如何解释此代码。 Use Ajax. 使用Ajax。 Use Jquery on change() method to take the value of the select and send it via ajax() to a php script that gets the info from the DB and returns what you want. 在change()方法上使用Jquery来获取select的值,并通过ajax()将其发送到一个php脚本,该脚本从数据库获取信息并返回您想要的内容。 Then use the Success part of the .ajax() method to put that data into you input on the form. 然后,使用.ajax()方法的Success部分将数据放入表单中的输入中。

$(function(){
    $("select#artist").change(function() 
    {
        var artist = $("select#artist").val();
    $.ajax({
    type: "POST",
    url: "ajax-find-artist.php",
    data: "artistID=" + artist, 
    cache: false,
    success: function(html){
    var newElems = html;
    $("input#artistRestult").html(newElems);
    }
    });
    });
});

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

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