interface php and html, in foreach loop - php

I have a PHP page that shows me data from the database. I'm inserting a button that, if pressed, deletes the record. The problem is that if I push the button the action is done on all the records, because I can't uniquely identify the click on the button
some code
foreach ( $associati as $associato ) {
echo "<hr />";
echo "Nome associato : ".$associato->Nome."</br>";
echo "Codice Fiscale : ".$associato->CF."</br>";
echo "<hr />";
if(isset($_POST["numerazione"])){
echo "Hello world"; //Query for delete
}
?>
<form method="POST" action="">
<input type="submit"
name="numerazione"
value="Elimina utente"
onclick="return confirm('Are you sure?')"
/>
</form>
<?php
}
How can I do to uniquely identify the button?

You can add a hidden field to each form that contains the unique identifier of the data, that means when you click the button, it will create a POST request, and in that POST request you can get the ID of the clicked record by doing $_POST['unique-id'], also make sure to populate the value of that hidden field using PHP
<?php
foreach ( $associati as $associato ) {
echo "<hr />";
echo "Nome associato : ".$associato->Nome."</br>";
echo "Codice Fiscale : ".$associato->CF."</br>";
echo "<hr />";
if(isset($_POST["numerazione"])){
$numerazione = $_POST["unique-id"];
echo "Unique record is : ".$numerazione."</br>";
}
?>
<form method="POST" action="">
<input type="hidden" name="unique-id" value="<?php echo $associato->CF; ?>" />
<input type="submit"
name="numerazione"
value="Elimina utente"
onclick="return confirm('Are you sure?')"
/>
</form>
<?php
}
?>

Pass the unique information (e.g. $id or $associato->id or whatever the variable which can identify the record) when the form is submitted
<form method="POST" action="">
<input type=hidden name=id value=<?php echo $id; ?>>
<input type="submit"
name="numerazione"
value="Elimina utente"
onclick="return confirm('Are you sure?')"
/>
</form>

Solution for this problem:
if(isset($_POST["mod"]) and $_POST["id"]== $associato->ID ){
echo "Hello world";
}
?>
<form method="POST" action="">
<input type="hidden" name="id" value=<?php echo $associato->ID; ?>>
<input type="submit"
name="mod"
value="valore"
onclick="return confirm('Are you sure?')"
/>
</form>

Related

How to prompt for button press confirmation?

I'm loading data in to a html table using following function. It also creates a delete button in front of each row. I want to prompt user to either confirm or cancel. But the code does not work and it does not prompt for confirmation. Can someone show me how to do it properly?.
I use (onclick="return confirm('Are you sure?')") to prompt.
//This function will list categories
function listCategories($sqlString)
{
$result = mysqli_query($this->connectToDb(), $sqlString);
if(mysqli_num_rows($result) > 0)
{
while ($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td>".$row['cat_id']."</td>";
echo "<td>".$row['it_category']."</td>";
echo '<td>
<form action="" method="POST">
<input type="hidden" name="catid" value="'.$row['cat_id'].'">
<input class="btn btn-outline-success" type="submit" name="Delete" value ="Delete" onclick="return confirm('Are you sure?')">
</form>
</td>';
echo "</tr>";
}
}
}
You need to escape the quotes around Are you sure. They're matching the quotes around the string argument to echo.
echo '<td>
<form action="" method="POST">
<input type="hidden" name="catid" value="'.$row['cat_id'].'">
<input class="btn btn-outline-success" type="submit" name="Delete" value ="Delete" onclick="return confirm(\'Are you sure?\')">
</form>
</td>';

Change only the clicked button to an new one

I have a button inside a table row which changes with a boolean value.
<tr id="<?php echo $zeile['id'] ?>" value="<?php echo $zeile['id'] ?>" class="task">
<td >
<form method="POST" action="main.php" value="<?php echo $zeile['id']; ?>">
<?php if ($done == true): ?>
button type="submit" name="checkbtn" id='<?php echo $zeile['id']; ?>' >
Check
</button>
<input type="hidden" name="id" value="<?php echo $zeile['id']; ?>">
<?php else: ?>
<button type="submit" name="uncheckbtn" id='<?php echo $zeile['id']; ?>'>
UnCheck
</button>
<input type="hidden" name="id" value="<?php echo $zeile['id']; ?>">
<?php endif ?>
</form>
</td>
The boolean value is change after the button is clicked here for ('checkbtn' and 'uncheckbtn')
$done = true;
if(isset($_POST['checkbtn'])){
*sqlvariables*
$done = false;
*sql*
}
with my jquery I want that I can click anywhere on the row to do a button click:
$(".table tr").on('click', function() {
$(this).toggleClass('highlighttask');
$(this).find('button[name="checkbtn"]').click();
});
The Problem is that every button changes to unchecked if I click on one row.
I think that I need to work with id's, but I don't know how.
You already have click event handler on tr so no need to use closest, just use find button
$(".table tr").on('click', function() {
$(this).toggleClass('highlighttask');
$(this).find('button[name="checkbtn"]').click();
});

Retrieving table field when checkbox values are equal to another field

I have language table that include LID,LNAME fields in a UI.
I have many checkboxes for LNAME.
When the user checks some of them and submits, the system display LID for the checked checkbox.
My HTML:
<input type ="checkbox" name="language[]" vaue="English">English
In PHP:
$lang=$_POST['language'];
$sql="select LID from lqnguage where lname=$lang";
Please can anyone help me please I'm having trouble.
Try below code.
PHP
// This is array for language.
$l_array=array();
$l_array[1]='Gujarati';
$l_array[2]='English';
$l_array[3]='Spenish';
// Get selected languages.
if(isset($_POST['submit']) && $_POST['submit']=='Submit'){
echo "<pre>";
print_r($_POST['language']);
}
HTML
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<?php foreach($l_array as $lid=>$lname){ ?>
<input type="checkbox" name="language[]" value="<?php echo $lid; ?>" /><?php echo $lname; ?><br />
<?php } ?>
<input type="submit" name="submit" value="Submit" />
</form>
Now, you can get selected language_id(as key) and language_name(as value) in Array.

HTML Forms in PHP

i am writing html form code with in php script
here it is
<?php
$problem_code="STR";
echo '<form name="submit-button" action="/codejudge/submit.php?id='.$problem_code.'">';
echo '<button type="submit" >Submit</button>';
echo "</form>";
?>
But after submitting url look like this localhost/codejudge/submit.php ?
but it should be like this localhost/codejudge/submit.php?id=STR
If a form is method="GET" (which is the default), as this one is, then submitting it will erase the existing query string in the action.
Store the data in a hidden input instead.
<?php
$problem_code="STR";
?>
<form name="submit-button" action="/codejudge/submit.php">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($problem_code); ?>">
<button type="submit">Submit</button>
</form>
You should specify a method of form submit.
$problem_code="STR";
echo '<form method=post name="submit-button" action="/codejudge/submit.php?id='.$problem_code.'">';
echo '<button type="submit" >Submit</button>';
echo "</form>";

Passing PHP form variables from one page to other

my php configuration on server are showing i can post variables to maximum size upto 8MB , thats enough .. but how to check for number of variables , sever is running ubuntu 4.4 , php .
i have a page which takes students marks and send them to a action page , but on action page doing echo for the post variables nothing is being displayed , where are doing an echo "hello"; this shows ...
this is the page which sends the variables
<form name="frm" action="marklistI.php" method="POST" class="" >
<?php $tb->displayTable() ?>
<div class="mainframe">
<input type="hidden" name="batch" value="<?php print $_GET['batch']; ?>"/>
<input type="hidden" name="sem" value="<?php print $_GET['sem']; ?>" />
<input type="hidden" name="chance" value="<?php print $_GET['chance']; ?>"/>
<input name="submit" type="submit" class="hide" value="Save"/>
<input type="hidden" name="url" value="<?php print $_SERVER['REQUEST_URI']; ?>"/>
</div>
</form>
and this are the variables are coming to action page .. but on echo they are not showing any value .
$dept =$_COOKIE['dept'];
$join=$_POST['batch'];
$type='e';
$sem=$_POST['sem'];
$chance=$_POST['chance'];
try placing this code on your action page:
if (isset($_GET)) {
echo "<h3>GET METHOD</h3>";
var_dump($_GET);
}
if (isset($_POST)) {
echo "<h3>POST METHOD</h3>";
var_dump($_POST);
}
if (isset($_COOKIE)) {
echo "<h3>COOKIE METHOD</h3>";
var_dump($_COOKIE);
}
See which method returns your variables and use it, otherwise, you are not filling any values on the form.
this is your code:
<form name="frm" action="marklistI.php" method="POST" class="" >
<?php $tb->displayTable(); ?>
<div class="mainframe"> <input type="hidden" name="batch" value="<?php print $_GET['batch']; ?>"/>
<input type="hidden" name="sem" value="<?php print $_GET['sem']; ?>" />
<input type="hidden" name="chance" value="<?php print $_GET['chance']; ?>"/>
<input name="submit" type="submit" class="hide" value="Save"/>
<input type="hidden" name="url" value="<?php print $_SERVER['REQUEST_URI']; ?>"/>
</div>
</form>
One possible reason for your issue:
You use "_GET[]" variables here but the form is POST.
GET and POST are two different methods to send data, GET is in the URL path (a=&b=&c=) while POST is hidden in the HTML headers.
So make sure you read those results as "$_POST['name']" and not GET.
I suggest this in the "receiving script" for debugging:
var_dump($_GET);
var_dump($_POST);
And in your browser use Chrome or Firefox + Firebug and Press "f12".
In that debugger you can catch the POST when you click the button and you can look which variables were sent.
That should help you debug your issue fast.
One other suggestion, I personally would write the code less "mixed".
It makes it hard to read and hard to modify.
Why not like this:
<?php
echo "
<form name='frm' action='marklistI.php' method='POST' class='' >".
$tb->displayTable().
"<div class='mainframe'>
<input type='hidden' name='batch' value='$_GET[batch]'/>
<input type='hidden' name='sem' value='$_GET[sem]' />
<input type='hidden' name='chance' value='$_GET[chance]'/>
<input name='submit' type='submit' class='hide' value='Save'/>
<input type='hidden' name='url' value='$_SERVER[REQUEST_URI]'/>
</div>
</form> ";
?>
My guess for your problem is that those values in the formular are actually empty, that's why you don't receive anything.

Categories