PHP button in a for each loop - php

I am displaying user details on a form using a for each loop. In the for each loop i am adding a button below the user details to delete the information for each user.
<?php
foreach( $Names as $Name )
{
$dbQuery = $db->prepare("select * from user WHERE Name = ?");
$dbQuery->execute(array($Name));
while ($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC))
{
$ID = $dbRow['ID'];
$Email = $dbRow ['Email'];
}
?>
<div class="form-group">
<label for="hotel_id" class=" col-sm-4 control-label">ID:</label>
<div class="col-sm-8 input">
<input type="text" class="form-control" id="ID" name="ID[]" placeholder="Enter City Name" value="<?php echo $ID?>">
</div>
</div>
<div class="form-group">
<label for="Name" class="col-sm-4 control-label">Name:</label>
<div class="col-sm-8 input">
<input type="text" class="form-control" id="Name" name="Name[]" value="<?php echo $Name?>">
</div>
</div>
<div class="form-group">
<label for="Email" class="col-sm-4 control-label">Email:</label>
<div class="col-sm-8 input">
<input type="text" class="form-control" id="Email" name="Email[]" value="<?php echo $Email ?>">
</div>
</div>
<div class ="form-group">
<label for="removeUser" class="col-sm-4 control-label"></label>
<div class="col-sm-8">
<input id="removeUser" name="removeUser" type="submit" value="Remove this user" class="btn btn-danger" onclick="return confirm('Are you sure you want to completely remove this user?');">
</div>
</div>
<?php
}
?>
I know how to delete the users but the problem i am having is that i cant seem to get the id for the specific user when the button is clicked. So far i have only been able to retrieve the last id displayed or all of the id's displayed.
Any help would be appreciated. Thanks.

First, you'll need to move the closing loop brace to the end of the html of each row. Then I recomend you to do a separated URL for deleting a record, and changing the submit button to a anchor tag (by CSS rules you can make it to look like a button), so the anchor will be:
Delete
So in the backend of delete.php you'll have something like:
if(isset($_GET['id'])){
$dbQuery = $db->prepare(sprintf("DELETE FROM user WHERE ID = %s", $_GET['id']));
$dbQuery->execute(array($Name));
}
You'll need to improve this delete query so it will be safe, because people might send you SQL code trough $_GET['id']

Your while loop is wrong, because last one is fetched and then html is rendered. Try add that html under while loop

Please add ID in hidden filed in a form. So you can get id each form submit like normal form field value. Add ID to hidden field value.

Related

Remove null variables from url

I have this form, when I press the submit button i will get redirected to select.inc.php.
But sometimes I dont want to fill out every field of the form but all the "variables"(?) are still getting appended to the URL.
When I only enter an id and will press the button the url will look like this: localhost/inc/select.inc.php?id=1?username=?email=?password=?rank=.
What I want is this: localhost/inc/select.inc.php?id=1
<form action="inc/select.inc.php" method="GET">
<h1 id="h1-select">Select</h1>
<div class="row">
<input type="text" name="id" placeholder="Id">
</div>
<div class="row">
<input type="text" name="username" placeholder="Username">
</div>
<div class="row">
<input type="text" name="email" placeholder="E-Mail">
</div>
<div class="row">
<input type="password" name="password" placeholder="Password">
</div>
<div class="row">
<input type="text" name="rank" placeholder="Rank">
</div>
<div class="row">
<button type="submit" name="submit">Select</button>
</div>
</form>
I want this to show stuff from the MySQL database on a page like when I want to list all users with the rank "1".
If you want to remove all empty strings (you get them as empty strings, not null), you can run $_GET through array_filter():
$_GET = array_filter($_GET, function ($val) {
return $val !== '';
});
Now the $_GET-param will only contain non empty strings.
If you don't need to keep 0 either, you can just do:
$_GET = array_filter($_GET);
That removes any param where the value is falsy

Symfony crawler selectButton method can't get form

here is my html
<form name="station" method="post" action="/stations/new" role="form">
<div class="form-group">
<label class="control-label required" for="station_name">Name</label>
<input type="text" id="station_name" name="station[name]" required="required" maxlength="255" class="form-control" >
</div>
<div class="form-group">
<div class="checkbox">
<label for="station_active">
<input type="checkbox" id="station_active" name="station[active]" value="1" />Active</label>
</div>
</div>
<div class="form-group">
<button type="submit" id="station_submit" name="station[submit]" class="btn btn-primary">Ajouter</button>
</div>
<input type="hidden" id="station__token" name="station[_token]" class="form-control" value="aze123aze" >
</form>
i want to get my form using the crawler. I tried the selectButton method like this
$form = $crawler->selectButton('station[submit]')->form(array());
but i get the error : InvalidArgumentException: The current node list is empty.
what is the problem ?
Unfortunately I have no enough rating to just write a comment instead of put it in the answer.
So, could you please show how are you getting $crawler? Problem might be:
$crawler not point to DOM which contains this form
this form appears on page after some java script actions(Ajax for example), but not sure that this is your case.
The selectButton method accept the value The button text. So Try with:
$form = $crawler->selectButton('Ajouter')->form(array());
Hope this help

Keep saved data in form after revisiting the page

I've created a form with three input fields. I managed to find out how to keep the data after saving the form, but when I leave the page and come back to this form, the fields are empty again.
Here is my code:
<?php
if(isset($_POST['save_home_details'])) {
$home_title = escape_string($_POST['home_title']);
$home_desc = escape_string($_POST['home_desc']);
$home_keywords = escape_string($_POST['home_keywords']);
$query = "INSERT INTO settings(home_title, home_desc, home_keywords) ";
$query .= "VALUES('{$home_title}', '{$home_desc}', '{$home_keywords}') ";
$home_details_query = mysqli_query($conn, $query);
confirm($home_details_query);
echo "<div class='alert alert-success'>Settings saved successfully!</div>";
}
?>
<form action="" method="post">
<div class="form-group">
<label for="home_title">Home title</label>
<input type="text" class="form-control" name="home_title" value="<?php if(isset($_POST['home_title'])) { echo htmlentities ($_POST['home_title']); }?>">
</div>
<div class="form-group">
<label for="home_title">Home description</label>
<input type="text" class="form-control" name="home_desc" value="<?php if(isset($_POST['home_desc'])) { echo htmlentities ($_POST['home_desc']); }?>">
</div>
<div class="form-group">
<label for="home_title">Home keywords</label>
<input type="text" class="form-control" name="home_keywords" value="<?php if(isset($_POST['home_keywords'])) { echo htmlentities ($_POST['home_keywords']); }?>">
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="save_home_details" value="Save settings">
</div>
</form>
Basically what I want here is:
to be able to add value to these fields
save the values
keep the values in the fields whenever I come back to this page
if I click the save settings button, update the values and not add a new record to the database.
I know I haven't added the update query yet, but can I do that on the same page where I insert the values into the database?
Thanks
If you need to store temporarily you can use $_SESSION['anyData'] = $anyData.
When you do not need you can unset($_SESSION['anyData'])

To get record of two dates

I want two dates if I post start date and end date from submit form. I have attendance record table. I want to retrieve this table. How can I do that?
This is my code. It redirects to test.php. How can I retrieve another page? Who can help me kindly?
<form method="post" class="form-horizontal" role="form" action="test.php">
<!--Start Date-->
<div class="col-lg-12">
<div class="form-group required">
<label for="startdate" class="control-label">Start Date:</label>
<input class="form-control" placeholder="Choose Date " type="text" id="datepicker-8" name="startdate" required="required" />
</div>
</div>
<!--End Date-->
<div class="col-lg-12">
<div class="form-group required">
<label for="enddate" class="control-label">End Date:</label>
<input class="form-control" placeholder="Choose Date " type="text" id="datepicker-9" name="enddate" required="required" />
</div>
</div>
<!--Create Button-->
<div class=""> <br/>
<input class="btn btn-success" type="submit" value="Search" />
</div>
</form>
You need to first get both start date and end date in vairable using $_REQUEST like that way.
$start_data = $_REQUEST['startdate'];
$end_data = $_REQUEST['enddate'];
Now you need to use sql query to fetch data from table :
$query = mysql_query("select * from table_name where startdate='$start_data' AND enddate='$end_data'");
while($row=mysql_fetch_array($query)) {
// Print your variable here
}
in test.php:
<?php
var_dump($_POST);
Then submit your form. You will see all the fields and values that were contained in the form. You can access individual fields like so: $startdate = $_POST['startdate']
Good luck!
When you click the submit button the page will be redirected to test.php file, the content which you will write in test.php will be shown. Now to retrieve date from post u can use $startdate=$_POST['startdate'] simillarly for $enddate=$_POST['enddate']. After this use queries to fetch the data from database using the startdate and enddate variable

clearing the textbox input on button click

i do have multiple text boxes called first name,last name,phone no and so on.. and i want to clear/refresh the input typed in it on button click called as refresh or clear.the problem is that the input in these text boxes are not getting refreshed because the data in it is coming from other page and it is getting refreshed when we type some thing in it
my code is as follows:
<div class="control-group">
<input value="<?php echo $model['pros']['oppid'] ?>" name="oppid" type="hidden" id="prospectid" />
<label class="control-label">First Name </label>
<div class="controls">
<input type="text" name="f_name" pattern="[-a-zA-Z]+" required="" title="First Name is required" value="<?php echo $model['pros']["f_name"]; ?>" />
</div>
</div>
<div class="control-group">
<label class="control-label">Last Name </label>
<div class="controls">
<input type="text" name="l_name" pattern="[-a-zA-Z]+" required="" title="Last Name is required" value="<?php echo $model['pros']["l_name"];?>" />
</div>
</div>
<div class="control-group">
<label class="control-label">Primary phone </label>
<div class="controls">
<input type="tel" required="" pattern="[-0-9]+" title="Please Enter correct phone" name="pri_phone" value="<?php echo $model['pros']["pri_phone"];?>" />
</div>
</div>
and the button as
<button class="btn">New</button>
please suggest me on this...
use the following code:
$("#elementid").live('click',function(){
$(this).parents('form').find('input[type="text"]').val('');
});
Assuming that your input elements are inside of a form, and so too is your button, you can do this using jQuery:
$('button.btn').click(function(e) {
e.preventDefault();
$(this).parents('form').find('input[type="text"], input[type="tel"]').val('');
});
you can write code in jquery as:
$('#new').live('click',function(){
$('#fname').val('');
$('#lname').val('');
$('#tel').val('');
});
If your button is not within the form you can use the following
$('button.btn').click(function() {
$('.control-group input:not(:submit)').val('');
});
This will reset any type of input within the control-group divs except for submit inputs
If your reset button is within the form then BenM's answer is what you want.
$(".clearform").live('click',function(){
$(this).parents('form').find('input[type="text"], input[type="tel"],input[type="time"],input[type="email"],input[type="number"],input[type="date"],select').val('');
});

Categories