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.
Related
I have created an array within a session which I have sucessfully managed to place into a table. It is effectively just a list of favourties that a user can create by clicking a button on a product page.
However, I want to create a button next to each product in the table which removes the product from the array and I cannot work out why it isn't working.
Here is my code: (updated)
<?php
if (isset($_POST['remove'])) {
$value_to_delete = 'JX-1E1-LTU';
if(($key = array_search($value_to_delete, $_SESSION['arr'])) !== false) {
unset($_SESSION['arr'][$key]);
$_SESSION["arr"] = array_values($_SESSION["arr"]);
}
}
?>
<?php
$contents = ($_SESSION['arr']);
$arrlength = count($contents);
echo '<table class="table table-striped equipment">';
echo '<thead>';
echo '<tr>';
echo '<th scope="col">';
echo 'Parts';
echo '</th>';
echo '<th scope="col">';
echo 'Remove Item';
echo '</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
for($x = 0; $x < $arrlength; $x++)
{
echo '<tr>';
echo '<td>';
echo $contents[$x];
$part = $contents[$x];
echo '</td>';
echo '<td>';
$pos = array_search($part, $contents);
echo ' ' . $pos;
?>
<form action="" method="post">
<input type="text" name="value" value="<?php echo $part;?>">
<input type="submit" name="remove" value="Remove">
</form>
<?php
echo '</td>';
echo '</tr>';
}
echo'</tbody>';
echo '</table>';
?>
The 'deleteall' button works at the bottom of the page - it successfully removes all the values from my session, but the 'remove' button isn't removing each product individually:
if (isset($_POST['remove'])) {
$key=array_search($_GET[$part],$_SESSION['arr']);
if($key!==false)
unset($_SESSION['arr'][$key]);
$_SESSION["arr"] = array_values($_SESSION["arr"]);
}
echo '<form action="" method="post">';
echo '<input type="submit" name="remove" value="Remove">';
echo '</form>';
I'm guessing I am doing something wrong in the above section, but I seemed to have hit a brick wall and tried everything I can think of, any help would be gratefully recevied!
Your second sample seems to imply you're executing the remove action right before the form display, so after having displayed the table. It won't work on the first page refresh.
So unless the remove action code is not in the right place, Try this piece of code below, which works here.
Use it first as a single file. Follow the instructions, and see if you can reproduce your issue. If not, it might be related to the environment.
Then, put the block code before displaying your table (and edit the $_SESSION["arr"] with correct values :)), and see if it removes the right element from the array.
If it doesn't, try debugging it through logging.
<?php
session_start();
echo "<pre>";
// Load this page once. Then, before the next page refresh,
// Comment the below line to check if the session values
// are updated
$_SESSION["arr"] = array("trousers", "blue", "jean");
print_r($_SESSION);
unset($_SESSION["arr"][array_search("blue", $_SESSION["arr"])]);
print_r($_SESSION);
$_SESSION["arr"] = array_values($_SESSION["arr"]);
print_r($_SESSION);
echo "</pre>";
?>
<?php
session_start();
include("#nav.php");
include("dbconnectie.php");
echo "Plaatsingen: ";
$query = $db->prepare("SELECT * FROM shop");
$query->execute();
$result = $query->fetchALL(PDO::FETCH_ASSOC);
echo "<table>";
foreach($result as &$data) {
echo "<tr>";
$img = $data['img_url'];
echo "<td>" . $data["brand"] . "</td>";
echo "<td>" . $data["model"] . "</td>";
echo "<td> Condition: " . $data["cond"] . "/100 </td>";
echo "<td> Prijs: $ " . number_format($data["price"],2,",",".") . "</td>";
echo "<td> <img src='$img' width='400' height='300' ></img> </td>";
echo "<td> Plaatsing nummer: " . $data['id_img'] . "</td>";
echo "</tr>";
echo "<br>";
}
echo "</table>";
if(isset($_POST['atc']))
{
if($_SESSION['on']){
$myarray = array('0');
$addtoarray = $_GET['id'];
array_push($myarray, $addtoarray);
$_SESSION['cart'] = $myarray;
echo "Toegevoogd aan uw winkelmandje.";
var_dump($_SESSION['cart']);
}else
{
echo "Log eerst in!";
}
}
?>
<html>
<title>Just for kicks</title>
<header>
</header>
<body>
<form method='post' action=''>
Plaatsing nummer invoeren:
<input type='number' name ='id' value ='id'><br>
<button type="submit" class="submit" name="atc" value="atc">Winkelmadje</button><br><br>
</form>
</body>
</html>
Between line 25 and 31 I'm trying to add numbers to the session array but I'm not sure how, because this is clearly not working. It doesn't add the number you fill in, at the form part. But it appears it doesn't do anything.
I trimmed your code down to just the bare minimum and added some inline comments. I made the assumption that the contents of $_SESSION['cart'] is an array.
Please notice:
POST was used consistently
The value of the input was removed (value ='id')
The check for $_SESSION['on'] was removed
Code
<?php
// Start your session.
session_start();
// The form was submitted.
if (isset($_POST['atc'])) {
// Get the cart so we can append to it.
// Assuming that the cart is an array.
$cart = (array)$_SESSION['cart'];
// Append the user's input to the end of the cart.
$cart[] = $_POST['id'];
// Store it in the session.
$_SESSION['cart'] = $cart;
// Dump out the session.
var_dump($_SESSION);
}
?>
<html>
<title>Just for kicks</title>
<header>
</header>
<body>
<form method='post'>
<label> Plaatsing nummer invoeren:
<input type='number' name='id'/>
</label><br>
<button type="submit" class="submit" name="atc" value="atc">Winkelmadje</button>
<br><br>
</form>
</body>
</html>
I have a form in which I enter the name, surname and function (result one select), I display them with a foreach in a table.
I want to add a button to every foreach record that will load the first form with the data from the row but I can not.
Thanks for the help!
<?php
require_once 'functii/functii.php';
$r = functia();
$options = "";
while ($row = mysqli_fetch_array($r)) {
$options = $options . "<option>$row[0]</option>";
}
?>
<form method="POST">
<table id="add"><tr><td>Nume:</td><td><input type="text" name="nume"/></td></tr>
<tr><td>Prenume:</td><td><input type="text" name="prenume"/></td></tr>
<tr><td>Functia:</td><td><select name="functia"><?php echo $options; ?></select></td></tr>
<tr><td>Activ:</td><td><input type="radio" name="activ"/></td></tr>
<tr><td></td><td><input type="submit" name="adaugare" value="Adauga"/><input type="submit" name="modificare" value="Modifica"/></td></tr></table>
</form>
<?php
if (isset($_POST['adaugare'])) {
require_once 'functii/functii.php';
$n = $_POST['nume'];
$p = $_POST['prenume'];
$f = $_POST['functia'];
$r = adaug_utilizator($n, $p, $f);
}
require_once 'functii/functii.php';
$utilizatori = grid_utilizatori();
if (count($utilizatori) == 0) {
print 'Nu sunt utilizatori adaugati';
} else {
?>
<table id="view">
<thead><tr><th colspan="4">Utilizatori adaugati</th></tr>
<tr><th>Nume</th><th>Prenume</th><th>Functia</th></tr>
</thead>
<tbody>
<?php
foreach ($utilizatori as $utilizator) {
print "<tr>";
print "<td>" . $utilizator['nume'] . "</td>";
print "<td>" . $utilizator['prenume'] . "</td>";
print "<td>" . $utilizator['denumire'] . "</td>";
print "<td><input type='submit' name='modifica' value='Modifica'></td>";
print "</tr>";
}
if (isset($_POST['modifica'])) {
$_POST['nume'] = $utilizator['nume'];
$_POST['prenume'] = $utilizator['prenume'];
$_POST['functia'] = $utilizator['denumire'];
}
?>
</tbody>
</table>
<?php
}
?>
You can execute loop as follows:
<?php
foreach($utilizatori as $index => $utilizator){
?>
<input type='submit' name='modifica<?php echo ($index+1);?>' value='Modifica' class="clickBtn"></td>
<?php
}
?>
with above loop name attribute will be as modifica1, modifica2 etc.
Now, you want to fill information based on button clicked to the form shown at your top of code, I think so.
For this, you can use jQuery:
$('.clickBtn').click(function(e){
var buttonOfRowClicked = $(this).attr('name');
var rowNumber = buttonOfRowClicked.substr(8); // It will get 1,2,3 etc table row.
$('input[name=nume]').val($(tr:nth-child(rowNumber) td:nth-child(1).val());
$('input[name=prenume]').val($(tr:nth-child(rowNumber) td:nth-child(2).val()); //similarly fill other variables.
});
I have not check the code by executing. It is overview for you to run your code. I think idea is sufficient for you for direction how should your code works.
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!.";
}
<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.