繁体   English   中英

PHP,Javascript,选择并提醒

[英]PHP,Javascript, select and alert

您好,我是Java语言的新手,目前正在尝试更新另一个开发人员代码。 我有一个非常简单的4个问题,其中包含复选框和文本字段的混合。

我的第一篇Javascript作品:它说如果postsitesurvey中没有键入任何信息,然后警告必须填写它。 下一步,我坚持下去。 该员工有2个可见选项和1个隐藏选项。 剩下零件了吗? 我希望如果他选择“是”,那么必须填写下一个字段,即“左边为”字段;如果不是,那么他可以继续提交表单。 任何帮助,将不胜感激。

echo "<form id=\"myForm\" name=\"myForm\" method=\"post\" action=\"edit.php?
action=upbill2\" onsubmit=\"return validateForm()\">";
echo "<table width=\"100%\" border=\"0\" cellspacing=\"5\" cellpadding=\"0\" 
class=\"table2\">";

echo "<div class=\"col-sm-6\">";
echo "<b>Post Site Survey</b>";
Echo "&nbsp&nbsp&nbsp";
echo "<textarea style=\"width:70%\" name=\"postsitesurvey\" 
id=\"postsitesurvey\" value=\"$postsitesurvey\" />";
echo $postsitesurvey;
echo "</textarea>";
echo "</div>";

echo "<div class=\"col-sm-6\">";

echo "<b>Was There Equipment Left Over?</b>";
Echo "&nbsp&nbsp&nbsp";
echo "<input name=\"eq_left\" type=\"checkbox\" id=\"eq_left_yes\" 
value=\"1\"";
if ($eq_left == "1") { echo " checked=\"checked\" "; }
echo "/> &nbsp&nbsp&nbspYes ";

Echo "&nbsp&nbsp&nbsp";

echo "<input name=\"eq_left\" type=\"checkbox\" id=\"eq_left_no\" 
value=\"2\"";
if ($eq_left == "2") { echo " checked=\"checked\" "; }
echo "/> &nbsp&nbsp&nbspNo ";
echo "</div>";

echo "<input name=\"eq_left\" type=\"checkbox\" id=\"eq_left_empty\" 
value=\"\" style=\"display:none;\" checked";
if ($eq_left == "") { echo " checked=\"checked\" "; }
echo "</div>";

echo "<div class=\"col-sm-6\">";
echo "<b>Left with?</b>";
Echo "&nbsp&nbsp&nbsp";

echo "<input style=\"width:45%\" name=\"eq_poc_\" type=\"text\" 
id=\"eq_poc\" value=\"$eq_poc\" placeholder=\"Name of customer\"";

echo "</div>";
echo "&nbsp&nbsp&nbsp or";
Echo "&nbsp&nbsp&nbsp";
echo "<input name=\"eq_poc\" type=\"checkbox\" id=\"chkNo\" value=\"MCS\"";
if ($eq_poc == "MCS") { echo " checked=\"checked\" "; }
echo "/> &nbsp&nbsp&nbspMCS";

echo "</div>";

echo "<br>";

echo "<div class=\"col-sm-12 text-center\">";
echo "<input name=\"st_id\" type=\"hidden\" id=\"st_id\" value=\"3\" />";
echo "<input name=\"job_id\" type=\"hidden\" id=\"job_id\" value=\"$job_id\" />";
echo "<input type=\"submit\" name=\"Submit\" value=\"Submit\" class=\"btn btn-primary\"/>";
echo "</div>";
echo "</table>";
echo "</form>";
}

---- JAVASCRIPT ----

function validateForm() {
var x = document.forms["myForm"]["postsitesurvey"].value;
if (x == "") {
    alert("Post site survey must be filled out");
    return false;
}

我删除了分散注意力的内容,您可以将粘贴内容复制到您的问题中,然后我将删除此帖子...

<?php
    $checked_1 = ($eq_left == "1")   ? 'checked="checked"' : '';
    $checked_2 = ($eq_left == "2")   ? 'checked="checked"' : '';
    $checked_3 = ($eq_left == "")    ? 'checked="checked"' : '';
    $checked_4 = ($eq_poc  == "MCS") ? 'checked="checked"' : '';
?>


<form id="myForm" name="myForm" method="post" action="edit.php?action=upbill2" onsubmit="return validateForm()">

    <b>Post Site Survey</b>
    <textarea name="postsitesurvey" id="postsitesurvey" value="$postsitesurvey" />
        <?=$postsitesurvey?>
    </textarea>

    <b>Was There Equipment Left Over?</b>
    <input name="eq_left" type="checkbox" id="eq_left_yes" value="1" <?=$checked_1?>/>
    <input name="eq_left" type="checkbox" id="eq_left_no" value="2" <?=$checked_2?>/>
    <input name="eq_left" type="checkbox" id="eq_left_empty" value="" style="display:none;" <?=$checked_3?>/>

    <b>Left with?</b>
    <input name="eq_poc_" type="text" id="eq_poc" value="$eq_poc" placeholder="Name of customer"/>
    <b>or</b>
    <input name="eq_poc" type="checkbox" id="chkNo" value="MCS" <?=$checked_4?>/><b>MCS</b>

    <input name="st_id" type="hidden" id="st_id" value="3" />
    <input name="job_id" type="hidden" id="job_id" value="<?=$job_id?>" />
    <input type="submit" name="Submit" value="Submit" class="btn btn-primary"/>
</form>

我认为类似这样的方法对您有用。 我将您的复选框更改为单选按钮,并删除了eq_left = "" 您还具有两个名为“ eq_poc”的字段。 你不可以做这个。 我删除了一个选项,但您可能要考虑其他选项或解释您要做的更好的事情。

我还添加了toggleHide()函数来显示或隐藏客户名称字段。

警报消息可能令人讨厌,因此我删除了警报消息,并添加了<div>将错误消息显示为红色。

需要注意的另一件事是,我在您的textarea中添加了required属性。 这样,浏览器便可以通知用户尚未填写必填字段。 但是,这样做不会在页面上以红色显示错误消息。 因此,根据您想要实现的方式,您可能希望删除必需的属性。

如果您对下面的代码感到满意,则下面的代码将自己运行,删除顶部的两行用于测试的行,看看它是否可以与其他代码传递的变量一起使用。

<?php

        //The two lines below are for testing purposes only. Once you are done testing, you can remove them to implement it with the rest of your code.
        $eq_left = "1";
        $eq_poc = "";

    $checked_yes = ($eq_left == "1")   ? 'checked="checked"' : '';
    $checked_no = ($eq_left == "2")   ? 'checked="checked"' : '';
?>
<html>
<script>

function toggleHide() {
        var equipment_left = document.getElementById('eq_left_yes').checked;

        if (equipment_left){
            document.getElementById('leftwith').style.display = 'block';
        }
        else {
            document.getElementById('leftwith').style.display = 'none';
        }

}

function validateForm() {
        var message = "";
        var postsitesurvey = document.getElementById('postsitesurvey').value;
        if (postsitesurvey == "") {
            message = "Post site survey must be filled out";
        }
        else if (equipment_left && eq_poc == ""){
            var equipment_left = document.getElementById('eq_left_yes').checked;
            var eq_poc = document.getElementById('eq_poc').value;
            message = "Customer name must be filled in";
        }

        if (message != ""){
            document.getElementById('message').textContent = message;
            return false;
        }


        return true;
}
</script>
<style>
.formfield {
        font-weight: bold;
}
#leftwith {
        display: none;
}
#message {
        color: red;
}
</style>
</body>
<form id="myForm" name="myForm" method="post" action="edit.php?action=upbill2" onsubmit="return validateForm()">

<div class="formfield">Post Site Survey</div>
<textarea name="postsitesurvey" id="postsitesurvey" value="$postsitesurvey" required="required"  /><?php echo $postsitesurvey ?></textarea>

<div class="formfield">Was There Equipment Left Over?
        <input name="eq_left" type="radio" id="eq_left_yes" value="1" onclick="toggleHide()" <?php echo $checked_yes ?> /> Yes
        <input name="eq_left" type="radio" id="eq_left_no" value="2" onclick="toggleHide()" <?php echo $checked_no ?> /> No
</div>

<div class="formfield" id="leftwith">
Left with?
<input name="eq_poc_" type="text" id="eq_poc" value="<?php echo $eq_poc ?>" placeholder="Name of customer"/>
</div>
<input name="st_id" type="hidden" id="st_id" value="3" />
<input name="job_id" type="hidden" id="job_id" value="<?php echo $job_id ?>" />
<div id="message"></div>
<input type="submit" name="Submit" value="Submit" class="btn btn-primary"/>
</form>
<script>
        toggleHide();
</script>
</body>
</html>

暂无
暂无

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

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