输入字符串以下划线的形式分隔

时间:2014-02-10 20:47:04

标签: php html forms

我没有找到任何关于此事的好运,因为我想不出如何说出来。

基本上我有一个表单,在HTML中,最终用户会将一个值“12345_54321”提交到1个输入字段,然后进行处理。

我希望能够允许有2个输入字段,因此其中一个输入“12345”,而在第二个输入字段中输入“54321”。

这似乎很容易,但我真正需要的是“_”必须用作分隔符,例如,当提交值时,它将处理“12345_54321”而不是“12345”和“54321”< / p>

到目前为止我的表格:

<form role="form" method="post" action="process.php">
<fieldset>
<div class="form-group">
  <input size="18" type="visible" name="postid" id="postid" class="form-control" placeholder="Enter Story ID Here:" class="input-medium" ><input size="18" type="visible" name="postid" id="postid" class="form-control" placeholder="Enter Comment ID Here:" class="input-medium" >
</div>
    <input type="submit" name="submit" class="btn btn-primary btn-large" id="submit_btn" value="Process"/>
</fieldset>

2 个答案:

答案 0 :(得分:0)

如果要为故事ID显示2个输入,则必须将页面修改为如此(不需要下划线(_):

<form role="form" method="post" action="process.php">
  <fieldset>
    <div class="form-group">
      <input size="18" type="visible" name="story_id_1" id="story_id_1" class="form-control" placeholder="Enter Story ID Here:" class="input-medium" >
      <input size="18" type="visible" name="story_id_2" id="story_id_2" class="form-control" placeholder="Enter Story ID Here:" class="input-medium" >
      <input size="18" type="visible" name="comment_id" id="comment_1" class="form-control" placeholder="Enter Comment ID Here:" class="input-medium" >
    </div>
    <input type="submit" name="submit" class="btn btn-primary btn-large" id="submit_btn" value="Process"/>
  </fieldset>
</form>

process.php 中,您可以通过查看POST来获取这些变量。

<?php
$story_id_1 = '';
$story_id_2 = '';
$comment_id = '';

// Check for empty fields
if(isset($_POST['story_id_1']))
  $story_id_1 = $_POST['story_id_1']; // From HTML Page
if(isset($_POST['story_id_2']))
  $story_id_2 = $_POST['story_id_2']; // From HTML Page
if(isset($_POST['comment_id']))
  $comment_id = $_POST['comment_id']; // From HTML Page

print 'Story Id 1: '. $story_id_1 . '</br>';
print 'Story Id 2: '. $story_id_1 . '</br>';
print 'Comment Id: '. $comment_id . '</br>';

答案 1 :(得分:0)

添加一个隐藏字段,其值将是两个输入字段的串联:

并在提交时,通过连接两个输入字段的值来设置其值:

<form role="form" method="post" action="process.php">
<fieldset>
    <input type="hidden" id="theValue" />
    <div class="form-group">
        <input size="18" type="visible" name="postid" id="postid1" class="form-control" placeholder="Enter Story ID Here:" class="input-medium" onchange="concat();">
        <input size="18" type="visible" name="postid" id="postid2" class="form-control" placeholder="Enter Comment ID Here:" class="input-medium" onchange="concat();">
    </div>
    <input type="submit" name="submit" class="btn btn-primary btn-large" id="submit_btn" value="Process" />
</fieldset>

<script>
// if no jQuery.....standard ECMAScript
function concat() {
    var val = document.getElementById('postid1').value + '_' + document.getElementById('postid2').value;
    document.getElementById('theValue').value = val;
    console.log(val);
}