且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Javascript变量值\ textarea内容到PHP变量

更新时间:2023-10-07 11:31:46

(This was a basic work in progress which has only a limited amount of functionality which I will attempt to improve if I have the time - it may still have technical faults - it is only intended to give an indication of what I think is needed - you will need to sort the results.)


The hidden input will be picked up by your php as $_POST['all_sexes'] and you could do something similar for other items. The difficult bit might be to edit the result on an "unclick" or change of mind.

You could use an onKeyPress event for the textboxes in a similar way. I would suggest putting it all in one form as it is all being submitted together.

This is the JavaScript:

 <script language="javascript">
 var all_sexes;
 var sex;

 function make_sex(sex){
 var sexes = document.getElementById('all_sexes').value
 if(sex == "Maschio"){sex = 'M';}
 if(sex == "Femmina"){sex = 'F';}
 if(sex == "Azienda"){sex = 'G';}
 if( sexes.indexOf(sex) >-1){ sex = '';} // working on what to do if unchecked and checked here
 all_sexes = document.getElementById('all_sexes').value + sex;

 document.getElementById('all_sexes').value = all_sexes;
 document.getElementById('outputfield').value = all_sexes;
 }
 </script>

And call the function in your checkbox with an onClick event passing it the value of the checkbox.

     Sesso
     <input type="checkbox" name="sex" value="Maschio" onclick="make_sex(this.value)"> M
     <input type="checkbox" name="sex" value="Femmina" onclick="make_sex(this.value)"> F
     <input type="checkbox" name="sex" value="Azienda" onclick="make_sex(this.value)"> G
     <input type="hidden" name="all_sexes" id="all_sexes" value="">

The html debugger is not happy with some text outside of spans but I am not here to look at that right now.

And, whatever you do do not put any POST variable content anywhere near your database without sanitizing it - see comments at the bottom of this page php registration form complication

Not sure if you actually need to display the click result on the page - if you don't and just want the submitted values this would be a much better approach - borrowed from PHP how to loop through a post array the accepted answer

 foreach( $_POST as $stuff ) {
    if( is_array( $stuff ) ) {
        foreach( $stuff as $thing ) {
           echo $thing . "<br />"; 
        } 
    } else { 
     echo $stuff ."<br />"; 
    }
  }

This is your html adapted to give you a quick demonstration of what it does - your php script shown here at the bottom would be on your query.php

There are a number of html errors where text is outside of the table elements which the debugger complains about - there are too many instances to fix here and perhaps you should be looking at a different structure rather than using tables for your framework as opposed to divs.

    <!DOCTYPE html>
    <html>
    <form name="code" method="post" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]);?>">
    <table border="0">
    <!-- ROW1 -->
    <tr>
    Da codice 
    <input type="text" name="dacode">
      a codice 
    <input type="text" name="acode">
    </tr>
    <!-- ROW2 -->   
    <br />          
    Sesso
    </tr>
    <tr>
    <input type="checkbox" name="Maschio" id="Maschio" value="Maschio" > M
    <input type="checkbox" name="Femmina" id="Femmina" value="Femmina" > F
    <input type="checkbox" name="Azienda" id="Azienda" value="Azienda" > G
    <!-- ROW3 -->
    <br>
    Da data nascita
    <input type="date" name="dadata" min="1980-01-01">
    a data nascita
    <input type="date" name="adata" min="1980-01-01">
    <!-- ROW4 -->
    <br>
    Sezioni
    <input type="checkbox" name="A" value="A"> A
    <input type="checkbox" name="B" value="B"> B
    <input type="checkbox" name="C" value="C"> C
    <input type="checkbox" name="D" value="D"> D
    <input type="checkbox" name="E" value="E"> E
    <input type="checkbox" name="F" value="F"> F
    <input type="checkbox" name="G" value="G"> G
    <input type="checkbox" name="H" value="H"> H
    <input type="checkbox" name="I" value="I"> I
    <input type="checkbox" name="J" value="J"> J
    <input type="checkbox" name="K" value="K"> K
    <input type="checkbox" name="L" value="L"> L
    <input type="checkbox" name="M" value="M"> M
    <input type="checkbox" name="N" value="N"> N
    <input type="checkbox" name="O" value="O"> O
    <input type="checkbox" name="P" value="P"> P
    <input type="checkbox" name="Q" value="Q"> Q
    <input type="checkbox" name="R" value="R"> R
    <input type="checkbox" name="S" value="S"> S
    <input type="checkbox" name="T" value="T"> T
    <input type="checkbox" name="U" value="U"> U
    <input type="checkbox" name="V" value="V"> V
    <input type="checkbox" name="W" value="W"> W
    <input type="checkbox" name="X" value="X"> X
    <input type="checkbox" name="Y" value="Y"> Y
    <input type="checkbox" name="Z" value="Z"> Z
    <br>
    <input type="checkbox" name="Ai" value="Ai"> Ai
    <input type="checkbox" name="Am" value="Am"> Am
    <input type="checkbox" name="Ae" value="Ae"> Ae
    <input type="checkbox" name="Bi" value="Bi"> Bi
    <input type="checkbox" name="Be" value="Be"> Be
    <input type="checkbox" name="sez" value="Bm"> Bm
    <input type="hidden" id="all_Bn" value="">
    <!-- ROW5 -->
    <br>
    Sezioni
    <input type="checkbox" name="Chk1" value="1"> 1
    <input type="checkbox" name="Chk2" value="2"> 2
    <input type="checkbox" name="Chk3" value="3"> 3
    <input type="checkbox" name="Chk4" value="4"> 4
    <input type="checkbox" name="Chk5" value="5"> 5
    </form>
    <br>
    <br>
     RESULT STRING
     <textarea type="text"  name="outputfield" id="outputfield"> </textarea>
     <input type="submit" name="submit" value="Invia">
                </td>
      </tr>
      </form>
      <br>
      <br>
      <?php
     foreach( $_POST as $stuff ) {
          if( is_array( $stuff ) ) {
              foreach( $stuff as $thing ) {
                echo $thing . "<br />"; 
             } 
          } else { 
            echo $stuff ."<br />"; 
          }
     }
     ?>
     </html>

A couple of other borrowed and slighty modified ideas which might help with handling the data sent by your form from:mysql_escape_string whole post array?

This one will just display what you set and its name and value:

    foreach(array_keys($_POST) as $key){ echo htmlspecialchars($key) . " " . htmlspecialchars($_POST[$key]) . "<br />";}

And this one is supposed to escape it (you need to be logged in to your sql session):

    foreach(array_keys($_POST) as $key){ $_POST[$key] = mysqli_real_escape_string($_POST[$key]); echo htmlspecialchars($key) . " " . htmlspecialchars($_POST[$key]);}

To collect the results from the A to Z checkboxes into a string where they have been checked this might work and has the advantage that you are not putting anything from the submitted value into your string.

     $abc = '';
     $abc_cnt = 'A';
     while( $abc_cnt1 < 26 ){
          if(isset($_POST[$abc_cnt])){
          $abc = $abc . $abc_cnt;
          }
     $abc_cnt++;
     $abc_cnt1++;
     }
     echo $abc;  // your result string for Sezzioni

The same applies to your other variables that you want to put into a string - for example if all the "Sesso" checkboxes are all clicked you will get "MFG" or you might have M and G or F and G.

     if (isset($_POST['Maschio'])) $sesso = $sesso . 'M';
     if (isset($_POST['Femmina'])) $sesso = $sesso . 'F';
     if (isset($_POST['Azienda'])) $sesso = $sesso . 'G';

相关阅读

推荐文章