I am creating a simple MVC site that uses CRUD. For the delete function I have a button, the code below is what I am trying to use to send the rowID to the controller to handle.
I am having trouble in that the code doesnt print out on the screen correctly, The php code is being printed as well as the button.
The rowID isnt being passed either and I am wondering if my approach with the php code is the correct way?
Code
<?php
$itemsDAO = new ItemsDAO();
$result=$itemsDAO->getItems();
foreach ( $result as $row ) {
$uid = $row['id'];
var_dump($uid);
?>
<form action="index.php" method="post">
<fieldset>
<input id='action' type='hidden' name='action' value='deleteItem' />
<p>
<div class="form-group">
<div class="controls">
<input type="hidden" id="fId" name="fId" value="<?php echo $uid; ?>">
<input type="submit" class="btn btn-success" value="Delete">
</div>
</div>
</p>
</fieldset>
</form>
<?php } ?>
Delete Function
public function getItems () {
$sqlQuery = "SELECT *";
$sqlQuery .= " FROM items";
$result = $this->getDbManager()->executeSelectQuery($sqlQuery );
return $result;
}
You could try a string replace in you forloop if you have one.
I think it would look something like this
foreach ( $this->model->itemList as $row )
$HTMLItemList .= "<li><strong>" . $row ["title"] . ": </strong>" . $row ["price"] . "<blockquote>" .
$row ["description"] . " " . str_replace("value2replace", $row['id'], $buttons) . "" . $update . "</blockquote></li>";
And then you would change your form to the following
<form action="index.php" method="post">
<fieldset>
<input id='action' type='hidden' name='action' value='deleteItem' />
<div class="form-group">
<div class="controls">
<input type="hidden" id="fId" name="fId"
value="value2replace"> <input type="submit"
class="btn btn-success" value="Delete" />
</div>
</div>
</fieldset>
Assuming you want a delete form for each and every result, you need to wrap the HTML inside the foreach statement:
<?php
$result=$itemsDAO->getItems();
foreach ($result as $row) {
$uid = $row['id'];
?>
<form action="index.php" method="post">
<fieldset>
<input id='action' type='hidden' name='action' value='deleteItem' />
<p>
<div class="form-group">
<div class="controls">
<input type="hidden" id="fID" name="uid" value="<?php echo $uid; ?>">
<input type="submit" class="btn btn-success" value="Delete">
</div>
</div>
</p>
</fieldset>
</form>
<?php } ?>
Related
EDIT: My edit button refuse to work.
I'm trying to edit this page, but it seems to be breaking when I click the edit button. it is retrieving data from the database and displaying on faculty.php successfully while if I try to edit it shows a blank page. what is wrong with my code, please.
faculty.php: a separate page where i have my edit button
<td>
<form action="facultyedit.php" method="POST">
<input type="hidden" name="edit_id" value="<?php echo $row['id']; ?>" >
<button type="submit" name="edit_data_btn" class="btn btn-success">EDIT</button>
</form>
</td>
facultyedit page: where i receive the edit button
<?php
if (isset($_POST['edit_data_btn']))
{
$id = $_POST['edit_id'];
$query = " SELECT * FROM register WHERE id='$id' ";
$query_run = mysqli_query($connection, $query);
foreach ($query_run as $row ) {
?>
<form action="" method="POST" >
<input type="text" name="edit_id" value="<?php echo $row['id'] ?>" >
<div class="form-group">
<label>Name:</label>
<input type="text" name="edit_name" value="<?php echo $row['name']; ?>" class="form-control" >
</div>
<div class="form-group">
<label>Designation:</label>
<input type="text" name="edit_designation" value="<?php echo $row['design']; ?>" class="form-control" >
</div>
<div>
<label>Description:</label>
<input type="text" name="edit_description" value="<?php echo $row['descrip']; ?>" class="form-control" >
</div>
<div class="form-group">
<label>Upload Image:</label>
<input type="file" name="edit_faculty_image" id="faculty_image" value="<?php echo $row['image']; ?>" class="form-control" >
</div>
<div class="">
Cancel
<button type="update" name="update_aboutusbtn" class="btn btn-primary">Update</button>
</div>
</form>
<?php
}
}
?
I have fixed the issue:
The problem was that I was selecting a register table from the database instead of the faculty table.
I am quite new to PHP so I need some help with my project.
I have a form on the page where the user writes its content and name, like this:
<div class="white shadow padding-10 margin-bottom">
<form class="form xl relative" method="POST" action="
<?php echo $_SERVER['PHP_SELF']; ?>
">
<div class="col-1 col-persist -margin">
<img class="pull-left width-100 round" src="images/logo.gif" alt = "logo"/>
</div>
<div class="col-9 col-persist gutter-h-10 padding-top-5 -margin">
<textarea placeholder="What is new?" name="content"></textarea>
<input type="text" name="name" placeholder="What is your name?">
</div>
<div class="col-2 col-persist -margin">
<input type="button" name="submit" value="Send">
</div>
</form>
</div>
PHP_SELF I use to stay on the same page
In php part I have this:
$userName = "";
$userInput = "";
if($_SERVER["REQUEST_METHOD"] === "POST"){
$userName = $_POST['name'];
$userInput = $_POST["content"];
}
if(isset($_POST)){
echo '<div class="white shadow padding-10 margin-bottom">';
echo '<form class="form xl relative" method="POST">';
echo '<div class="col-1 col-persist -margin">';
echo '<img class="pull-left width-100 round" src="images/logo.gif" alt = "logo"/>';
echo '</div>';
echo '<div class="col-9 col-persist gutter-h-10 padding-top-5 -margin">';
echo '<textarea placeholder="What's new?" name="content">'.$userInput.'</textarea>';
echo '</div>';
echo '<input type="text" name="name" placeholder="What's your name?">'.$userName;
echo '</div>';
echo '</form>';
echo '</div>';
}
However, this code does not work at all. When debugging, the browser shows some problem in these lines:
echo '<textarea placeholder="What's new?" name="content">'.$userInput.'</textarea>';
echo '</div>';
echo '<input type="text" name="name" placeholder="What's your name?">'.$userName;
What is the problem? I guess it is something in syntax, but maybe more in code logic itself?
Thanks in advance!!!
First your form needs a submit button, so change <input type="button" name="submit" value="Send"> to <input type="submit" name="submit" value="Send">
Also, you don't need to use PHP_SELF, simply leaving the action attribute as action="" will keep it on the same page.
As for the error in the php, you have single quote marks that are not escaped. This should work:
echo '<textarea placeholder="What\'s new?" name="content">'.$userInput.'</textarea>';
echo '</div>';
echo '<input type="text" name="name" placeholder="What\'s your name?">'.$userName;
You need to change <input type='submit' name='submit' value='send'> in your html and not button.
I have two submission buttons on my webpage which are assigned to do different things. My problem is that clicking 1 of the buttons is invoking the actions of both buttons. How can I make it so that clicking 1 button only invokes the associated form submission action?
(This is my first day using php).
First form:
<form id="frm1" onsubmit="document.getElementById('frm1').submit();">
<center><br>
<input style="width:150px" name="data" type="text" autocomplete="off" placeholder="Add Time (hh:mm:ss)">
</input>
<input name="id" type="hidden" value="<?php echo $ID; ?>">
<input name="addTime" type="hidden" value="true">
<button style="width:40px" type="submit" class='btn btn-success pull-right'>
<i class="icon-plus"></i>
</button
</form>
Second form:
<form id="frm2" onsubmit="document.getElementById('frm2').submit();">
<center>
Edit Group:
<?php
#
$styles = sqlToQuery("SELECT groupID, Name from sha_scheduler_dev.sha_scheduler_map_job_group");
echo '<select style="width:100" name="groupNum">';
for($i = 0; $i < count($styles); $i ++){
echo ' <option style="width:100" value="'.$styles[$i][0].'">'.$styles[$i][1].'</option>';
}
echo '</select><br>';
?>
<input name="editGroup" type="hidden" value="true"><br>
<button type="submit" class='btn btn-info pull-right'>
Submit Group
</button>
</form>
Checking if buttons are submitted:
if ($_GET['addTime']==='true'){
sqlToQuery("replace into sha_scheduler_dev.sha_scheduler_map_jobtimes (job_ID, time_to_run) values (".$ID." ,'".$_GET['data']."')");
echo '<script>window.location.assign("'.$redir.'?id='.$ID.'");</script>';
}
if ($_GET['editGroup']==='true'){
sqlToQuery("UPDATE sha_scheduler_dev.sha_scheduler_jobs a SET a.group = '".$_GET['groupNum']."' WHERE a.job_ID = ".$ID." ");
echo '<script>window.location.assign("'.$redir.'?id='.$ID.'");</script>';
}
In your first form, button tag is not closed properly, that's why first form is tag is not closed properly,use this,
<form id="frm1" onsubmit="document.getElementById('frm1').submit();">
<center><br>
<input style="width:150px" name="data" type="text" autocomplete="off" placeholder="Add Time (hh:mm:ss)">
<input name="id" type="hidden" value="<?php echo $ID; ?>">
<input name="addTime" type="hidden" value="true">
<button style="width:40px" type="submit" class='btn btn-success pull-right'>
<i class="icon-plus"></i>
</button>
</center>
</form>
Also change your php code like this
if ( isset($_GET['addTime']) && $_GET['addTime']==='true'){
sqlToQuery("replace into sha_scheduler_dev.sha_scheduler_map_jobtimes (job_ID, time_to_run) values (".$ID." ,'".$_GET['data']."')");
echo '<script>window.location.assign("'.$redir.'?id='.$ID.'");</script>';
}
if ( isset($_GET['editGroup']) && $_GET['editGroup']==='true'){
sqlToQuery("UPDATE sha_scheduler_dev.sha_scheduler_jobs a SET a.group = '".$_GET['groupNum']."' WHERE a.job_ID = ".$ID." ");
echo '<script>window.location.assign("'.$redir.'?id='.$ID.'");</script>';
}
Give you're submit buttons a value and a name catch the different values using $_GET(in your case) or $_POST
Update: (just to give it a try maybe)
First Form
<form id="frm1" action="" method="post">
<center><br>
<input style="width:150px" name="data" type="text" autocomplete="off" placeholder="Add Time (hh:mm:ss)" />
<input name="id" type="hidden" value="<?php echo $ID; ?>" />
<input style="width:50px" type="submit" class='btn btn-success pull-right' name="submit_form1" value="submit" />
<i class="icon-plus"></i>
</button>
</center>
</form>
Second Form
<form id="frm2" name="frm2" action="" method="post">
<center>
Edit Group:
<?php
$styles = sqlToQuery("SELECT groupID, Name from sha_scheduler_dev.sha_scheduler_map_job_group");
echo '<select style="width:100" name="groupNum">';
for ($i = 0; $i < count($styles); $i ++)
{
echo ' <option style="width:100" value="' . $styles[$i][0] . '">' . $styles[$i][1] . '</option>';
}
echo '</select><br>';
?>
<input type="submit" class='btn btn-info pull-right' name="submit_form2" value="Submit Group" />
</form>
PHP
<?php
if (filter_input(INPUT_POST, "submit_form1"))
{
// {Check for empty var on data}
sqlToQuery("replace into sha_scheduler_dev.sha_scheduler_map_jobtimes (job_ID, time_to_run) values (" . $ID . " ,'" . $_GET['data'] . "')");
echo '<script>window.location.assign("' . $redir . '?id=' . $ID . '");</script>';
}
if (filter_input(INPUT_POST, "submit_form2"))
{
sqlToQuery("UPDATE sha_scheduler_dev.sha_scheduler_jobs a SET a.group = '" . $_GET['groupNum'] . "' WHERE a.job_ID = " . $ID . " ");
echo '<script>window.location.assign("' . $redir . '?id=' . $ID . '");</script>';
}
?>
This is pretty tight and keeps your url clean.
I'm currently in the process of coding a MySQL search page and I'm unable to work out how to make it so that if there's no data put in one form it'll do nothing, and if there's data in the other form it'll search the database for that value.
<?PHP
echo '<h3 class="hp-1">Kick Logs</h3><div class="wrapper">
<div class="logcol-1"><form name="form1" id="mainForm" method="post"enctype="multipart/form-data" action="' . $_SERVER['REQUEST_URI'] . '">
<input name="name" type="text" id="name" placeholder="Players Name">
</form>';
echo '<form name="form2" id="mainForm" method="post"enctype="multipart/form-data" action="' . $_SERVER['REQUEST_URI'] . '"><input name="reason" type="text" id="reason" placeholder="Kick Reason"></form>';
$name = mysql_real_escape_string($name);
$reason = mysql_real_escape_string($reason);
$kicklogname = mysql_query("SELECT * FROM `log1` WHERE `user` LIKE '%$name%'") or die(mysql_error());
$kicklogreason = mysql_query("SELECT * FROM `log1` WHERE `user` LIKE '%$reason%'") or die(mysql_error());
if($name == ""){
echo "You must enter a name to search"; }
else {
echo '<table width="700" border="0">
<tr class="listheader">
<td width="100" bgcolor="#afe6ff">Username</td>
<td width="220" bgcolor="#afe6ff">Reason</td>
</tr>';
while($row = mysql_fetch_array($kicklogname))
{
echo '<tr><td bgcolor="#daf4ff" class="contentleft">';
echo $row['user'];
echo '</td><td bgcolor="#eefaff" class="contentright">';
echo $row['reason'];
echo '</td></tr>';
}
echo '</table></div></div>';
}
?>
Update
Sorry, re-read the question. To check multiple forms on the same page you need to add a submit button to each form and give it a name:
<?php
if (!empty($_POST) && isset($_POST['reasonsubmit'])) {
echo 'Submitted reason form';
}
if (!empty($_POST) && isset($_POST['namesubmit'])) {
echo 'Submitted name form';
}
?>
<form action="reason.php" method="post">
<input type="text" name="reason" id="reason" />
<input type="submit" name="reasonsubmit" />
</form>
<form action="name.php" method="post">
<input type="text" name="name" id="name" />
<input type="submit" name="namesubmit" />
</form>
Alternatively, if you didn't want to give the submit button a name you can just add a hidden field to each form and you will get the same behavior.
<form action="reason.php" method="post">
<input type="hidden" name="reasonsubmit" id="reasonsubmit" />
<input type="text" name="reason" id="reason" />
<input type="submit" />
</form>
<form action="name.php" method="post">
<input type="hidden" name="namesubmit" id="namesubmit" />
<input type="text" name="name" id="name" />
<input type="submit" />
</form>
Is it possible to attach the data to the url like this?
<?php
$query = mysql_query(......) or die(mysql_error());
while($row = mysql_fetch_assoc($query)){
echo "<a href='#foo?id=" . $row['Id'] . "'>" . $row['Name'] . "</a>";
}
?>
<div id="foo">
<form action="process.php" method="post">
<input type="hidden" name="id" value="<?php echo $_GET['id'] ?>"/>
<input type="text" name="message" value=""/>
<input type="submit" name="sendMessage" value="Send Message"/>
</form>
</div>
I want to pass the $row['Id'] to the div so that it can be put in the value of the hidden input type. Thank you for your answers and suggestions!
Try changing your code like this.
<?php
$query = mysql_query(......) or die(mysql_error());
while($row = mysql_fetch_assoc($query)){
echo "<a href='#foo?id=" . $row['Id'] . "'>" . $row['Name'] . "</a>";
$id = $row['Id'] ;
}
?>
<div id="foo">
<form action="process.php" method="post">
<input type="hidden" name="id" value="<?php echo $id ; ?>"/>
<input type="text" name="message" value=""/>
<input type="submit" name="sendMessage" value="Send Message"/>
</form>
</div>
Cheers!
Prasad.
If you're trying to take the row number that is clicked on and then make that the value for the hidden input, You probably want to use some javascript/jQuery. Its not even that difficult. Take a look at an example I made here
<?php echo "<div id=".$_GET['id'].">"?>....html....</div>
When you loop this, I don't know how you're applying it but if it's with a lot of other stuff, you might want to consider building an array and then outputting to a template format.