i have issue in laravel 5.1 with insert a multiple inputs intro DB.
i build a query code for multiple inputs and after that i send them to controller but i have there a issues to insert it to DB
how i can insert the inputs to db if the results its like this:
{"_token":"C6m83bcZKaQsOtRiYEKxJAzzZjvdLerl9QpsvSSs","client_id":["aJQsijwqFVG9r0","aJQsijwqFVG9r0"],"short":["4","11"],"url":["567567567567","3453434534"]}
the code:
HTML:
<form class="js-validation-material form-horizontal push-10-t" action="{!! url() !!}/addreg" method="post">
{!! csrf_field() !!}
<div class="form-group">
<div id="buildyourform"></div>
</div>
<div class="form-group">
<div class="col-xs-12">
<button class="btn btn-sm btn-primary" type="submit">Submit</button>
</div>
</div>
</form>
<script>
$(document).ready(function() {
$("#add").click(function() {
var intId = $("#buildyourform div.form").length + 1;
var fieldWrapper = $("<div class=\"form col-sm-6 col-lg-6\" id=\"field" + intId + "\"><div class=\"form-material\">");
var client_id = $("<input type=\"hidden\" name=\"client_id[]\" value=\"{!! $task->client_id !!}\" class=\"form-control\" />");
var langname = $("<select type=\"text\" name=\"short[]\" class=\"form-control\">{!! $data['langs'] !!}</select>");
var url = $("<input type=\"text\" name=\"url[]\" class=\"form-control\" placeholder=\"Insert a url..\" />");
var label = ("<label for=\"date\"><h3 class=\"block-title\">Lang" + intId + "</h3></label>");
var removeButton = $("<button class=\"btn btn-danger btn-xs push-5-r push-10\" type=\"button\"><i class=\"fa fa-times\"></i></button>");
removeButton.click(function() {
$(this).parent().remove();
});
$("#buildyourform").append(fieldWrapper);
$("#field" + intId + " .form-material").append(client_id,langname,url,removeButton,label);
});
});
</script>
the results need to be in the DB like:
DB::table('table')->insert([
['client_id' => $request->client_id],
['short' => $request->short],
['url' => $request->url],
]);
for each input.
The problem is you are sending multiples inputs with the same name, so it only get the last one.
Everytime you do and "add" try to wrap them in a div with a class (example: "foo"). So you will get some "foo" divs.
Create a hidden input called for example "items"
{{ Form::hidden('items') }}
Next you have to overwrite the click function of the submit button:
$('#submit-form').click(function(e){
a = {};
$.each($('.foo'), function(k,v){
a[k] = $(v).find(':input').serializeArray()
});
$('input[name="items"]').val(JSON.stringify(a));
$('#form').submit()
}
And in the controller:
$items = json_decode(Input::get('items'), true);
Then you have an array with all of your inputs.
Related
I've seen the other code for looping textbox, but it goesn't seem to apply in my form wizard form script.
What i need is a specific text box that loops when the user inputs a certain number of textboxes that he want to put.
Example:
there's a text box in which the user will enter the number of textboxes that he wanted to display, say 3. So after I click the submit button, there should be 3 textboxes as the output.
the following code when I put in form wizard not work properly
<fieldset>
<div class="form-card">
<div class="row">
<div class="col-md-7">
<h2 class="fs-title">Item Detail:</h2>
</div>
<div class="col-md-5">
<h2 class="steps">Step 3 - 4</h2>
</div>
</div>
//CODE here
</div> <input type="button" id="next4" name="next" class="next action-button" value="Submit" onclick="validate3(0)"/>
<input type="button" name="previous" class="previous action-button-previous" value="Previous" />
</fieldset>
You can use the following code sniffed to generate text boxes dynamically (using jquery).
<script type="text/javascript">
$(document).ready(function(){
var counter = 2;
$("#addButton").click(function () {
if(counter>10){
alert("Only 10 textboxes allow");
return false;
}
var newTextBoxDiv = $(document.createElement('div')).attr("id", 'TextBoxDiv' + counter);
newTextBoxDiv.after().html('<label>Textbox #'+ counter + ' : </label>' +
'<input type="text" name="textbox' + counter +
'" id="textbox' + counter + '" value="" >');
newTextBoxDiv.appendTo("#TextBoxesGroup");
counter++;
});
$("#removeButton").click(function () {
if(counter==1){
alert("No more textbox to remove");
return false;
}
counter--;
$("#TextBoxDiv" + counter).remove();
});
$("#getButtonValue").click(function () {
var msg = '';
for(i=1; i<counter; i++){
msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
}
alert(msg);
});
});
</script>
</head><body>
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1">
<label>Textbox #1 : </label><input type='text' id='textbox1' >
</div>
</div>
<input type='button' value='Add Button' id='addButton'>
<input type='button' value='Remove Button' id='removeButton'>
<input type='button' value='Get TextBox Value' id='getButtonValue'>
For some reason a POST variable from my form is always empty. Here's my code...
I have a bootstrap select which returns information which I display upon selecting a new item from the dropdown (select).
Code:
$(function() {
$("#shiftsDD").change(function(){
var selectedItemId = $('#shiftsDD option:selected').val();
$.post('/step/services/shiftInfo.php', { shiftID : selectedItemId }, function(res){
$("#shiftCard").show();
var jsonRes = $.parseJSON(res);
$("#address").html(jsonRes[0]);
$("#duties").html(jsonRes[1]);
$("#employer").html(jsonRes[4]);
$("#payRate").html(jsonRes[5]);
$("#theShiftID").html(jsonRes[6]);
console.log(res);
});
});
});
All of those values from the jsonRes array are assigned in here:
<form role="form" name="cardForm" method="POST">
<div class="card" id="shiftCard">
Employer:
<h3 id="employer">Test Employer</h3><br>
Address:
<h3 id="address">Test Address</h3><br>
Duties include:
<h3 id="duties">Test duties</h3><br>
Pay rate:
<h3 id="payRate">Test PayRate</h3><br>
<h5 id="theShiftID" name="myShiftID"></h5>
<br>
<button type="submit" class="btn btn-lg btn-green-solid" name="shiftsBtn">Accept work</button>
</div>
</form>
I've tried lots of things but I am new to ajax and not a pro in PHP so I don't know why:
if (isset($_POST['shiftsBtn'])) {
$theId = $_POST['myShiftID'];
...
myShiftID is always empty
ShiftsDD select code:
<select name="shiftsDD" id="shiftsDD" class="form-control form-control-45">
<option value="0">Select a shift</option>
<?php
$theQuery = "
SELECT DATE_FORMAT(StartTime, '%d/%b/%Y' ) AS ShiftDate, ShiftID,ShiftTitle,TIMESTAMPDIFF(MINUTE, StartTime, EndTime) AS ShiftDuration, ROUND(PayRate,2) as PayRate FROM Shifts;
";
$result = $DBH->query($theQuery);
$result->execute();
while($r = $result->fetch()) {
$theDuration = $r['ShiftDuration'] / 60;
echo "<option value='" . $r['ShiftID'] ."'>[". $r['ShiftDate'] . "] " . $r['ShiftTitle'] . " (Hours: " . $theDuration . ")</option>";
}
?>
</select>
Ok so you need to change the form to match below. Have removed the elements you don't need to change. Note changing the h5 to an input.
<form role="form" name="cardForm" method="POST">
<div class="card" id="shiftCard">
<input id="theShiftID" name="myShiftID" value="" /><br />
<button type="submit" class="btn btn-lg btn-green-solid" name="shiftsBtn">Accept work</button>
</div>
</form>
Then once you've made the form changes, you need to change your js from .html() to .val()
$(function() {
$("#shiftsDD").on('change', function(){
var selectedItemId = $(this).val();
$.post('/step/services/shiftInfo.php', { shiftID : $(this).val() }, function(res){
$("#shiftCard").show();
var jsonRes = $.parseJSON(res);
$("#theShiftID").val(jsonRes[6]);
});
});
});
I have a form that can delete records from a MySql database using ajax and jQuery. I'm trying to get the jQuery to only select the relevant record passed to it and not just delete the top row of records which it does at the moment. I think I need to get
<div class="'.$id.'">
from my form and make it a data attribute that can be selected. Hope I'm making myself clear. Many thanks.
<script type="text/javascript">
//Delete Review
$(document).ready(function(){
//Need to get $id here.
$(".deleteReview").click(function (e) {
e.preventDefault();
var username = $("#username").val();
var film_id = $("#film_id").val();
var id = $("#id").val();
$.post('ajax_deleteReview.php', {
username: username,
film_id: film_id,
id: id
}, function(data) {
$("#message").html(data);
$("#message").hide();
$("#message").fadeIn(500);
$("#message").fadeOut(2500);
});
return false;
});
});
</script>
<form>
<div class="'.$id.'">
<input type="hidden" id="username" name="username" value="'. $username.'">
<input type="hidden" id="film_id" name="film_id" value="'.$film_id .'">
<input type="hidden" id="id" name="id" value="'.$id .'">
<button type="submit" id="" class="btn btn-danger btn-xs pull-right">delete</button>
</form>
</div>
I would recommend you to use data-* prefixed attributes to persists your data.
HTML
<div data-id="'.$id.'" data-username="'. $username.'" data-filmid="'.$film_id .'" class="deleteReview">
<button type="submit" id="" class="btn btn-danger btn-xs pull-right">delete</button>
<div >
Then you can fetch it using .data(key)
Return the value at the named data store for the first element in the jQuery collection, as set by data(name, value) or by an HTML5 data-* attribute
Script
//Delete Review
$(document).ready(function(){
//Need to get $id here.
$(".deleteReview").click(function (e) {
e.preventDefault();
var username = $(this).data("username");
var film_id = $(this).data('filmid');
var id = $(this).data('id');
......
return false;
});
});
First in your PHP do something like this, you add the data attributes to your delete button since you are not actually submitting a real form. I also moved the deleteReview class to the button.
print '<button type="submit" class="deleteReview" data-id="'.$id.'" data-film-id="'.$film_id.'" data-username="'.htmlentities($username).'" class="btn btn-danger btn-xs pull-right">delete</button>';
Then, in your jQuery, update click event callback like so:
$(".deleteReview").click(function (e) {
e.preventDefault();
var username=$(this).data('username');
var film_id=$(this).data("film-id");
var id=$(this).data('id');
/* the rest of your code */
The data attributes become accessible easily with jQuery using $(this).data('some-attribute-name')
I have Jquery code that can create multiple text boxes .Now, i want to submit my text boxes' values so that i could get them using PHP on next page.I want to submit text box in the same way as ( )
and on the second page,i could get values using POST method.How can i i post multiple text values in this case and how can i retrieve them on the next page using php?? Plz help as i am a new bee.
CODE:
<head>
<script type="text/javascript">
$(document).ready(function(){
var counter = 2;
$("#addButton").click(function () {
if(counter>10){
alert("Only 10 textboxes allow");
return false;
}
var newTextBoxDiv = $(document.createElement('div'))
.attr("id", 'TextBoxDiv' + counter);
newTextBoxDiv.after().html('<label>Textbox #'+ counter + ' : </label>' +
'<input type="text" name="textbox' + counter +
'" id="textbox' + counter + '" value="" >');
newTextBoxDiv.appendTo("#TextBoxesGroup");
counter++;
});
$("#removeButton").click(function () {
if(counter==1){
alert("No more textbox to remove");
return false;
}
counter--; $("#TextBoxDiv" + counter).remove(); });
$("#getButtonValue").click(function ()
{
var msg = '';
for(i=1; i<counter; i++){
msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
}
alert(msg);
});
});
</script>
</head><body>
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1">
<label>Textbox #1 : </label><input type='textbox' id='textbox1' >
</div>
</div>
<input type='button' value='Add Button' id='addButton'>
<input type='button' value='Remove Button' id='removeButton'>
<input type='button' value='Get TextBox Value' id='getButtonValue'>
</body>
Try to giving name for text boxes as array
e.g. input type='textbox' name='textbox[]' id='textbox1'
and access it on PHP page as array
$arrayRequest = $_REQUEST['textbox'];
I have a form with select fields dynamically generated and with the options taken from php (same for all the selects). But I need that every <select>...</select> must be shown in a different line. This can be done having every <select>...</select> inside a <div>...</div>.
This is the code that I have, it´s working but the select fields are not within a div:
<script type="text/javascript">
//<![CDATA[
$(function(){
var counter = 1;
$("#addButton").click(function () {
if(counter>10){
alert("Only 10 textboxes allow");
return false;
}
var select = $('<select>').attr({id:'select'+counter,name:'select'+counter});
$.ajax({
url: 'selects.php',
dataType: "html",
success: function(data) {
select.html(data);
}
});
select.appendTo("#TextBoxesGroup");
counter++;
});
$("#removeButton").click(function () {
if(counter==1){
alert("No more textbox to remove");
return false;
}
counter--;
$("#select" + counter).remove();
});
$("#getButtonValue").click(function () {
var msg = '';
for(i=1; i<counter; i++){
msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
}
alert(msg);
});
});
//]]>
</script>
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1">
<label>Textbox #1 : </label><input type='text' id='textbox1' >
</div>
<div id="TextBoxDiv2">
<label>Textbox #2 : </label><input type='text' id='textbox2' >
</div>
</div>
<input type='button' value='Add Button' id='addButton'>
<input type='button' value='Remove Button' id='removeButton'>
<input type='button' value='Get TextBox Value' id='getButtonValue'>
And this is the html code generated by select.php:
<option value="1">Uno</option>
<option value="2">Dos</option>
<option value="3">Tres</option>
<option value="4">Cuatro</option>
Use the wrap() function. :
Put this code just before the counter++;. :
$("#TextBoxesGroup > select").wrap ( function () {
return '<div id="wrap_' + this.id + '"></div>';
} );
See it in action at jsFiddle.