javascript - 如何动态更改标签值

标签 javascript php jquery database

我想根据通过下拉列表所做的选择来更改标签值。 如果我创建一个下拉列表,我可以更改该值,但是当我创建标签或文本框时,由于代码中的一些错误,我无法更改其值。

请仔细查看我的代码并告诉我必须在哪里进行更改:

<?php

include 'connect.php';

$emp_id = "";

$current_coordinator_id = "";

$new_coordinator_id="";
?>

<?php

if(isset($_POST['emp_id'])) {
    $emp_id= $_POST['emp_id'];
}

if(isset($_POST['current_coordinator_id'])) {
    $current_coordinator_id= $_POST['current_coordinator_id'];
}

if(isset($_POST['new_coordinator_id'])) {
    $new_coordinator_id= $_POST['new_coordinator_id'];
}

function Showemp_id()
{
    $emp_id= "";
    $con = Connectdb();
    $result_function = mysqli_query($con,"SELECT * FROM dat_database.emp_hei");
    while($row = mysqli_fetch_array($result_function))
    {
        $emp_id .= '<option value="' . $row['EMP_ID'] . '">' . $row['EMP_ID'] . '</option>';
    }
    return $emp_id;
}

?>

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>

    <script language="javascript" type="text/javascript">
        function formValidator(){
        //alert('Out side PHP');
        // Make quick references to our field
        var emp_id = document.getElementById('emp_id');
        var current_coordinator_id = document.getElementById('current_coordinator_id');
        //var swversion = document.getElementById('swversion');

        // Check each input in the order that it appears in the form!
        //if(madeSelection(functional_area , "Please Choose a Functional Area")){
        //if(madeSelection(project_id , "Please Choose a Project id")){
        //if(isAlphanumeric(swversion, "Please enter a SW version. if not applicable put 'NA'.")){
        //if(madeSelection(aspice_code_id , "Please Choose a ASPICE Code id")){
        //if(madeSelection(task_id , "Please Choose a Task id")){
        //if(madeSelection(subtask_id , "Please Choose a Subtask id")){
        //if(isAlphanumeric(date, "Please enter a date.")){
        if(isNumeric(hours_spent, "Please enter the hours spent")){
        if(isAlphanumeric(defectid, "Please enter a Defect id. if not applicable put 'NA'.")){
        return true;
        }
        }
        //}
        //}
        //}
        //}
        //}
        //}
        //}
        return false;
        }
        function isNumeric(elem, helperMsg){
            var numericExpression = /^[0-9.]+$/;

            if(elem.value.match(numericExpression)){
                return true;
            }else{
                alert(helperMsg);
                elem.focus();
                return false;
            }
        }
        function isAlphanumeric(elem, helperMsg){
            var alphaExp = /^[0-9a-zA-Z-.]+$/;

            if(elem.value.match(alphaExp)){
                return true;

            }else{
                alert(helperMsg);
                elem.focus();
                return false;
            }
        }

        function madeSelection(elem, helperMsg){
        if(elem.value == "Please Choose"){
        alert(helperMsg);
        elem.focus();
        return false;
        }else{
        return true;
        }
        }

        </script>
        <script>
        var maxcurrent_coordinator_idLength=0;

        function showcurrent_coordinator_id(str)
        {
        var xmlhttp;
        if (str.length==0)
        {
        document.getElementById("taskhint").innerHTML="";
        return;
        }
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        var str1=xmlhttp.responseText;
        var subStr=str1.split(",",2);
        var arrStr=subStr[0].split("#");
        if(maxcurrent_coordinator_idLength<arrStr.length)
        {
        maxcurrent_coordinator_idLength=arrStr.length;
        }

        for(var i=0;i<arrStr.length;i++)
        {
        document.getElementById("current_coordinator_id").options[i]=new Option(arrStr[i]);
        document.getElementById("current_coordinator_id").options[i].innerText=arrStr[i];
        }

        for(var j=arrStr.length;j<maxcurrent_coordinator_idLength;j++)
        {
        document.getElementById("current_coordinator_id").remove(arrStr.length);
        }
        }
        }
        xmlhttp.open("GET","http://10.129.65.230/subcordinator.php?r="+str,true);
        xmlhttp.send();
        }

    </script>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css">
    <meta charset="utf-8">
    <!-- Set the viewport width to device width for mobile -->
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
</head>


<body>

    <section class="examples white page-title">
    <div class="row">
    <div class="nine columns">
    <div class="twelve columns">
    <div class="fsBody fsEmbed">

    <form onsubmit="return formValidator()" action=''" method="POST" accept-charset="UTF-8" novalidate enctype="multipart/form-data" class="fsForm fsSingleColumn" id="fsForm1428139">
    <div role="alert" id="submittingText" style="display:none;">Submitting</div>
    <p class="fsSectionHeading" style="text-align: center;">
    </p>
    <p style="text-align: center;">
    <span style="font-size: 24px;">Update Coordinator_ID</span>
    </p>

    <p class="fsSectionText" style="text-align: center;">
    Please fill out required information below.
    </p>

    <table style="width:100%">


    <tr>
    <td><label class="fsSupporting" style="margin-left:50px; for="emp_id">EMP_ID</label></td>
    <td><select name="emp_id"  id="emp_id" onchange="showcurrent_coordinator_id(this.value)" required="" style="width: 214px; margin-top:5px; class="fsField fsFieldState fsRequired" aria-required="true">
    <option value="">----- Select -------</option>
    <?php echo Showemp_id(); ?>
    </select>
    </td>
    </tr>
    <tr>
    <td><label class="fsSupporting" style="margin-left:50px; for="current_coordinator_id">Current Coordinator ID</label></td>
    <td> 
    <select name="current_coordinator_id" size="1" multiple id="current_coordinator_id" required="" style="width: 214px; margin-top:5px; class="fsField fsFieldState fsRequired">
    </select>
    </td>
    </tr>

    <tr>
    <td><label class="fsSupporting" style="margin-left:50px; for="current_coordinator_id">New Coordinator ID</label></td>
    <td>

    <input type="text" id="new_coordinator_id" name="new_coordinator_id" size="50" required style="width: 200px; margin-top:5px; class="fsField fsFormatPhoneUS fsRequired" aria-required="true"></td>
    </tr>
    </table>
    <br>

    <p class="fsSectionText" style="text-align: center;">
    <input id="fsSubmitButton1428139" class="fsSubmitButton" style="width: 200px; color: rgb(0, 0, 0); background: rgb(180, 86, 146);" type="submit" value="Submit" onClick="formValidator()" />
    </p>
    </form>

</body>

</html>

<?php

    $conn=mysqli_connect("localhost","root","mmint123","dat_database");

    $query = "UPDATE dat_database.emp_hei SET coordinator_id='$new_coordinator_id'  WHERE EMP_ID='$emp_id'";

    mysqli_query($conn,$query) or die('Error, query failed'); 


    if ($query > 0)
    {
        echo "<script type='text/javascript'>alert('Coordinator_id updated successfully!')</script>";
    }

?>


<!-- You can place html anywhere within the source tags -->


<script language="javascript" type="text/javascript">
// You can place JavaScript like this

</script>
<?php
// You can place PHP like this

?>

最佳答案

您可以使用 jquery 来解决您的问题。

以下代码将为您提供所选选项的值。

$('select').on('change', function() {
  alert( this.value ); // or $(this).val()
});

之后您可以使用内部 html 更改标签的值。 使用内部 html 的演示代码:

document.getElementById("name_of_label_div_tag_here").innerHTML = "Dynamic Text goes here";

PS - 如果您不想对其进行硬编码,可以使用 Ajax 从服务器获取动态文本的值。

关于javascript - 如何动态更改标签值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31025295/

相关文章:

javascript - 将导航栏转换为水平视差网站上的固定标题

javascript - 将图像悬停使其变暗

javascript - 如何在 JS/jQuery 上裁剪具有透明背景的 JPG 和 PNG

javascript - 当切换标签时,谷歌图表会失去它们的风格

javascript - jQuery 查找 (':focus' ) 未按预期运行

PHP sprintf 被认为是有害的?

Php gettext 不显示翻译后的文本(使用 poEdit 生成 .pot 和 .mo 文件后)

jquery - CSS Slide 多主体背景图片

JavaScript 正则表达式 : why match number with trailing decimal?

php - 在 php 中的 if 语句中分配给 var