【问题标题】:textbox value does not pass to javascript and php文本框值不会传递给 javascript 和 php
【发布时间】:2013-02-01 17:26:54
【问题描述】:

我有这些代码,它们应该传递 vercode 的值,但不幸的是它没有这样做......我希望它将代码从文本框传递到 javascript 和 php

代码如下:

HTML:

    <?php
session_start();
?>
<html>
    <title>Adviser Registration</title>
    <head>
    <link rel="shortcut icon" href="favicon.ico" type="image/ico" />
    <script type="text/javascript">
    function upperCase()
        {
            //get ids
            var lname=document.getElementById("lname");
            var fname=document.getElementById("fname");
            var mname=document.getElementById("mname");
            //function
            lname.value=lname.value.toUpperCase();
            fname.value=fname.value.toUpperCase();
            mname.value=mname.value.toUpperCase();
        }
    </script>
    <script language="JavaScript" src="js/gen_validatorv31.js" type="text/javascript"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="css/reg.css">
    </head>
    <body>
        <div id="wrapheader">
    </div>
    <div id="wrap">
        <table width="902"  class="tableaccountinfo2" style="border-collapse:collapse; background:url(images/dock-bg.gif)">
            <tr>
                <td width="39" class="picaccount2"></td>
                <td width="851" height="33">Account Info</td>
            </tr>
        </table>


      <table width="898">  
        <tr>
                <td width="30"></td>
                <td width="856" style="color:#060; font-size:12px;"><b>*Note:</b>Please make sure that your account information is a registered email address. This will be used as your Autumn Account and for account validation. </td>
            </tr>
       </table>



        <table width="928" align="center" id="tablereg" style="border-collapse:collapse; font-size:14px;">
             <tr>
                <td height="23">&nbsp;</td>
          </tr>
            <tr>
                <td width="137" height="44">E-mail Address:</td>
      <td width="242"><input type="text" id="email" /></td>
                <td width="505"><span id="email_feedback">&nbsp;</span></td>
            </tr>
            <tr>
                <td height="44">Password:</td>
              <td><input type="password" id="pass" /></td>
            </tr>
            <tr>
                <td height="44">Repeat Password:</td>
            <td><input type="password" id="rpass" /></td>
                <td><span id="pass_av">&nbsp;</span></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
            </tr>
      </table>
      <table id="tablereg" style="width:400px;border-collapse:collapse;font-size:14px;">
             <tr>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td width="137">Verification Code: </td>
                <td>
                    <input type="password" id="vercode" name="vercode" />
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
            </tr>
      </table>


     <!----------------------Personal Info------------------------>
    <table width="902"  class="tableaccountinfo" style="border-collapse:collapse; background:url(images/dock-bg.gif)"  >
            <tr>
            <td width="36" class="picpersonal"></td>
                <td width="854" height="33">
                    Personal Information
                </td>
            </tr>
        </table>
        <table width="899" align="center" id="tablereg" style="border-collapse:collapse;">
             <tr>
                <td height="23">&nbsp;</td>
          </tr>
            <tr>
                <td width="123" height="44">Firstname:</td>
                <td width="765"><input type="text" id="fname" onChange="upperCase()" /></td>
            </tr>
            <tr>
                <td height="44">Middle Name:</td>
                <td><input type="text" id="mname" onChange="upperCase()" /></td>
            </tr>
            <tr>
                <td height="44">Lastname:</td>
                <td><input type="text" id="lname" onChange="upperCase()" /></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
            </tr>
      </table>

       <!----------------------School Info------------------------>

<table width="902"  class="tableaccountinfo" style="border-collapse:collapse; background:url(images/dock-bg.gif)">
            <tr>
             <td width="37" class="picstudentinfo">  </td>
                <td width="853" height="33">
                    School Information
                </td>
            </tr>
        </table>
        <table style="border-collapse:collapse;" align="center" id="tablereg">
             <tr>
                <td height="23" width="125px">&nbsp;</td>
            </tr>
            <tr>
                <td height="44" width="125px">Staff ID:</td>
                <td><input type="text" id="staffid" /></td>
            </tr>
            <tr>
                <td height="44" width="125px">College:</td>
                            <td>
                                <select id="college">
                                <option value="please_select">Select your college...</option>
                                <?php
                                include "connection.php";
                                $getcollege = mysql_query("SELECT * FROM college");
                                while($collrow = mysql_fetch_assoc($getcollege))
                                {
                                    $coll = $collrow['collegeab'];
                                    $collid = $collrow['college_id'];
                                ?>
                                    <option value="<?php echo $collid; ?>"><?php echo $coll; ?></option>
                                <?php
                                }
                                ?>
                                </select>
                            </td>
            </tr>
              <tr>
                <td height="44" width="125px">Coordinator:</td>
                <td id="coorhold">&nbsp;</td>
            </tr>
            <tr>
                <td height="23" width="125px">&nbsp;</td>
          </tr>
      </table>

       <!----------------------Incase you forgot your password------------------------>

     <table width="902"  class="tableaccountinfo" style="border-collapse:collapse; background:url(images/dock-bg.gif)">
            <tr>
                <td width="37" class="picincase"></td>
                <td width="853" height="33">
                    In case you forgot your password
                </td>
            </tr>
        </table>


      <!----------------------Registration Result------------------------>

     <table width="928px"  id="tablereg" style="border-collapse:collapse;">
        <tr>
            <td><span id="regresult">&nbsp;</span></td>
        </tr>
     </table>

     <!----------------------Register Button---------------------->

     <table width="928"  id="tablereg" style="border-collapse:collapse;">
        <tr>
            <td width="224"></td>
            <td width="664"><input type="button" id="submitregister" value="Register" name="submitregister" /></td>
        </tr>
     </table>
    </div>
<script type="text/javascript" src="js/advreg.js"></script>
<script type="text/javascript" src="js/chooseadv.js"></script>
<script language='JavaScript' type='text/javascript'>
function refreshCaptcha()
{
    var img = document.images['captchaimg'];
    img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
}
</script>
    </body>
</html>

JAVASCRIPT:

$('#submitregister').click(function() {  
    //grab values
    var vercode = $('#vercode').val();

    //show loading text
    $('#regresult').text('Loading...');

    //perform http request
    $.post('validations/vadvreg.php', { vercode: vercode
    }, function(data){
        $('#regresult').html(data);
    });
});

PHP

<?php
include 'connection.php';
session_start();

if(isset($_POST['vercode'])){

    $vercode = mysql_real_escape_string(htmlentities($_POST['vercode']));

}else {
    echo "<font color='red'>Please fill in ALL fields!</font>1";
}
?>

【问题讨论】:

  • 这与您的问题无关,但请注意 mysql_ 函数已弃用。
  • 尝试将输入改为读取
  • @twoleggedhorse 我试过了,没用
  • 它在一个
    里面?请显示 html 的整个表单部分...
  • 尝试发出警报(vercode);在 live var vercode 之后......它是否显示值?还可以查看是否使用开发人员工具触发了请求 - 按 F12 并使用网络工具或使用 www.fiddler2.com - URL 可能无法解析

标签: php javascript html textbox passwords


【解决方案1】:

输入必须具有name 属性,以便在提交表单时作为请求的一部分发送:

<input type=password id=vercode name=vercode>

来自 W3C:http://www.w3.org/html/wg/drafts/html/master/single-page.html#attr-fe-name

【讨论】:

  • 我通过它的ID获取vercode的值,而不是通过名称,我没有使用表单,我使用javascript获取值
  • @jekuyotcosta 你预计会发生什么?您并没有真正说出问题所在,只是说“它没有这样做”
  • 问题是它没有传递 ID 为 vercode 的文本框的值
  • @jekuyotcosta 你怎么知道的?它通过了什么?
【解决方案2】:

使用 jQuery http://api.jquery.com/jQuery.post/ 中指定的 done 命令

//perform http request
$.post('validations/vadvreg.php', { vercode: vercode})
.done(function(data){
    $('#regresult').html(data);
});

【讨论】:

  • 确保路径正确。如果文件位于同一目录中,则 vadvref.php 将提供服务。
猜你喜欢
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2012-12-30
  • 1970-01-01
  • 1970-01-01
  • 2017-08-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多