How to edit multiple field from one blade form - php

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>

Related

accept and decline button in one form

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.

How to edit multiple values in database in one time

I am making an admin panel for my website.
I have problem editing many values in a database at one time.
My website is https://jaguarz.cf but the admin panel is not visible to common visitors and only i can control it
Warning: count(): Parameter must be an array or an object that implements Countable
I get this error while counting no of id s i want to change
if (isset($_POST['edit_features'])) {
$count=count($_POST['id_edit']);
for($i=0;$i<$count;$i++){
$sql1="UPDATE projects
SET name='" . $_POST['name_edit'][$i] . "',
link='" . $_POST['link_edit'][$i] . "',
rank='" . $_POST['rank_edit'][$i] . "'
WHERE id='" . $_POST['id'][$i] . "'";
$result1=mysqli_query($db,$sql1);
}
}
Here is an example of my form:
<form method="post" action="admin_panel" style="margin:auto;" class="resize form-style-8">
<div class="table-responsive" style="width:99%;">
<table class="table table-bordered">
<thead>
<tr>
<th style="color: rgb(0,0,0);font-size: 20px;">Id</th>
<th style="color: rgb(0,0,0);font-size: 20px;">Name</th>
<th style="color: rgb(0,0,0);font-size: 20px;">Link</th>
<th style="color: rgb(0,0,0);font-size: 20px;">Rank</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" name="id_edit" value="1" readonly></td>
<td><input type="text" name="name_edit" value="Videos"></td>
<td><input type="text" name="link_edit" value="https://www.youtube.com/channel/UCXqIrdlG-gsfSGuFUt0v-1g"></td>
<td><input type="text" name="rank_edit" value="AAA"></td>
</tr>
<tr>
<td><input type="text" name="id_edit" value="2" readonly></td>
<td><input type="text" name="name_edit" value="Basic Message Encrypter And Decrypter"></td>
<td><input type="text" name="link_edit" value="project?page=en-decrypter"></td>
<td><input type="text" name="rank_edit" value="B"></td>
</tr>
<tr>
<td><input type="text" name="id_edit" value="5" readonly></td>
<td><input type="text" name="name_edit" value="Calculator"></td>
<td><input type="text" name="link_edit" value="project?page=calculator"></td>
<td><input type="text" name="rank_edit" value="E"></td>
</tr>
</tbody>
</table>
<div style="margin: 20px 0px 0px 0px">
<button type="submit" name="edit_features" class="btn btn-info text-justify" style="margin: 10px 0px 10px 0px; width:auto; text-align:center !important;">Edit Projects</button>
</div>
</div><br><br>
<div class="table-responsive" style="width:99%;">
<table class="table table-bordered">
<thead>
<tr>
<th style="color: rgb(0,0,0);font-size: 20px;">Id</th>
<th style="color: rgb(0,0,0);font-size: 20px;">Name</th>
<th style="color: rgb(0,0,0);font-size: 20px;">Link</th>
<th style="color: rgb(0,0,0);font-size: 20px;">Rank</th>
</tr>
</thead>
<tbody>
<tr>
<td>Auto Generated</td>
<td><input type="text" name="name_add"></td>
<td><input type="text" name="link_add"></td>
<td><input type="text" name="rank_add"></td>
</tr>
</tbody>
</table>
<div style="margin: 20px 0px 0px 0px">
<button type="submit" name="add_features" class="btn btn-danger text-justify" style="margin: 10px 0px 10px 0px; width:auto; text-align:center !important;">Add Projects</button>
</div>
</div>
</form>
I expect it to change all values at one time
There are couple of principal issues with your code:
<input type="text" name="id_edit" value="1" disabled>
(as per your original code)
In these inputs
1) disabled elements do not get submitted by the browser. You should change them to readonly (as you've already done in your edit)
2) name="id_edit" is repeated multiple times, but you can't have multiple items with the same name - the server will only see one of them. To make it submit an array of values under the same name, change it to name="id_edit[]". You need to do this for all your repeatable fields.
The last issue is a typo in your server code:
WHERE id='" . $_POST['id'][$i] . "'";
needs to be
WHERE id='" . $_POST['id_edit'][$i] . "'";
because you aren't submitting any field called id in your form.
Lastly I'm going to issue a broader warning, for an issue you need to rectify now you've got the form working on a basic level: Your code is vulnerable to SQL Injection attacks. You should use parameterised queries and prepared statements to help prevent attackers from compromising your database by using malicious input values. http://bobby-tables.com gives an explanation of the risks, as well as some examples of how to write your queries safely using PHP / mysqli. Never insert unsanitised data directly into your SQL. The way your code is written now, someone could easily steal, incorrectly change, or even delete your data.
you got that warning message because you pass a single value data inside form you need to modified attribute name in each tag input like this
<tr>
<td><input type="text" name="id_edit[]" value="1" readonly></td>
<td><input type="text" name="name_edit[]" value="Videos"></td>
<td><input type="text" name="link_edit[]" value="https://www.youtube.com/channel/UCXqIrdlG-gsfSGuFUt0v-1g"></td>
<td><input type="text" name="rank_edit[]" value="AAA"></td>
</tr>
<tr>
<td><input type="text" name="id_edit[]" value="2" readonly></td>
<td><input type="text" name="name_edit[]" value="Basic Message Encrypter And Decrypter"></td>
<td><input type="text" name="link_edit[]" value="project?page=en-decrypter"></td>
<td><input type="text" name="rank_edit[]" value="B"></td>
</tr>
as above you send an array to php that you can use count for every data has sent from the form and do your stuff on php as well.
but I tell you that way is not the best way.. maybe you can use
data[]['id_edit']
in name attribute as well so you just need to
count($_POST['data'])
without worrying about imbalance count for every data

two submit buttons for the same form

Lets says that I have form and in the end I have 2 buttons
1 - send test form
2 - send live form
both send the same form. How can I send parameter to the server so I will know if its test or live?
Thanks
<form method='post' action='index.php?page=mailing&campID=<?PHP echo $_GET['campID'] ?>&act=<?PHP echo $actType ?>' id="Form" >
<table class="sort">
<tr>
<td>email address</td>
<td><input type="text" name="emailTest" value="<?PHP echo $user_details['email'] ?>" /></td>
</tr>
<tr>
<td></td>
<td>send test</td>
<td>send live</td>
</tr>
</table>
</form>
Just check in php if 'submit' field is 'test' or 'live'.
<form method='post' action='' id="Form" >
<table class="sort">
<tr>
<td>email address</td>
<td><input type="text" name="emailTest" value="" /></td>
</tr>
<tr>
<td></td>
<td><button type="submit" name="submit" value="test">Test</button></td>
<td><button type="submit" name="submit" value="live">Live</button></td>
</tr>
</table>
</form>
You'll either need to use JS, or one submit button, and a radio button. The latter is a better option, as you can't accidentally submit something to the wrong queue. Also, you should really be using a submit button, not an anchor.
The reason I would prefer the radio buttons is because once you click submit, you're past the point of no return. The radio button allows you to to click the wrong one, and then change it.
input[type="submit"] {
background: none;
border: 0;
color: blue;
text-decoration: underline;
cursor: pointer;
}
<form method='post' action='' id="Form">
<table class="sort">
<tr>
<td>email address</td>
<td>
<input type="text" name="emailTest" value="" />
</td>
</tr>
<tr>
<td>
<input type="radio" name="testLive" value="test" id="test" />
<label for="test">Submit as Test</label>
</td>
<td>
<input type="radio" name="testLive" value="live" id="live" />
<label for="live">Submit as Live</label>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit" />
</td>
</tr>
</table>
</form>

clone column1 to column 2 using button

I need another name2 column where in it has a button(copy all) once you click it a dialog box will appear "Are you sure you want to copy |Yes/No". it'll clone the data from name to name2.
<table class="downpanel">
<thead>
<tr>
<th></th>
<th>Name</th>
<th></th>
<th>Name2</th>
<th></th>
<th colspan="">Count</th>
<th></th>
<th>Unit</th>
<th></th>
<th>Category</th>
<th></th>
<th>Data1</th>
<th></th>
<th>Data2</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="button" class="copyall">
</td>
<td>
<input type="text" size="25" name="name">
</td>
<td>
<input type="text" size="25" name="name2">
</td>
<td>
<input type="button" class="copy">
</td>
<td>
<input type="text" size="3" name="from">
</td>
<td>
<input type="text" size="3" name="to">
</td>
<td>
<input type="button" class="copy">
</td>
<td>
<select name="unit" style="width: 75px;">
<option value="blah">blah</option>
</select>
<br />
</td>
<td>
<input type="button" class="copy">
</td>
<td>
<select name="category" style="width: 275px;">
<option value="blah">blah</option>
</select>
<br />
</td>
<td>
<input type="button" class="copy">
</td>
<td>
<input type="text" size="10" id="datepicker" name="data1">
</td>
<td>
<input type="button" class="copy">
</td>
<td>
<input type="text" size="7" name="data2">
</td>
<td>
<input type="button" class="copy">
</td>
</tr>
</tbody>
</table>
http://jsfiddle.net/jakecigar/hq2GG/2/
check this http://jsfiddle.net/hq2GG/4/
var r = confirm("Are you sure you want to copy?");
in both copy and copy all
http://jsfiddle.net/hq2GG/6/
You are facing a mode mash up issue. You initiated your data using HTML and now you want to manipulate it via Javascript. The more complex your issues get, the more you will end up in coding chaos. This is NOT about mixing different coding practices but to break the conceptual rework by designing one part in HTML and the other in Javascript. So as javascript seems to be mandatory here first design everything as if your whole application would only consist of Javascript. Define a mode:
DOM-Node-Manipulation (would be state of the art and also easier for manipulation) OR
html compile style (using innerHTML)
Once you got a mode make your matrix a matrix. If you need to copy cells, respectively rows and columns, you need to be able to address those. In the DOM Model you could maintain a two-dimensional wrapper array, in the html compile style you would obviously fittle with IDs, such as cell1_3, which helps you to make use of the getElementById. At the point in time you are able to address elements, it is just a matter of formulating loops, to get things copied, moved or deleted in bulk mode.
Once you got all of the conceptional prework you can decide to prefill your html page with html (and not javascript) - however this must follow the rules you set yourself for the scripting mode.

Passing selected value to PHP form using <a> links instead of select/option

I'm creating a dropdown menu where each selection will be wrapped in links. The value selected needs to be passed to a php contact form but I'm not sure how to do this.
Normally, I'd just use the select-option method but I want to style the menu completely from scratch so is it possible to get the same functionality from just a regular html dropdown menu?
The desired values in question are in the 'Who are you?' section...
<div id="contact_info">
<form name="htmlform" method="post" action="./contact.php">
<table>
<caption>CONTACT</caption>
<tr>
<td valign="top">
<label for="your_name">Name:</label>
</td>
<td valign="top">
<input type="text" name="your_name" maxlength="50" size="23">
</td>
</tr>
<tr>
<td valign="top">
<label for="email">Email:</label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="50" size="23">
</td>
</tr>
<tr>
<td valign="top">
<label>Who are you?:</label>
</td>
<td valign="top">
<a>
<p style="background:#ffffff;">I am...</p>
</a>
<a href="">
<p>an artist.</p>
</a>
<a href="">
<p>an educator.</p>
</a>
<a href="">
<p>a student.</p>
</a>
<a href="">
<p>a curator.</p>
</a>
<a href="">
<p>other...</p>
</a>
</td>
</tr>
</table>
<table>
<tr>
<td style="width:120px !important;"></td>
<td valign="top">
<label for="comments">Message:</label>
</td>
</tr>
<tr>
<td style="width:120px !important;">
<input type="submit" value="Submit">
</td>
<td valign="top">
<textarea name="comments" maxlength="1000" cols="44" rows="19"></textarea>
</td>
</tr>
</table>
</form>
</div>
You will need javascript to do this. As you have stated that your are not that fluent in javascript, I will use jQuery here to illustrate what you need to do in an easy way. Note that it can be done in plain javascript or using another library as well.
First you need to add a hidden field that will contain the value you want to submit with the rest of the form and then you need to use javascript to get the value of the selected option / click and put it in the hidden field:
I will also add an ID to the table cell to make selecting the right element and catching the events easier:
html:
<td valign="top" id="I_am_a">
<input type="hidden" name="I_am_a">
<a><p style="background:#ffffff;">I am...</p></a>
<p>an artist.</p>
<p>an educator.</p>
<p>a student.</p>
<p>a curator.</p>
<p>other...</p>
</td>
javascript (assuming jQuery is included):
$("#I_am_a a").on('click', function(e) {
e.preventDefault(); // prevent the default click action
$("#I_am_a input")
.val( $(this).find("p").text() ); // assign the text contents of the
// paragraph tag in the clicked a
// to the input element
});
Something like this should do it.

Categories