当表单提交时,如何根据下拉框中的值禁用html表单输入框?

时间:2022-02-08 07:26:22

I have a html form with multiple input fields, a name and an address for a customer. when the user submits the form the values in these fields are sent into a database made up of 2 tables.

我有一个包含多个输入字段的html表单,一个客户名和一个地址。当用户提交表单时,这些字段中的值被发送到由两个表组成的数据库中。

I also have a drop down box with the options 'yes, and 'no'. when the form is submitted with 'no' selected I need the values to get sent to the database as normal but then instead of clearing the input fields for a new entry I need name and last name to grey out / become read only and keep the values that the user entered in them. while the address fields reset for a new entry.

我还有一个“yes”和“no”选项的下拉框。提交表单时选择“不”我需要的值发送到数据库正常但不是结算新条目我需要的输入字段名和姓灰/成为只读,让用户输入的值。当地址字段为新条目重置时。

The user can then enter new values in the address fields and press submit again where these will get added to the address table of my database with the customer Id of the name/lastname they previously entered.

然后,用户可以在address字段中输入新的值,然后再次按submit,在那里,这些值将被添加到我的数据库的address表中,并使用之前输入的名称/lastname的客户Id。

this will be repeated until they press a different button on the form that then sends the final address and resets the entire form for a new customer entry.

这将被重复,直到他们在表单上按下一个不同的按钮,然后发送最终地址并为新的客户条目重置整个表单。

So far I have it setup so a user can enter the customer name and address and press submit, it will then get send to the database and the form will reset for another customer entry.

到目前为止,我已经设置了它,用户可以输入客户名和地址并按submit,然后它将被发送到数据库,表单将重置为另一个客户条目。

This is my form, with 4 entry fields,

这是我的表格,有4个输入字段,

<form name="add" method="post" action="newCustomer.php">
<div id="leftcol">
<label><span>Name</span><br /> <input type="text" name="addFname" /></label>
<label><span>Surname</span> <input type="text" name="addLname" /> </label>
<label><span>Single Site?</span><Select name="field" onchange="showHide(this.value)

</label>
<div id="rightcol">
<label><span>Address line 1</span><input type="text" name="addSiteLine1" /></label>
<label><span>Address line 2</span><input type="text" name="addSiteLine2" /></label>

So far i have tried to add this to the bottom of my php code

到目前为止,我已经尝试将它添加到php代码的底部

if($dropdown== "no"){
     disable_text(true);

     }

disable text is a javascript function that disables the text, but this didn't do anything.

禁用文本是一个javascript函数,它禁用文本,但这没有做任何事情。

I also tried to call this function upon form submit but then that stopped my $_POST methods from obtaining the values. when I set tried this again using read-only instead of disabled nothing seemed to happen.

我也尝试在表单提交时调用此函数,但随后停止了$_POST方法获取值。当我使用只读而不是禁用设置再次尝试时,似乎什么也没有发生。

I am basically trying to avoid needing a second form / webpage for adding customer addresses and trying to avoid adding multiple input boxes for multiple addresses ( because some may have 100+ addresses)

基本上,我尽量避免需要第二个表单/网页来添加客户地址,并尽量避免为多个地址添加多个输入框(因为有些地址可能有100多个)

EDIT

编辑

$Dropdown is defined in the php as:

$Dropdown在php中定义为:

$dropdown = $_POST['field'];

in the form as

在形式

<label><span>Single Site?</span><Select name="field" onchange="showHide(this.value);">
</label>
<Option value="yes">yes</option>
<Option value="no">no</option>

the function was being called, when the function disabled all of the text fields I could see it happen, they would flash grey then the page would refresh and I would get error messages saying that

这个函数正在被调用,当这个函数禁用了我能看到的所有文本域时,它们会闪烁灰色然后页面会刷新,我会得到错误消息

    $fName = $_POST['addFname'];

could not be defined. when I changed the function to make the fields readonly nothing seemed to happen, it would just submit the data and clear the form ready for a new input.

不能被定义。当我更改函数使字段重新读取时,似乎没有发生任何事情,它只会提交数据并为新输入准备好表单。

1 个解决方案

#1


3  

I may not understand the question so correct me if I am wrong. Can't you just disable the fields with javascript and save the field values in session variables to carry back to the form?

如果我错了,我可能不会理解这个问题,所以请纠正我。难道不能使用javascript禁用字段并将字段值保存到会话变量中,以便将其带回表单吗?

document.getElementById('fieldToDisable').style.disabled= 'true';

Make a session variable flag and if that is set on page load then refill the form and disable the fields.

创建一个会话变量标志,如果在页面加载中设置,则重新填充表单并禁用字段。

#1


3  

I may not understand the question so correct me if I am wrong. Can't you just disable the fields with javascript and save the field values in session variables to carry back to the form?

如果我错了,我可能不会理解这个问题,所以请纠正我。难道不能使用javascript禁用字段并将字段值保存到会话变量中,以便将其带回表单吗?

document.getElementById('fieldToDisable').style.disabled= 'true';

Make a session variable flag and if that is set on page load then refill the form and disable the fields.

创建一个会话变量标志,如果在页面加载中设置,则重新填充表单并禁用字段。