I have a tip section that can be "custom" input from a customer. When the customer enters a value, I want to pass it to PHP for updating the order.
我有一个提示部分,可以是客户的“自定义”输入。当客户输入值时,我想将其传递给PHP以更新订单。
I'm unsuccessful and I'm thinking I'm approaching this problem the wrong way since I can't get it to work. What options are available to me without using AJAX?
我不成功,我认为我正在以错误的方式解决这个问题,因为我无法让它发挥作用。没有使用AJAX,我可以使用哪些选项?
The tip section:
小费部分:
<?= ($receipt['tip'] > 0 ?
'<tr id="tip-area"><th>Tip</th>
<td><textarea id="tip" name="update_tip" readonly>'. $receipt['tip'].'</textarea></td>
</tr>'
: '') ?>
My form which has different tip options:
我的表单有不同的提示选项:
<form method="post">
<tr>
<button title="20% Tip" type="submit" name="update_tip" id="update_tip" class="tip-button"
value="<?= ($_SESSION['order']['quote']['subtotal'] * 0.2); ?>">
<small>20%<br><?= number_format(($_SESSION['order']['quote']['subtotal'] * 0.2), 2) ?></small>
</button>
<button title="Edit Tip" type="button" name="update_tip" id="custom-tip" class="tip-button">
<small>Edit<br>Tip<br></small>
</button>
<button title="Save Tip" type="submit" id="save_tip" class="hidden">
<small>Save<br>Tip</small>
</button>
</tr>
</form>
My jQuery:
我的jQuery:
$('#custom-tip').click(function(){
$('#tip').removeAttr('readonly').focus();
$('.tip-button').addClass("hidden");
$('#save_tip').removeClass("hidden");
});
$('#save_tip').click(function (){
var tip = $('textarea#tip').val();
$('<input type="hidden" name="update_tip" value="' + tip + '">').submit();
});
$('#tip').focus(function(){
this.value = '';
});
When they press "Edit Tip", the readonly
property is remove, the area comes into focus and value is cleared.
当他们按下“编辑提示”时,只读取属性,该区域成为焦点并清除值。
Then the user should enter a value and hit Save
.
然后用户应输入一个值并点击Save。
Then I'm trying to retrieve the value they entered.
然后我试图检索他们输入的值。
2 个解决方案
#1
1
I think this is what you want:
我想这就是你想要的:
$("form").submit(function() {
$("<input>", {
type: "hidden",
name: "update_tip",
value: $("#tip").val()
}).appendTo($(this));
});
This will create the hidden input with the value from the textarea and append it to the current form when before the form is submitted.
这将使用textarea中的值创建隐藏输入,并在提交表单之前将其附加到当前表单。
#2
0
You need to have PHP code on the same page (because your form does not specify an action) that handles the data. Use the 'name' attribute for your input to specify the key of the value you wish to access in the $_POST super global. For example, if I post a form with the following code:
您需要在同一页面上使用PHP代码(因为您的表单没有指定操作)来处理数据。使用输入的“name”属性指定要在$ _POST超级全局中访问的值的键。例如,如果我使用以下代码发布表单:
<form method='POST'>
<input type='hidden' value='hello' name='world'>
<input type='submit' value='submit'>
</form>
Then in PHP, I can access the value of the element with the name "world" with the following code:
然后在PHP中,我可以使用以下代码访问名为“world”的元素的值:
$_POST['world']
$ _ POST [ '世界']
Use this syntax to acquire the data from the form and persist it/update it.
使用此语法从表单中获取数据并保留/更新它。
#1
1
I think this is what you want:
我想这就是你想要的:
$("form").submit(function() {
$("<input>", {
type: "hidden",
name: "update_tip",
value: $("#tip").val()
}).appendTo($(this));
});
This will create the hidden input with the value from the textarea and append it to the current form when before the form is submitted.
这将使用textarea中的值创建隐藏输入,并在提交表单之前将其附加到当前表单。
#2
0
You need to have PHP code on the same page (because your form does not specify an action) that handles the data. Use the 'name' attribute for your input to specify the key of the value you wish to access in the $_POST super global. For example, if I post a form with the following code:
您需要在同一页面上使用PHP代码(因为您的表单没有指定操作)来处理数据。使用输入的“name”属性指定要在$ _POST超级全局中访问的值的键。例如,如果我使用以下代码发布表单:
<form method='POST'>
<input type='hidden' value='hello' name='world'>
<input type='submit' value='submit'>
</form>
Then in PHP, I can access the value of the element with the name "world" with the following code:
然后在PHP中,我可以使用以下代码访问名为“world”的元素的值:
$_POST['world']
$ _ POST [ '世界']
Use this syntax to acquire the data from the form and persist it/update it.
使用此语法从表单中获取数据并保留/更新它。