So, I'm working on a mask which filters the data when <input type="button" />
is clicked.
I have:
- school classes(1, 2, 3, 4, 5)
- sections (A, B, C....Z)
- and sex checkboxes (male, female).
This mask interacts with a mysql database, so if the only one checkbox checked is "male", it should give me back all the males in my database. The problem is that I used javascript to filter sexes and to make my string of sexes and I don't know how to pass it to a php variable to use it on my query.
I've been looking online for 3-4 days and all recommend to use $_POST. If I use $_POST it says that the variable is undefined. I searched for this error on the internet and it says to add a control with isset. I've done this but nothing seems to work because it gives me a new error "undefined index".
So I need help for passing the value of a javascript variable to a php variable (I know it's hard because javascript is client side and php is server side) or directly pass my textarea text (I can display my javascript result in a textarea) to a php variable.
May someone help me?
Here is my index.php:
<?php
require 'connection.php'
?>
<html>
<script src="scripts.js"></script>
<form name="code" method="post" action="query.php; <?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table border="0">
<!-- ROW1 -->
<tr>
Da codice
<input type="text" name="dacode">
a codice
<input type="text" name="acode">
</tr>
</form>
<!-- ROW2 -->
<form id="sexform" name="sexform">
<tr>
<td></td>
<td>
Sesso
<input type="checkbox" name="sex" value="Maschio"> M
<input type="checkbox" name="sex" value="Femmina"> F
<input type="checkbox" name="sex" value="Azienda"> G
<input type="hidden" id="xxx">
</form>
<!-- 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 -->
<form id="sezform" name="sezform">
<br>
Sezioni
<input type="checkbox" name="sez" value="A"> A
<input type="checkbox" name="sez" value="B"> B
<input type="checkbox" name="sez" value="C"> C
<input type="checkbox" name="sez" value="D"> D
<input type="checkbox" name="sez" value="E"> E
<input type="checkbox" name="sez" value="F"> F
<input type="checkbox" name="sez" value="G"> G
<input type="checkbox" name="sez" value="H"> H
<input type="checkbox" name="sez" value="I"> I
<input type="checkbox" name="sez" value="J"> J
<input type="checkbox" name="sez" value="K"> K
<input type="checkbox" name="sez" value="L"> L
<input type="checkbox" name="sez" value="M"> M
<input type="checkbox" name="sez" value="N"> N
<input type="checkbox" name="sez" value="O"> O
<input type="checkbox" name="sez" value="P"> P
<input type="checkbox" name="sez" value="Q"> Q
<input type="checkbox" name="sez" value="R"> R
<input type="checkbox" name="sez" value="S"> S
<input type="checkbox" name="sez" value="T"> T
<input type="checkbox" name="sez" value="U"> U
<input type="checkbox" name="sez" value="V"> V
<input type="checkbox" name="sez" value="W"> W
<input type="checkbox" name="sez" value="X"> X
<input type="checkbox" name="sez" value="Y"> Y
<input type="checkbox" name="sez" value="Z"> Z
<br>
<input type="checkbox" name="sez" value="Ai"> Ai
<input type="checkbox" name="sez" value="Am"> Am
<input type="checkbox" name="sez" value="Ae"> Ae
<input type="checkbox" name="sez" value="Bi"> Bi
<input type="checkbox" name="sez" value="Be"> Be
<input type="checkbox" name="sez" value="Bm"> Bm
</form>
<!-- ROW5 -->
<form id="claform" name="claform">
<br>
Sezioni
<input type="checkbox" name="cla" value="1"> 1
<input type="checkbox" name="cla" value="2"> 2
<input type="checkbox" name="cla" value="3"> 3
<input type="checkbox" name="cla" value="4"> 4
<input type="checkbox" name="cla" value="5"> 5
<br>
<br>
RESULT STRING
<textarea type="text" name="outputfield" id="outputfield"> </textarea>
<input type="button" onclick="myFunction(); selectValues(); query();" value="Invia">
</td>
</tr>
</form>
<br>
<br>
</html>
This is my query.php file:
<?php
require 'connection.php';
// Inizio selezione
$usedatabase = "use " . $dbname;
if (isset($_POST['xxx'])) {
$queryfinale = "select * from sergiodellera.mskselez where sesso='" . $_POST['xxx'] . "'" ;
}
//$final1 = mysql_query($usedatabase) or die(mysql_error());
//$final2 = mysql_query($queryfinale) or die(mysql_error());
//$query = mysql_query($queryfinale);
//$row = mysql_fetch_row($query);
//$final2 = $row[5];
echo $queryfinale;
?>
and this is my js file:
function myFunction() {
var sex = document.forms["sexform"];
var sez = document.forms["sezform"];
var cla = document.forms["claform"];
var txt = "";
var txtsex = "";
var txtsez = "";
var txtcla = "";
var isex;
var isez;
var icla;
//FOR SEX
for (isex = 0; isex < sex.length; isex++) {
if (sex[isex].checked) {
txtsex = txtsex + sex[isex].value;
}
}
//FOR SEZ
for (isez = 0; isez < sez.length; isez++) {
if (sez[isez].checked) {
txtsez = txtsez + sez[isez].value;
}
}
//FOR CLA
for (icla = 0; icla < cla.length; icla++) {
if (cla[icla].checked) {
txtcla = txtcla + cla[icla].value;
}
}
txtsex = txtsex;
txtsez = txtsez + ";";
txtcla = txtcla + ";";
txt = txtsex + txtsez + txtcla;
for (sexfor = 0; sexfor < txtsex.length; sexfor++) {
if (txtsex.length = 22){
var asex = txtsex;
var bsex = asex.substring(0, 1);
var csex = asex.substring(7, 8);
var dsex = asex.substring(14, 15);
}
if (txtsex.length = 15){
var asex = txtsex;
var bsex = asex.substring(0, 1);
var csex = asex.substring(7, 8);
}
if (txtsex.length = 8){
var asex = txtsex;
var bsex = asex.substring(0, 1);
}
break;
}
sexcontainer = bsex + csex + dsex;
document.getElementById("outputfield").value = sexcontainer;
//myFunction()
}
function selectValues(){
document.getElementById('xxx').value = sexcontainer;
return true;
}
function query() { window.open("query.php");
}
I'm expecting that if i check for example male and female checkboxes i'll have a string like "MF". So, through a php variable i'm gonna have select * from tablename where sex=$varsex
where varsex is MF