简体   繁体   English

加载时以HTML格式显示数据

[英]Display data in HTML form when loaded

N00b alert; N00b警报; I know enough to be dangerous, so forgive my ignorance...I've been through all of the related questions here and elsewhere, but I just can't seem to comprehend the answer that's surely included in the responses :-( 我知道足够危险了,所以请原谅我的无知...我已经遍及这里和其他地方的所有相关问题,但是我似乎无法理解肯定包含在答复中的答案:-(

I'm posting a record id to a page where I want a form to display with the contents of the related record. 我要在页面上张贴记录ID,并在其中希望表单显示相关记录的内容。 I'm getting the record in correctly (confirmed using the alert) with this script in the HEAD section (jquery 1.9 is called as well): 我在HEAD部分使用此脚本正确获取了记录(使用警报确认)(也调用了jQuery 1.9):

<script type="text/javascript">
function getSelectedCustomer () {        
    ...use the id to get the right record...
    databaseAPI.callback = function() {
        if (databaseAPI.error) {
            alert("Database Error: " + databaseAPI.error);
            } 
        else {
            var customerRecord = databaseAPI.result;
            alert("Test Callback: " + new String(customerRecord.full_name));
            $("#quoteForm").load(customerRecord);
            }
        return;
        };
    databaseAPI.ajaxGet();        
    }
window.onload = getSelectedCustomer; 
</script>

...and the form in the BODY to be loaded: ...以及要加载的BODY中的表单:

<form method="post" id="quoteForm" action="process_quote.php">
<table>
<tbody>
<tr>
    <td>Name</td>
    <td><input type="text" value="<?php $customerRecord['full_name']; ?>" name="full_name"></td>
</tr>
...other bits of the form...
<tr>
    <td>
        <input type="submit" value="Submit">
    </td>
</tr>
</tbody>
</table>
</form>

I know I'm incorrectly munging various things together. 我知道我错误地将各种东西混在一起。 Can someone please get me straightened out on what to do? 有人可以请我弄清楚该怎么做吗?

Michael's answer solved the INPUT fields in the form. Michael的答案解决了表单中的INPUT字段。 Didn't mention I had SELECT fields as well: 没提到我还有SELECT字段:

<select size="0" name="email_sent">
    <option value="No">No</option>
    <option value="Yes">Yes</option>
</select>

Changing INPUT to SELECT works. 将INPUT更改为SELECT即可。

What your missing is where code is being executed. 您缺少的是正在执行代码的地方。 The PHP code is being executed on the server, before being sent to the browser. 在将PHP代码发送到浏览器之前,它正在服务器上执行。 The Javascript is then rendered by the browser. 然后由浏览器呈现Javascript。 You can't pass variables back and forth between Javascript and PHP. 您不能在Javascript和PHP之间来回传递变量。

You want to inject the name with Javascript. 您想使用Javascript注入名称。 I see you're already using jQuery, so the heavy lifting is already done for you. 我看到您已经在使用jQuery,因此繁重的工作已经为您完成。 Remove the value="<?php $customerRecord['full_name']; ?>" from the PHP file, and replace $("#quoteForm").load(customerRecord); 从PHP文件中删除value="<?php $customerRecord['full_name']; ?>" ,然后替换$("#quoteForm").load(customerRecord); with $("#quoteForm input[name='full_name']").val(customerRecord.full_name); $("#quoteForm input[name='full_name']").val(customerRecord.full_name);

Should work, might need some variation depending on your exact circumstances. 应该可以工作,可能需要根据您的实际情况进行一些更改。 At least it should put you down the right path. 至少它应该使您走上正确的道路。

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

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