accept and decline button in one form - php

i've been stuck here i hours and now i don't know what to do. i have 2 buttons. Accept and Decline. i want to change a foreign key value whenever i clicked those.
this is in my blade
<table class="col-5 table table-bordered ml-3 mr-3">
<tbody>
<thead class="thead-dark">
<tr>
<th colspan="2">RENT REQUEST CARD</th>
</tr>
</thead>
<tr>
<td><p class="text-muted"><small>Borrower's Name:</small></p></td>
<td><h6>{{ $borrower_request->borrowers_name }}</h6></td>
</tr>
<tr>
<td><p class="text-muted"><small>Email:</small></p></td>
<td><h6>{{ $borrower_request->email }}</h6></td>
</tr>
<tr>
<td><p class="text-muted"><small>Contact number:</small></p></td>
<td><h6>{{ $borrower_request->contact_number }}</h6></td>
</tr>
<tr>
<td><p class="text-muted"><small>Date of Return:</small></p></td>
<td><h6>{{ $borrower_request->return_date }}</h6></td>
</tr>
<tr>
<td><p class="text-muted"><small>Request Status:</small></p></td>
<td>
<h5 class="text-danger"><em>{{ $borrower_request->requestStatus->request_status }}</em></h5>
</td>
</tr>
<tr>
<td colspan="2" class="text-right">
<button type="submit" class="btn btn-primary">Accept</button>
<button type="submit" class="btn btn-danger">Decline</buttonz>
</td>
</tr>
</tbody>
</table>
i want to change the value of the Request Status. should i put the form on the request status only or in the whole table?
and then here's in mu controller
public function getRequestsId(Request $request, $id)
{
$borrowersrequest = BorrowerRequest::find($id);
$requeststatus = RequestStatus::all();
return view('/borrowsmanager');
}
public function changeStatus(Request $request, $id)
{
$borrowersrequest = BorrowerRequest::find($id);
$borrowersrequest->request_status_id = 2;
$borrowersrequest->save();
return redirect('/borrowsmanager');
}
and then i am thinking how can i make the accept buttons throw value="2" and decline button value="3" if they are on the same form? should split then in 2 different function or maybe in 1? :(

Change your buttons to input
<input type="submit" name="accept" class="btn btn-primary" value="Accept" />
<input type="submit" name="decline" class="btn btn-danger" value="Decline" />
Next in $request check for existence of accept or decline property:
// Something like
$borrowersrequest->request_status_id = isset($request->accept) ? 2 : 3;
$borrowersrequest->save();
If you submit form via ajax - input type="submit" will not be passed to server. You'll have to change your js-script (if any) and pass some flag according to pressed button.

Related

How to edit multiple field from one blade form

Hello I have a form like the below image-
The HTML code for this is-
<table style="width:100%" class="table table-striped">
<tr>
<td><strong>First Name</strong></td>
<td>Test Name</td>
<td><a class="btn btn-success" href="#">Edit</a></td>
</tr>
<tr>
<td><strong>Last Name</strong></td>
<td>Test Name</td>
<td><a class="btn btn-success" href="#">Edit</a></td>
</tr>
<tr>
<td><strong>Username</strong></td>
<td>test_username</td>
<td><a class="btn btn-success" href="#">Edit</a></td>
</tr>
</table>
Now what I am trying here is when I click on edit button I only need to be able to edit that specific field and store the updated value to my database only. All I want to know is, each time I click my form submit button only the updated field value should be passed to my controller method. I can pass my field name by a hidden input.
(I know I can keep a hidden input field in a form and change the field value by javascript. But I need to know if there is any better approach.)
Can I implement this using only one form tag in my Laravel blade? If yes how can I implement this without any touch in JS?
Okay, so you can do this with a css trick, look that I have added the input hidden with css, and instead of the a tag I used a label that is linked to the input with the for and id attributes, so when the user clicks on the label it focus on the input and triggers the css input:focus that shows the input and hide the name.
All is wrapped by a single form that if you add a <button type="input">Send</button> will send all to the controller
But you've mentioned that you just want to send the data that has been changed, unfortunately I don't think that this is possible without javascript, but you should set the value of the input to the original value, so when updated it will update to the same value, no problem at all, acctually almost all edit forms works that way
input {
opacity: 0;
width: 0;
}
input:focus {
visibility: visible;
opacity: 1;
width: 100px;
}
input:focus + span {
display: none;
}
<form>
<table style="width:100%" class="table table-striped">
<tr>
<td><strong>First Name</strong></td>
<td>
<input id="first_name" name="first_name" value="Test Name" />
<span>Test Name</span>
</td>
<td>
<label for="first_name" class="btn btn-success" href="#">Edit</label>
</td>
</tr>
<tr>
<td><strong>Last Name</strong></td>
<td>
<input id="last_name" value="Test Name" />
<span>Test Name</span>
</td>
<td>
<label for="last_name" class="btn btn-success" href="#">Edit</label>
</td>
</tr>
<tr>
<td><strong>Last Name</strong></td>
<td>
<input id="username" value="test_username" />
<span>test_username</span>
</td>
<td>
<label for="username" class="btn btn-success" href="#">Edit</label>
</td>
</tr>
</table>
</form>

undefined index from form post (untypical)

Here is the page with a table, which contains info rows that generates for every row from request.
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Account</th>
<th>Level</th>
<th class="text-center">Action</th>
</tr>
</thead>
<tbody>
<?php
$mysqli=connect();
$result=$mysqli->query("request is here");
while($rows=$result->fetch_assoc()) {
echo "<form action='script.php' method='post'>";
if ($rows['admin']>$_SESSION['admin']||$_SESSION['admin']<6||$rows['online']==1)
$levelinput='<td><input type="text" class="input-sm
form-control" value="'.$rows['admin'].'" disabled></td>
<td class="text-center"><input type="submit" class="btn btn-
simple btn-sm" value="Change" disabled></td>';
else $levelinput='<td><input type="number" class="input-sm
form-control" name="level" value="'.$rows['admin'].'"></td> ----- LEVEL IS HERE
<td class="text-center"><input type="submit" class="btn btn-round
btn-simple btn-sm btn-warning" value="Change"></td>';
if ($rows['ucp_online']>time()-60*5) $name=$rows['login']." <span
class='label label-success'>ONLINE</span>";
else $name=$rows['login'];
echo '
<tr>
<td>'.$rows['id'].'</td>
<td><a href="/admin/user.php?id='.$rows['id'].'" class="btn
btn-simple btn-sm">'.$name.'</a></td>
'.$levelinput.'
</tr>
<input type="hidden" name="admin" value="'.$rows['id'].'">
</form>';
}
$mysqli->close();
?>
</tbody>
</table>
Form moves us to script.php, which takes only $_POST['admin'] index and don't takes $_POST['level'] index. And of course, at the page level value displays correctly.

How to do multiple postrequest in one form?

Situation
I have a form where I can update certain records in the database and delete them. The form is located in a view and I handle the postrequest in the controller.
Question
How can use 2 seperate postrequest in one form? I need one for updating the form and one for removing certain records.
Code - View
<form class="form-horizontal col-md-8" method="post" action="{$URL_FULL}" id="frmMarkerList">
<input type="hidden" name="postrequest" value="updateMarkers">
<input type="hidden" name="postrequest" value="deleteMarkers">
<input type="hidden" name="mk_UUId" value="{$aCoords.Marker_UUId}">
<table class="table table-hover" id="tblMarkers">
<thead>
<tr>
<th class="col-md-10">Coordinaten</th>
</tr>
</thead>
<tbody> {*'Marker_Id','Marker_UUId','Markercat_Id','Lat','Lng','Titel','Url'*}
{foreach $aCoords|default:array() as $mk_coords} {*'Woning_Id','Woning_UUId','Projectwoning_Id','Projectwoning_Titel','Project_Id','Project_Titel','Woning_Bouwnr'*}
<tr id="row_{$mk_coords.Marker_Id}">
<td class="td-shapes">
<strong>Lat:</strong>
<input type="text" class="coord" name="Coords[{$mk_coords.Marker_Id}]" data-wid="{$mk_coords.Marker_Id|escape}" value="{$mk_coords.Lat|escape}" style="width: 250px;margin:4px 0 0 0">
<strong>Lng:</strong>
<input type="text" class="coord" name="Coords[{$mk_coords.Marker_Id}]" data-wid="{$mk_coords.Marker_Id|escape}" value="{$mk_coords.Lng|escape}" style="width: 250px;margin:4px 0 0 0">
</td>
<td>
<button type="button" class="close" aria-label="Close"><span aria-hidden="true">×</span></button> {*This is the delete request button*}
</td>
</tr>
{/foreach}
</tbody>
</table>
<input type="submit" class="btn btn-primary" value="Bijwerken"> {*This is the update request button*}
</form>
Code - Controller
if($this->getRequest()->getPost('postrequest')){
//verwerk request eerst
switch($this->getRequest()->getPost('postrequest')){
case 'updateMarker':
var_dump($aCoords);
break;
case 'deleteMarker':
var_dump($aCoordsCat);
break;
default:
break;
}
}

Form with two buttons that needs two different functions

I have a login form here with two buttons namely login and register. Both of them are under a form so the form has an action and both of them are doing the same function. I need them to be linked to my different php pages. Here is my code:
<table>
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table>
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td">Username</td>
<td>:</td>
<td><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit1" value="Login"><input type="submit" name="Submit2" value="Sign Up""></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
If I click on my login button, I want it to be linked to "checklogin.php" which is same as the link on the field. And if I click on sign up, it will be linked to "signup.php".
Submit to one place. Worry about what to do with the data on the server.
<?php
if (isset($_POST['Submit1'])) {
include('login.php');
} else if (isset($_POST['Submit2'])) {
include('signup.php');
} else {
# default behaviour
}
My advise is don't use any of the buttons as a submit button.
Use the onclick and link to 2 separate functions - login() and register().
Inside the login() and register() function you can use jquery to submit the form to the a different url from each function.
In your Javscript you need to define the functions
<script>
function login() {
//make form post here
}
function register() {
//make form post here
}
</script>
Read this answer to get more details on how to submit a form using Ajax.
Hope this helps!
Without changing server side code this can be easily achieved by using javascript to change form action attribute before submitting. Try this pattern:
<input type="submit" onclick="this.form.action = 'another_url';" />
this functionality can be achieved using JavaScript this is one of the option.If you use javascript you need not use form tag.
<input type="button" name="Submit1" value="Login" onclick="login()">
<input type="button" name="Submit2" value="Sign Up" onclick="signup()">
JS Functions:
function login()
{
window.location = 'http://www.yourdomain.com'
}
function signup()
{
window.location = 'http://www.yourdomain.com'
}
Just remind to give type="button" instead of type="submit"
You can carry out validations for the form if you use JS.Hope this helps.
You can create a function in JavaScript linktoURL. Call that function on button onClick event and pass the URL as the parameter to that function where you want to redirect
function linktoURL(url)
{
document.form1.action = url;
document.form1.sumbit();
}
<table>
<tr>
<form name="form1" id="form1" method="post" action="checklogin.php">
<td>
<table>
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td">Username</td>
<td>:</td>
<td><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td>
<input type="button" name="Submit1" value="Login" onclick="linktoURL('checklogin.php');">
<input type="button" name="Submit2" value="Sign Up" onclick="linktoURL('signup.php');"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

Jquery not focusing on the correct fields

I am trying to have the focus in my isbn text box or the edition text boxafter running a calculation in jquery, but instead it is going to the first button on the page or the sales rank field.
My html is:
<td><label>ISBN</label></td>
<td><input type="text" id="isbn" name="isbn" /></td>
<td></td>
<td rowspan=7><a id="detailURL" href="#" target="_blank"><img id="bookCover" src="../images/imgNotFound.gif" height="200px" /></a></td>
<td rowspan=7><input type="button" id="isIE" name="isIE" value="IE Price Reduction" />
<br/><br/><input type="button" id="isAIE" name="isAIE" value="AIE Price Reduction" />
<br/><br/><input type="button" id="isModHighlight" name="isModHighlight" value="Moderate Highlighting" />
<br/><br/><input type="button" id="isHeavHighlight" name="isHeavHighlight" value="Heavy Highlighting" />
<br/><br/><input type="button" id="isMissingSupply" name="isMissingSupply" value="Missing Supply" />
<br /><br/><input type="button" id="waterDamage" name="waterDamage" value="Water Damage / Poor Cond." />
</td>
<td rowspan=7>
<!-- SUPPLY CHECK TABLE -->
<table id="supply" summary="Check Supply" width="10%" border="1" align="right">
<caption><h3>Check Supply!</h3></caption>
<thead>
<tr>
<th scope="col" class="rounded-isbn">Supply:</th>
<!--<th scope="col" class="rounded-isbn">Who Requires:</th>-->
</tr>
</thead>
<tbody>
</tbody>
</table>
</td>
</tr>
<tr>
<td><label for="quantity" class="label">Quantity</label></td>
<td><input type="text" id="quantity" name="quantity" /></td>
<td></td>
</tr>
<tr>
<td><label for="payPrice" class="label">Price to Pay</label></td>
<td><input type="text" id="payPrice" name="payPrice"/></td>
<td></td>
</tr>
<tr>
<td><label>Edition</label></td>
<td><input type="text" id="edition" name="edition" readonly="readonly"/></td>
<td></td>
</tr>
<tr>
<td><label>Title</label></td>
<td><input type="text" id="title" name="title" /></td>
<td></td>
</tr>
<tr>
<td><label>Sales Rank</label></td>
<td><input type="text" id="salesRank" name="salesRank" readonly="readonly" /></td>
<td></td>
</tr>
<tr>
<td></td><td><input type="button" id="buy" value="Buy" /></td><td></td>
</table>
</form>
The jquery in question is:
if($('#payPrice').val() <= 0 ) {
$(':text')[0].focus();
} else {
$("#edition").focus();
}
When the value of #payPrice is <=0, it is defaulting to the first button id="isIE" and not the isbn field. If the value of #payprice is >0 it is focusing on the sales rank text area, and not edition. I have tried setting a timeout, which did not work as well as $("#isbn).focus();, which didn't work either. I changed it to $(':text')[0].focus(); to see if that would work, but it still went to the first button.
What am I doing wrong here? How do I correct this to make it work as it should?
have you tried
if(parseFloat($('#payPrice').val()) <= 0 ) {
...
}
if parseFloat make no difference try this:
if($('#payPrice').val()<=0){
$("input[name='isbn']").focus();
}else{
$("input[name='edition']").focus();
}
otherwise you could see that example:
function sampledFunction()
{
window.setTimeout(function ()
{
$('#mobileno').focus();
}, 0);
return false;
}
read here
I'd recommend using a better selector:
like $('[type=text]') or $('input:text')
See: jQuery text selector
Here is the final outcome, thanks to JackTurky who pointed me in the right direction.
//focus on either isbn or edition
setTimeout(function() {
if($('#payPrice').val() <= 0 ) {
$("#isbn").focus();
} else {
$("#edition").focus();
}
}, 10);

Categories