Check boxes checked? - php

Now I've done a lot of research, tried a lot of methods in PHP, including $_POST isset..
foreach
etc
But i need some help!
Basically, just want to check if the checkboxes have been checked. And THEN, add 1 to the number of $visits made if the checkbox has been checked.
As long as I can check if the checkbox is checked, i think i can figure it out from there!
Thanks in advance
( note: $visits is the number of times a property has been visited. This coding displays property information read from a file)
<?
$filename = "house.txt";
$filepointer = fopen($filename,"r"); // open for read
?>
<html>
<head>
<h1> Search for properties</h1>
<form method = "post" action= "visit.php">
Enter max price
<input type = "text" name = "max" value="<?=$Max;?>">
<input type = "submit" name = "submit">
<br><i><p>Properties found</p></i></br>
</form>
</head>
</html>
<?
$myarray = file ($filename);
for ($mycount = 0; $mycount < count($myarray); $mycount++ ) { // one input line at a time
$aline = $myarray[$mycount];
$postcode = getvalue($aline,0);
$value = getvalue($aline,1);
$image = getvalue ($aline,2);
$visits = getvalue($aline,3);
$Max = $_POST['max'];
if ($value < $Max) {
print "<table border = 2>";
print "<FORM METHOD='POST' ACTION='visit.php' >";
print "<td> <input type='checkbox' name='check' value='Yes' > $postcode </td><BR> \n";
print "<td>$value <BR>";
print "<td>$image<BR>";
print "<td>$visits<BR><p>";
print "</table>";
print "</form>";
}
}
function getvalue ($aline, $commaToLookFor) {
$intoarray = explode(",",$aline);
return $intoarray[ $commaToLookFor];
}
if (isset($_POST['check']) && $_POST['check'] == 'Yes') {
echo "checked!";
} else {
echo "not checked!.";
}
?>

You're submitting a different form than the one you think you are...you have two forms on the page, both submitting to "visit.php". This line shouldn't exist:
print "<FORM METHOD='POST' ACTION='visit.php' >";
...since you've already created the form at the top of your file.
This will require a little reorganization of your code, but the basic idea is that you want one and only one form that contains all the fields and the submit button, otherwise you're submitting the form that contains the max price and nothing else.
Alternatively, if you actually do need separate forms (I'm not familiar enough with your use case to be sure), then the second form would need its own submit button.
Simplified working code:
print "<table border = 2>";
print "<FORM METHOD='POST' ACTION='visit.php' >";
print "<td> <input type='checkbox' name='check' value='Yes' > $postcode </td><BR> \n";
print "<td> <button type='submit'>Submit</button> </td><BR> \n";
print "</table>";
print "</form>";
//personally I would just check for isset($_POST['check']), but it doesn't really matter...
if (isset($_POST['check']) && $_POST['check'] == 'Yes')
{
echo "checked!";
}
else
{
echo "not checked!.";
}

Related

the form clears the array everytime I send it

I have a form with 2 selects, when you send the first, the second select charges the values that are called on my oracle bd with a query, then when i send the second select, it generates a table with checkboxes:
if(isset($idTActi)){
$stallTableTarifas=oci_parse($conn, "SELECT TARIFAS.ID, TARIFAS.ID_TIPO_ACTIVIDAD, TARIFAS.TIPO, TIPO_ACTIVIDAD.TEMPS_KM, TARIFAS.PRECIO
FROM TARIFAS, TIPO_ACTIVIDAD
WHERE TARIFAS.ID_TIPO_ACTIVIDAD = TIPO_ACTIVIDAD.ID
AND TARIFAS.ID_TIPO_ACTIVIDAD = $idTActi");
oci_execute($stallTableTarifas);
echo "<div class='divPrecios'>";
echo "<table>";
echo "<tr class='tabPreciosTitles'>";
echo "<td>Tipus Tarifa</td>
<td>Temps/Km</td>
<td>Preu</td>
<td><input type='submit' class='carrito' value=''></td>";
echo "</tr>";
while (($row=oci_fetch_array($stallTableTarifas,OCI_BOTH))!=false){
echo "<tr>";
echo "<td>".$row['TIPO']."</td>";
echo "<td>".$row['TEMPS_KM']."</td>";
echo "<td>".$row['PRECIO']."</td>";
echo "<td><input type='checkbox' name='checkbox[]' value='".$row['ID']."'/></td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
}
echo "</form>";
The variable $idTActi it's the id that i return from the second select, so when i click on the checkboxes and i send it on the button named class='carrito', that's an sprite that i generate on css, i see on the bottom another table with the information that i selected on the previous table:
echo "<div class='divPrecios'>";
echo "<table>";
echo "<tr class='tabPreciosTitles'>";
echo "<td>Nom Activitat</td>
<td>Nom Tipus Activitat</td>
<td>Tipus Tarifa</td>
<td>Temps/km</td>
<td>Preu</td>";
echo "</tr>";
foreach($_POST['checkbox'] as $item){
$stallTableCarrito=oci_parse($conn, "SELECT ACTIVIDAD.NOM AS NOM_ACTIVIDAD, TIPO_ACTIVIDAD.NOM AS NOM_TACTIVIDAD, TARIFAS.TIPO, TIPO_ACTIVIDAD.TEMPS_KM, TARIFAS.PRECIO
FROM TARIFAS, ACTIVIDAD, TIPO_ACTIVIDAD
WHERE TARIFAS.ID = $item
AND TARIFAS.ID_TIPO_ACTIVIDAD = TIPO_ACTIVIDAD.ID
AND TIPO_ACTIVIDAD.ID_ACTIVIDAD = ACTIVIDAD.ID");
oci_execute($stallTableCarrito);
$array=array(
0=>array(),
1=>array(),
2=>array(),
3=>array(),
4=>array()
);
while (($row=oci_fetch_array($stallTableCarrito,OCI_BOTH))!=false){
array_push($array[0],$row['NOM_ACTIVIDAD']);
array_push($array[1],$row['NOM_TACTIVIDAD']);
array_push($array[2],$row['TIPO']);
array_push($array[3],$row['TEMPS_KM']);
array_push($array[4],$row['PRECIO']);
}
for ($x=0;$x<count($array[0]);$x++){
echo "<tr>";
echo " <td>".$array[0][$x]."</td>";
echo " <td>".$array[1][$x]."</td>";
echo " <td>".$array[2][$x]."</td>";
echo " <td>".$array[3][$x]."</td>";
echo " <td>".$array[4][$x]."</td>";
echo " <td><input type='submit' class='carritoElim' value=''></td>";
echo "</tr>";
}
}
echo "</table>";
echo "</div>";
Basically that's a shopping form.
And where is the problem? When i send the pushed checkboxes with the button class='carrito', the form by default refresh the page and clears my array, what can i do?
In your first part of code, is your form tag open ? (I guess it is if this one works)
In the second part, is your <input type='submit' class='carritoElim' value=''> tag in a form ?
Because if it's not, you gonna have a bad time ;-)
Maybe in the last form you should generate hidden input with same names as your first form and same values.
If you don't I guess your variable $idTActi won't be set anymore and it won't succeed the first test if(isset($idTActi)). That could be why you get a cleared page.
If you have a multi step form in the same php page, for this kind of html code :
<form method=POST url="myURL">
<select name="select1">[...]</select>
<select name="select2">[...]</select>
<!-- VARIOUS PART : may not be displayed -->
<div id="checkboxes">
<input type="hidden" name="boxStep" value="1"/>
<input type="checkbox" name="cb1" value="1"/>
[...]
</div>
<!-- END OF VARIOUS PART -->
</form>
Then you need php tests in this order :
// if post request
if (isset($_POST)) {
if (isset($_POST['boxStep'])) {
// behavior when checkboxes values are sent
} else {
if (isset($_POST['select2'])) {
// behavior when second select is filled
// display "VARIOUS PART"
} else {
// behavior when only first select is filled
// Do not display "VARIOUS PART"
}
}
} else {
// default behavior (no select filled)
// Do not display "VARIOUS PART"
}
Apolo

Submit form of Iframe from the parent window

<iframe id="frame1" name="frame1" align="center" src="committee_assign1.php" height="400" width="700">
</iframe>
<center><input onClick="submitiframeform(); return false;" type="button" name="submit" value="Submit" />
<script type="text/javascript">
function submitiframeform(){
window.frames['frame1'].document.forms['fypassign'].submit();
}
</script>
The above is the main page name committee_assign.php ..
And below is the page where the iframe called committee_assign1.php.
<?php
include '../database.php';
include 'valid_login.php';
if(isset($_POST['submit'])) {
$continue = FALSE;
$i = 0;
while ($continue == FALSE) {
if (isset($_POST['id_'.$i])) {
$fypcomm = $_POST['fypcomm_'.$i];
$user = $_POST['id_'.$i];
$sql = mysql_query(" UPDATE Lecturer SET LectFypCommittee = '$fypcomm' WHERE LectID = '$user' ")
or die(mysql_error());
mysql_query($sql);
} else
{$continue = TRUE;}
$i++;
}
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.location.href='../committee/committee_assign1.php'
</SCRIPT>");
}
?>
<head>
</head>
<body>
<form id="fypassign" name="fypassign" method="post" action="" target="_self" onSubmit="">
<?php
$counter = 0;
echo "<table class ='box'>";
echo "<thead>";
echo "<tr>";
echo "<th align='left' valign='top'>"."Lecturer Name"."</th>";
echo "<th align='left' valign='top'>"."FYP Committee"."</th>";
echo "</tr>";
$sql = mysql_query(" SELECT * FROM Lecturer ORDER BY LectFypCommittee DESC, LectName ASC ") or die(mysql_error());
while($info = mysql_fetch_assoc($sql)) {
$idcount = "id_".$counter;
echo "<input type='hidden' name='$idcount' id='$idcount' value={$info['LectID']} />";
echo "<tr>";
echo "<td>";
echo $info['LectName'];
echo "</td>";
echo "<td>";
$formname = "fypcomm_".$counter;
echo "<select name='$formname'>";
//to convert the flag value to user understandable language
if ($info['LectFypCommittee'] == '0'){
$dbfyp = 'No';
}
else $dbfyp = 'Yes';
echo "<option selected='selected' value='{$info['LectFypCommittee']}'>".$dbfyp."</option>";
if ($info['LectFypCommittee'] == '0'){
echo "<option value='1'>".'Yes'."</option>";
}
else echo "<option value='0'>".'No'."</option>";
echo "</select>";
echo "</td>";
echo"</tr>";
$counter++;
}
echo "</table>";
?>
</form>
</body>
I clicked submit button at the parent page and the page refresh but the value is not update.
Can anyone here guide me on this please?
So sorry to post such long codes as I hope you guys could understand more what I am doing. TQ
You have no input in your committee_assign.php named submit:
if(isset($_POST['submit']))
You must check with something like this:
if(isset($_POST))
You are checking if submit has been posted but you dont have an input named submit. Add an input named submit with some value and check if that post submit exists. You can make it hidden if you dont want to see an extra input.
if($_POST['submit']) is checking if there is a value with key 'submit' in the post array. All the key and value in $_POST array is name and value of a form element respectively.

Update Database field on Form Submission - PHP

What This does is gets the data from the db where 'approve' field's value is '0'. Displays the form. Now what we want to do here is to UPDATE the value of the 'approve' field to '1' on the click of the "Approve" Button. I think there is some issue with the IF condition or something, not sure. No issue in connecting to the db. Or do i need to close the db connection or commit or something for update to take place, not sure. Thanks for the help.
require("dbconn.php");
// get the form data and store it in the database
// show database data
$query="SELECT * FROM page where approve=0";
$result=mysql_query($query);
if ($result)
{
print "<b>Approval pending for below listings.</b><br><br>";
while($row = mysql_fetch_array($result))
{
echo '<form name="submit_form" action="" method="post">';
$page_url = $row['page_url'];
$contact_number = $row['contact_number'];
$description = $row['description'];
$category = $row['category'];
$address = $row['address'];
$business_name = $row['business_name'];
echo "<input type=\"text\" name=\"business_link\" value=\"$page_url\" readonly><br/>";
echo "<input type=\"text\" name=\"contact_number\" value=\"$contact_number\" readonly><br/>";
echo "<input type=\"text\" name=\"description\" value=\"$description\" readonly><br/>";
echo "enter code here`<input type=\"text\" name=\"category\" value=\"$category\" readonly><br/>";
echo "<input type=\"text\" name=\"address\" value=\"$address\" readonly><br/>";
echo "<input type=\"text\" name=\"business_name\" value=\"$business_name\" readonly><br/>";
echo "<input type=\"Submit\" Value=\"Approve\" name=\"submit\"/>";
echo "</form>";
echo "<hr><br>";
if($_POST['submit_form'] == "submit")
{
mysql_query("UPDATE page SET approve='1' WHERE business_name='$business_name' AND contact_number='$contact_number' AND page_url='$page_url' AND description='$description' AND address='$address' AND category='$category'");
echo "Thank you!";
}
}
}
else
{
print mysql_error();
}
You are checking for "submit_form", but you should be checking for "submit" from your Submit button.
Change:-
if($_POST['submit_form'] == "submit")
to following:-
if(isset($_POST['submit']) && $_POST['submit'] == "Approve")
Also, you should put the above code after "require("dbconn.php");", like following:-
require("dbconn.php");
if(isset($_POST['submit']) && $_POST['submit'] == "Approve"){
// update query here
// show notification
// you can show form too
}else{
// display the form
}
Try to use the php code isset($_POST['submit']) and check whether button is submitting or not.Also remove the <input type="Submit"......> to <input type="submit"......>.Echo the SQL queries and check whether all values are coming in the query.
use
if(isset($_POST['submit_button_name']))

Incrementing/Adding data from a text file

I have a webpage that contains some data from a text file, when checked displays the checked data on a different page.
Every time a check box is checked I would like to increment the visits field to it, how do I increment the variable "$visit" every time it is visited and save it to the text file?
new.php
<html>
<body bgcolor="#99FF00">
<table border="1">
<FORM ACTION="new.php" METHOD="POST">
Enter maximum price <input type="text" name="maximumprice"/>
<p><input type="submit" value="Go" name="Go"/>
</form>
<?
$mPrice = $_POST['maximumprice'];
$file1 = "properties.txt";
$filedata = fopen ($file1, "r");
$array1 = file ($file1);
print "<form action=\"visit.php\" method=\"POST\">";
for ($counter1 = 0; $counter1 < count($array1); $counter1++)
{
$arrLine = $array1[$counter1];
$pCode = getvalue ($arrLine, 0);
$price = getvalue ($arrLine, 1);
$picture = getvalue ($arrLine, 2);
$visit = getvalue ($arrLine, 3);
if ($price < $mPrice)
{
print "<tr>";
print "<td>";
print $pCode. "<br>";
print $price. "<br>";
//print $picture. "<br>";
print $visit. "<br>";
print "<input type=\"checkbox\" name=\"box[]\" value=\"$arrLine\" />";
print "</td>";
print "<td>";
printf("<img src='$picture' width='200' height='150'>");
print "</td>";
print "</tr>";
}
}
print '<input type="submit" name="Visit" value="Visit"/>';
// Move the form outside the for loop
print "</form>";
fclose ($filedata);
function getvalue ($text, $arrNo)
{
$intoarray = explode (",", $text);
return $intoarray[$arrNo];
}
?>
</table>
</body>
</html>
this is the second page, display.php
<html>
<body bgcolor="#99FF00">
<?
foreach ($_POST['box'] as $values)
{
echo "$values <hr/>";
}
?>
</body>
</html>
There are several tutorials on how to do this on the web.
A great tutorial is the one here:
http://www.developingwebs.net/phpclass/hitcounter.php
Of course incrementing everytime a checkbox is checked will require some AJAX script.
Unless you are waiting for the post data to come through and then updating the counter.

What am I doing wrong with my PHP text boxes piece of code?

// Hy..I have the following piece of code:
mysql_select_db("baza_chestionar", $con);
$result = mysql_query("SELECT intrebari.descriere_intrebare,intrebari.nume_admin,intrebari.id_chestionar FROM intrebari WHERE intrebari.nume_admin='".$_SESSION['nume_admin']."' AND intrebari.id_chestionar='".$_SESSION['nr_chestionar']."' ");
$i=0;
while ($row = mysql_fetch_array($result))
{
$i++;
echo $i.")&nbsp". $row['descriere_intrebare'];
echo "<br><br>";
echo "<form method='POST' action='selectare_raspuns.php' id='myform' >
<input type='text' size='30' name='intrebare[]'>
</form> ";
echo "<br><br>";
}
echo "<input type='submit' value='Salveaza raspunsuri' onclick='myform.submit()'/>";
mysql_close($con);
// This select some data from a table and display it and for each data a textbox. I have another page that takes the data from the textboxes and insert it in another table. Here is the code:
foreach($_POST['intrebare'] AS $textbox)
{
$sql="INSERT INTO raspunsuri values ('','$textbox')";
var_dump($sql);
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
}
// But it only inserts the first value of the first text box. What am I doing wrong? I declared the name of the text box as an array an I loop thru with a foreach statement..
You're looping the myform form as as well. The FORM tag should be taken out of the while loop and the submit button should be within the form tag (so you can use it without JS). Also, just print_r your POST data to confirm you have the data there.
Your problem is that you use more than one form tag in your page. So only one value text box is send to your second page. You must declare the form tag before the while loop and you close the tag after your loop.
Your form tag is echoed inside the loop. Put is outside.
echo "<form method='POST' action='selectare_raspuns.php' id='myform' >\n";
while ($row = mysql_fetch_array($result))
{
$i++;
echo $i.") ". $row['descriere_intrebare'] ."<br /><br /><input type='text' size='30' name='intrebare[]'><br /><br />";
}
echo "<input type='submit' value='Salveaza raspunsuri' name="submit" />\n";
."</form>\n";

Categories