1

所以我试图获得这个功能,它是表单的状态下拉选择,给我在另一个文件中使用 $_POST 提交时选择的值,事情是我真的不知道当你使用'name = ' 传入的参数。

这是我的代码

<?php
    function state_select ($name,$full_state,$selected = '') {

  $state_list = array("AL|Alabama","AK|Alaska","AZ|Arizona", "AR|Arkansas", "CA|California", "CO|Colorado", "CT|Connecticut", "DE|Delaware", "DC|Washington D.C.", "FL|Florida", "GA|Georgia", "HI|Hawaii", "ID|Idaho", "IL|Illinois", "IN|Indiana", "IA|Iowa", "KS|Kansas", "KY|Kentucky", "LA|Louisiana", "ME|Maine", "MD|Maryland", "MA|Massachusetts", "MI|Michigan", "MN|Minnesota", "MS|Mississippi", "MO|Missouri", "MT|Montana", "NE|Nebraska", "NV|Nevada", "NH|New Hampshire", "NJ|New Jersey", "NM|New Mexico", "NY|New York", "NC|North Carolina", "ND|North Dakota", "OH|Ohio", "OK|Oklahoma", "OR|Oregon", "PA|Pennsylvania", "RI|Rhode Island", "SC|South Carolina", "SD|South Dakota", "TN|Tennessee", "TX|Texas", "UT|Utah", "VT|Vermont", "VA|Virginia", "WA|Washington", "WV|West Virginia", "WI|Wisconsin", "WY|Wyoming");

  ?>
  <select name=<?=$name;?> id="state" ><?
    foreach ($state_list as $s) {
        $values = explode("|",$s);
        ?><option value="<?=$values[0];?>" <?
                if($selected == $values[0]) { ?>SELECTED<? }
                ?>>
                <? if($full_state) { ?><?=$values[1];?><? } else { ?><?=$values[0];?><? } ?></option><?
    }
  ?></select><?
}
?>


<span class="InputDiscription">State: </span><div class="InputInner"><?=state_select('state',false,'AL'); ?></div><!----- END .InputInner ---------->
<?php
 $setError=$_GET['seterror']; 
if($setError == 1){
    echo "<span class='errorMsg'>*Required</span>";
    }
else
    echo "<span>*</span>"; 

?>

谢谢!

已更新_ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __

将值发送到的文件 ---

 $selectvalue=$_POST['hiddenfield'];
    echo $selectvalue;

主文件!

<head>  
<script type="text/javascript">
function submitform(){
    // Get the index of selected item
    var list = document.getElementById('state');
    var INDEX = list.selectedIndex;
    document.getElementById("hiddenfield").value=list[INDEX].value;

    //Submit the form
    document.forms["theform"].submit();
}
</script>
</head>
    <div class="InputContainer">
    <!------------------------------------------STATE SELECTION CODE ------------------------------------------->
<?php
    function state_select ($name,$full_state,$selected = '') {

  $state_list = array("AL|Alabama","AK|Alaska","AZ|Arizona", "AR|Arkansas", "CA|California", "CO|Colorado", "CT|Connecticut", "DE|Delaware", "DC|Washington D.C.", "FL|Florida", "GA|Georgia", "HI|Hawaii", "ID|Idaho", "IL|Illinois", "IN|Indiana", "IA|Iowa", "KS|Kansas", "KY|Kentucky", "LA|Louisiana", "ME|Maine", "MD|Maryland", "MA|Massachusetts", "MI|Michigan", "MN|Minnesota", "MS|Mississippi", "MO|Missouri", "MT|Montana", "NE|Nebraska", "NV|Nevada", "NH|New Hampshire", "NJ|New Jersey", "NM|New Mexico", "NY|New York", "NC|North Carolina", "ND|North Dakota", "OH|Ohio", "OK|Oklahoma", "OR|Oregon", "PA|Pennsylvania", "RI|Rhode Island", "SC|South Carolina", "SD|South Dakota", "TN|Tennessee", "TX|Texas", "UT|Utah", "VT|Vermont", "VA|Virginia", "WA|Washington", "WV|West Virginia", "WI|Wisconsin", "WY|Wyoming");

  ?>
  <select name=<?=$name;?> id="state" ><?
    foreach ($state_list as $s) {
        $values = explode("|",$s);
        ?><option value="<?=$values[0];?>" <?
                if($selected == $values[0]) { ?>SELECTED<? }
                ?>>

                <?  if($full_state) { ?><?=$values[1];?><? } else { ?><?=$values[0];?><? } ?></option><?
    }
  ?></select><?
}
?>




<span class="InputDiscription">State: </span><div class="InputInner">
<?=state_select('state',false,'AL'); ?>
 </div><!----- END .InputInner ---------->
<input type="hidden" id="hiddenfield" name="hiddenfield" value="">
<?php

 $setError=$_GET['seterror']; 
if($setError == 1){
    echo "<span class='errorMsg'>*Required</span>";
    }
else
    echo "<span>*</span>"; 

?>

</div><!---------------------------------END .InputContaienr ----------------------------------->

更新 - - - - - - - - - - - - - - - - - - - - - - - - - ----------------------- 主文件-------------

<html>
<head>  
<script type="text/javascript">
function submitform(){
    // Get the index of selected item
    var list = document.getElementById('state');
    var INDEX = list.selectedIndex;
    document.getElementById("hiddenfield").value=list[INDEX].value;

    //Submit the form
    document.forms["theform"].submit();
}
</script>
</head>
<body>

<?php
include('Main.css');

$hostname="";
$username="";
$password="";
$database="";

/*mysql_connect($hostname,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");*/

//function getFN(){
try {  
  # MySQL with PDO_MYSQL  
  $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);  
  $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

  //$DBH->prepare('SELECT first FROM contacts');
}  
catch(PDOException $e) { 
    echo "I'm sorry, I'm afraid I can't do that.";  
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);   
}  
//get query
$FNresult=$DBH->query('SELECT CompanyNames FROM companynames'); 
//set fetch mode
$FNresult->setFetchMode(PDO::FETCH_ASSOC);

$DBH = null; 
?>


        <div id="MainContainer">   
            <h2 align="center">
                <u>PLEASE SUPPLY THE FOLLOWING INFORMATION REGARDING YOUR AUTO INSURANCE POLICY:</u>
           </h2>

         <div class="FormContainer">
            <form name="theform" action="insert.php" method="post">
                <div class="CheckBoxContainer">
            <input type="checkbox" value"On" name="only-thisform"/>I will complete the insurance verification form only. 
            <?php 
            $setError=$_GET['seterror'];

            if ($setError == 1){

                echo "<div class='errorMsg'> *Required</div>";
            }
             ?>
            <br>
            <br>

            <input type="checkbox"  value="On" name="approval" id="approval"/>I agree to authorize PCG Protects, LLC to receive a certificate of liability and or to be added as an additional interest on my personal auto policy, whichever accommodates my Auto Insurance Carriers standard practice, so they can monitor the active status of my personal auto policy, on behalf of my employer.
             <?php 
             $setError=$_GET['seterror'];
             if ($setError == 1){

                echo "<div class='errorMsg'> *Required</div>";
             }

             ?>
            </div><!------------------------------------END   .CheckBoxContainer ------------------------------------------->
            <br>
            <br>


<script type="text/javascript"
     src="http://code.jquery.com/jquery-latest.min.js">
</script>   
<!-- javascript on client-side -->


<div class="DropDownSelector">
<?php

$dropdown = "<select name='CompanyNames' id='CompanyNames' >";

$dropdown .= "\r\n<option  id ='other' name ='other' value='other'>other</option>";

while($row =$FNresult->fetch()) {

  $dropdown .= "\r\n<option  id ='firstname' name ='first' value='{$row['CompanyNames']}'>{$row['CompanyNames']}</option>";
}

$dropdown .= "\r\n</select>";

echo $dropdown;
?>
    <div class="DropdownDiscription">Please select your insurance company.  Select other if you do not see it listed. </div>
</div> <!------------------------------------------END .DropDownSelector -------------------------------------------------------->
<script type="text/javascript">  

var dropdown = $('#CompanyNames');
//var test = $('#firstname');
//document.write(dropdown.val());

dropdown.bind('change', function(){

    $.post('backgroundScript.php', 
        { 
            'CompanyNames': dropdown.val()

        },
        function(response) {
            $('#compname').val(response.Companyname);
            //$('#phone').val(response.phone);
            //$('#policynum').val(response.policynum);
            //$('#nameofPolicyholder').val(response.policyholder);
            //$('#mailing').val(response.mailing);
            // Repeat for all of your form fields
        },
        'json'
    );

});
</script>
<br>
<div class="FormBody">
    <div class="InputContainer">
<span class="InputDiscription">Insurance Company Name: </span><div class="InputInner"> <input type="text" name="compname" id="compname" ></div><!----- END .InputInner ---------->

<?php
 $setError=$_GET['seterror']; 
if($setError == 1){
    echo "<span class='errorMsg'>*Required</span>";
    }
else
    echo "<span>*</span>"; 
?>

</div><!---------------------------------END .InputContaienr ----------------------------------->
<br>
    <div class="InputContainer">
<span class="InputDiscription">Insurance Phone number: </span><div class="InputInner"><input type="text"  name="phone" id="phone"></div><!----- END .InputInner ---------->
<?php
 $setError=$_GET['seterror']; 
if($setError == 1){
    echo "<span class='errorMsg'>*Required</span>";
    }
else
    echo "<span>*</span>"; 
?>

</div><!---------------------------------END .InputContaienr ----------------------------------->
<br>
    <div class="InputContainer">
<span class="InputDiscription">Policy Number: </span><div class="InputInner"><input type="text" name ="policynum" id="policynum"></div><!----- END .InputInner ---------->
<?php
 $setError=$_GET['seterror']; 
if($setError == 1){
    echo "<span class='errorMsg'>*Required</span>";
    }
else
    echo "<span>*</span>"; 
?>

</div><!---------------------------------END .InputContaienr ----------------------------------->
<br>
    <div class="InputContainer">
<span class="InputDiscription">Name of the Policyholder: </span><div class="InputInner"><input type="text" name="nameofPolicyholder" id="nameofPolicyholder"></div><!----- END .InputInner ---------->
<?php
 $setError=$_GET['seterror']; 
if($setError == 1){
    echo "<span class='errorMsg'>*Required</span>";
    }
else
    echo "<span>*</span>"; 
?>


</div><!---------------------------------END .InputContaienr ----------------------------------->
<br>
    <div class="InputContainer">
<span class="InputDiscription">Policyholder mailing address: </span><div class="InputInner"><input type="text" name="mailing" id="mailing"></div><!----- END .InputInner ---------->
<?php
 $setError=$_GET['seterror']; 
if($setError == 1){
    echo "<span class='errorMsg'>*Required</span>";
    }
else
    echo "<span>*</span>"; 
?>

</div><!---------------------------------END .InputContaienr ----------------------------------->
<br>
    <div class="InputContainer">
<span class="InputDiscription">City: </span><div class="InputInner"><input type="text" name="city" id="city"></div><!----- END .InputInner ---------->
<?php
 $setError=$_GET['seterror']; 
if($setError == 1){
    echo "<span class='errorMsg'>*Required</span>";
    }
else
    echo "<span>*</span>"; 
?>


</div><!---------------------------------END .InputContaienr ----------------------------------->
<br>
    <div class="InputContainer">
    <!------------------------------------------STATE SELECTION CODE ------------------------------------------->
<?php
    function state_select ($name,$full_state,$selected = '') {

  $state_list = array("AL|Alabama","AK|Alaska","AZ|Arizona", "AR|Arkansas", "CA|California", "CO|Colorado", "CT|Connecticut", "DE|Delaware", "DC|Washington D.C.", "FL|Florida", "GA|Georgia", "HI|Hawaii", "ID|Idaho", "IL|Illinois", "IN|Indiana", "IA|Iowa", "KS|Kansas", "KY|Kentucky", "LA|Louisiana", "ME|Maine", "MD|Maryland", "MA|Massachusetts", "MI|Michigan", "MN|Minnesota", "MS|Mississippi", "MO|Missouri", "MT|Montana", "NE|Nebraska", "NV|Nevada", "NH|New Hampshire", "NJ|New Jersey", "NM|New Mexico", "NY|New York", "NC|North Carolina", "ND|North Dakota", "OH|Ohio", "OK|Oklahoma", "OR|Oregon", "PA|Pennsylvania", "RI|Rhode Island", "SC|South Carolina", "SD|South Dakota", "TN|Tennessee", "TX|Texas", "UT|Utah", "VT|Vermont", "VA|Virginia", "WA|Washington", "WV|West Virginia", "WI|Wisconsin", "WY|Wyoming");

  ?>
  <select name=<?=$name;?> id="state" ><?
    foreach ($state_list as $s) {
        $values = explode("|",$s);
        ?><option value="<?=$values[0];?>" <?
                if($selected == $values[0]) { ?>SELECTED<? }
                ?>>

                <?  if($full_state) { ?><?=$values[1];?><? } else { ?><?=$values[0];?><? } ?></option><?
    }
  ?></select><?
}
?>




<span class="InputDiscription">State: </span><div class="InputInner">
<?=state_select('state',false,'AL'); ?>
 </div><!----- END .InputInner ---------->
<input type="hidden" id="hiddenfield" name="hiddenfield" value="">
<?php

 $setError=$_GET['seterror']; 
if($setError == 1){
    echo "<span class='errorMsg'>*Required</span>";
    }
else
    echo "<span>*</span>"; 

?>

</div><!---------------------------------END .InputContaienr ----------------------------------->
<br>
<?php 
 $setError=$_GET['seterror']; 
if($setError == 1){
echo "<script type='text/javascript'>alert('Please check to make sure all required fields are filled out!');</script>";
}
?>
<input type="Submit" name="Submit" >

</div><!-------------------------------------------END .FormBody ------------------------------------------------------->
</form>


    </div><!--------------------------------------End .FormContainer ---------------------------------------------->

</div><!----------------------------------End #MainContainer----------------------------------->

</body>
</html>

其他文件---------------- 除了那个显示代码之外,所有内容都被注释掉了/ / ** * ** * ** * ** * ** * ** * ** *验证复选框已选中** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** / /* $setError = 0; $ch1 = '未选中'; $ch2 = '未选中'; // print_r("测试 ch1 : $ch1"); if(isset($_POST['only-thisform'])) {

                $ch1 = 'Check';
                //if ($setChecker != 1)
                $setError= 0;
                //print_r("testing ch1: $ch1");

            }
            if ($ch1 !='Check' ){

            $setError = 1;
            returnBack();

        }
            if (isset($_POST['approval'])) {
                //$setChecker = 1;
                $ch2 = 'Check';
                $setError= 0;
                //print_r("testing  ch2: $ch2");
            }
             if ($ch2 != 'Check' ){

            $setError = 1;
            returnBack();

        }*/
/***************************************************************************************************************************************/       
        /* 
function returnBack(){
    header("Location: http://pcgprotects.com/PCG/test.php?seterror=1");
    exit;

}


if(isset($_POST['compname']))  {
    $compname=$_POST['compname'];
}
else{
    $setError = 1;
    returnBack();
}

if(isset($_POST['phone']))  {
    $phone=$_POST['phone'];
}
else{
    $setError = 1;
    returnBack();
}

if(isset($_POST['policynum']))  {
    $policynum=$_POST['policynum'];
}
else{
    $setError = 1;
    returnBack();
}

if(isset($_POST['nameofPolicyholder']))  {
    $nameofPolicyholder=$_POST['nameofPolicyholder'];
}
else{
    $setError = 1;
    returnBack();
}

if(isset($_POST['mailing']))  {
    $mailing=$_POST['mailing'];
}
else{
    $setError = 1;
    returnBack();
}
if(isset($_POST['city']))  {
    $city=$_POST['city'];
}
else{
    $setError = 1;
    returnBack();
}
if(isset($_POST['mailing']))  {
    $mailing=$_POST['mailing'];
}
else{
    $setError = 1;
    returnBack();
}
*/

    echo "hiddenfield=".$_POST['hiddenfield'];

/*
mysql_connect($hostname,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$new_id_number = mysql_insert_id();
$query = "INSERT INTO contacts VALUES ('$new_id_number','$compname','$phone','$policynum','$nameofPolicyholder','$mailing')";
echo 'being inserted';
mysql_query($query);

if ('SELECT * FROM companynames WHERE CompanyNames != :compname');{
    $query = "INSERT INTO companynames VALUES('$new_id_number','$compname')";
    mysql_query($query);
    echo "working";
}
mysql_close();*/
?>
4

2 回答 2

1

这将提交表单,您需要添加一个表单元素并在 select 元素上添加 onchange 事件提交它,我希望这会有所帮助

<html>
<head>  
<script type="text/javascript">
function submitform(){
    document.forms["theform"].submit();
}
</script>
</head>
<body>

<form action="otherfile.php" method="post" name="theform">
<?php
    function state_select ($name,$full_state,$selected = '') {

  $state_list = array("AL|Alabama","AK|Alaska","AZ|Arizona", "AR|Arkansas", "CA|California", "CO|Colorado", "CT|Connecticut", "DE|Delaware", "DC|Washington D.C.", "FL|Florida", "GA|Georgia", "HI|Hawaii", "ID|Idaho", "IL|Illinois", "IN|Indiana", "IA|Iowa", "KS|Kansas", "KY|Kentucky", "LA|Louisiana", "ME|Maine", "MD|Maryland", "MA|Massachusetts", "MI|Michigan", "MN|Minnesota", "MS|Mississippi", "MO|Missouri", "MT|Montana", "NE|Nebraska", "NV|Nevada", "NH|New Hampshire", "NJ|New Jersey", "NM|New Mexico", "NY|New York", "NC|North Carolina", "ND|North Dakota", "OH|Ohio", "OK|Oklahoma", "OR|Oregon", "PA|Pennsylvania", "RI|Rhode Island", "SC|South Carolina", "SD|South Dakota", "TN|Tennessee", "TX|Texas", "UT|Utah", "VT|Vermont", "VA|Virginia", "WA|Washington", "WV|West Virginia", "WI|Wisconsin", "WY|Wyoming");

  ?>
  <select name=<?=$name;?> id="state" onchange="submitform()"><?
    foreach ($state_list as $s) {
        $values = explode("|",$s);
        ?><option value="<?=$values[0];?>" <?
                if($selected == $values[0]) { ?>SELECTED<? }
                ?>>
                <? if($full_state) { ?><?=$values[1];?><? } else { ?><?=$values[0];?><? } ?></option><?
    }
  ?></select><?
}
?>


<span class="InputDiscription">State: </span><div class="InputInner"><?=state_select('state',false,'AL'); ?></div><!----- END .InputInner ---------->
<?php
 $setError=$_GET['seterror']; 
if($setError == 1){
    echo "<span class='errorMsg'>*Required</span>";
    }
else
    echo "<span>*</span>"; 

?>
</form>
</body>
</html>
于 2012-08-12T04:08:30.837 回答
0

这个怎么样?并在其他 php 文件中使用 $selectvalue=$_POST['hiddenfield']

<html>
<head>  
<script type="text/javascript">
function submitform(){
    // Get the index of selected item
    var list = document.getElementById('state');
    var INDEX = list.selectedIndex;
    document.getElementById("hiddenfield").value=list[INDEX].value;

    //Submit the form
    document.forms["theform"].submit();
}
</script>
</head>
<body>

<form action="otherfile.php" method="post" name="theform">
<?php
    function state_select ($name,$full_state,$selected = '') {

  $state_list = array("AL|Alabama","AK|Alaska","AZ|Arizona", "AR|Arkansas", "CA|California", "CO|Colorado", "CT|Connecticut", "DE|Delaware", "DC|Washington D.C.", "FL|Florida", "GA|Georgia", "HI|Hawaii", "ID|Idaho", "IL|Illinois", "IN|Indiana", "IA|Iowa", "KS|Kansas", "KY|Kentucky", "LA|Louisiana", "ME|Maine", "MD|Maryland", "MA|Massachusetts", "MI|Michigan", "MN|Minnesota", "MS|Mississippi", "MO|Missouri", "MT|Montana", "NE|Nebraska", "NV|Nevada", "NH|New Hampshire", "NJ|New Jersey", "NM|New Mexico", "NY|New York", "NC|North Carolina", "ND|North Dakota", "OH|Ohio", "OK|Oklahoma", "OR|Oregon", "PA|Pennsylvania", "RI|Rhode Island", "SC|South Carolina", "SD|South Dakota", "TN|Tennessee", "TX|Texas", "UT|Utah", "VT|Vermont", "VA|Virginia", "WA|Washington", "WV|West Virginia", "WI|Wisconsin", "WY|Wyoming");

  ?>
  <select name=<?=$name;?> id="state" onchange="submitform()"><?
    foreach ($state_list as $s) {
        $values = explode("|",$s);
        ?><option value="<?=$values[0];?>" <?
                if($selected == $values[0]) { ?>SELECTED<? }
                ?>>
                <? if($full_state) { ?><?=$values[1];?><? } else { ?><?=$values[0];?><? } ?></option><?
    }
  ?></select><?
}
?>


<span class="InputDiscription">State: </span><div class="InputInner"><?=state_select('state',false,'AL'); ?></div><!----- END .InputInner ---------->
<?php
 $setError=$_GET['seterror']; 
if($setError == 1){
    echo "<span class='errorMsg'>*Required</span>";
    }
else
    echo "<span>*</span>"; 

?>
<input type="hidden" id="hiddenfield" name="hiddenfield" value="">
</form>
</body>
</html>

?>

在 otherfile.php 中输入这个,你可以得到值

<?php
echo "hiddenfield=".$_POST['hiddenfield'];
?>
于 2012-08-12T05:11:47.603 回答