I've a form which can be saved to database multiple times depending upon number of entries in to it. To be precise, I have 4 input fields and after filling all the fields I click on Add button. Upon clicking on Add button, I'm creating an entry in a table row inside <form> tag like,
<form>
<table>
<tr>
<td> <input type="hidden" name="a[]" value="xyz" /> xyz</td>
<td> <input type="hidden" name="b[]" value="123" /> 123</td>
<td> <input type="hidden" name="c[]" value="456" /> 456</td>
<td> <input type="hidden" name="d[]" value="abc" /> abc</td>
</tr>
<tr>
<td> <input type="hidden" name="a[]" value="xyz" /> xyz</td>
<td> <input type="hidden" name="b[]" value="123" /> 123</td>
<td> <input type="hidden" name="c[]" value="456" /> 456</td>
<td> <input type="hidden" name="d[]" value="abc" /> abc</td>
</tr>
</table>
</form>
Now I want to save each row of the table into database as a row. I'm unable to understand how to do that. I'm getting the data in controller using dd($a = $request->input('a')); for a single field as,
array:2 [▼
0 => "xyz"
1 => "xyz"
]
I'm using this to save it into DB using Laravel,
$save=Model::Create(array(
'a'=>$request->input('a'),
'b'=>$request->input('b'),
'c'=>$request->input('c'),
'd'=>$request->input('d')
));
I'm getting error,
Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in /var/www/html/quoting/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php on line 681 and defined
How can I save it into the database using the Laravel command.?
use this
$input = $request->all();
foreach($request->a as $key => $value) {
Model::Create(array(
'a' => $value,
'b' => $input['b'][$key],
'c' => $input['c'][$key],
'd' => $input['d'][$key],
));
}
you need to change name for all inputs
<form>
<table>
<tr>
<td> <input type="hidden" name="myform[a][0]" value="xyz" /> xyz</td>
<td> <input type="hidden" name="myform[b][0]" value="123" /> 123</td>
<td> <input type="hidden" name="myform[c][0]" value="456" /> 456</td>
<td> <input type="hidden" name="myform[d][0]" value="abc" /> abc</td>
</tr>
<tr>
<td> <input type="hidden" name="myform[a][1]" value="xyz" /> xyz</td>
<td> <input type="hidden" name="myform[b][1]" value="123" /> 123</td>
<td> <input type="hidden" name="myform[c][1]" value="456" /> 456</td>
<td> <input type="hidden" name="myform[d][1]" value="abc" /> abc</td>
</tr>
</table>
</form>
Then in your controller first dd($request->myform) and check you are getting values properly
if you want to store multidimensional array then you have ot use insert not create
$save=Model::insert(multdimensionl array here);
Related
lets say we have this:
echo '<form method="post">
<div class="form-group">
<table class="table table-bordered table-hover table-striped" style="width:auto">
<tr>
<td><label for="array">ARRAY_NAME</label></td>
<td>
<input type="checkbox" name="array[]" value="1" /> option1<br />
<input type="checkbox" name="array[]" value="2" /> option2
</td>
</tr>
<tr>
<td><label for="array2">ARRAY_NAME2</label></td>
<td>
<input type="checkbox" name="array2[]" value="1" /> option1<br />
<input type="checkbox" name="array2[]" value="2" /> option2
</td>
</tr>
<tr>
<td><label for="array3">ARRAY_NAME3</label></td>
<td>
<input type="checkbox" name="array3[]" value="1" /> option1<br />
<input type="checkbox" name="array3[]" value="2" /> option2
</td>
</tr>
</table>
</div>
<button type="submit" name="submit" class="btn btn-success">Submit</button>
</form>';
I tried to implement this code: <?php echo (isset($_POST['array1[0]']) && $_POST['array1[0]'] == 1) ? "checked='checked'" : "" ?>
but it didn't work! It only works if you have name="array1" and name="array2". this way I'm thinking I can save multiple data in a parent array saved! Like this form[array1[],array2[],array3[]].
Can someone give me a solution because I'm stuck! Thanks in advance guys!!
You are trying to access the values incorrectly.
You can access a array posted like this:
echo $_POST['array1'][0];
php.net/$_POST See the first comment.
When you put square brackets in the name of form control, PHP will inflate that set of elements with similar names into an array.
You need to access:
$_POST['array1'][0]
not
$_POST['array1[0]'] // This is incorrect
(You also need to match the actual name of your control: Your HTML has array, array2 and array3 but not array1)
My studybook gives me an assignment which requires me to have 2 forms on one page and output them both to the same page. Is this even possible? Both forms work fine independently. Both have the action:
<?php echo $_SERVER['PHP_SELF']; ?>".
Both have a submit button, but the button only submits the form it's part of. This probably makes sence though.
Thing is i need the page to either post both form outputs when clicking one of the 2 submit buttons or press them subsequently but the information from the first form needs to stay on the page.
Is this possible or am i trying do to the impossible?
the forms are as follows;
form 1:
<form name="orderform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Korting:
<tr>
<td>
<input type="checkbox" name="korting[]" value=15 /> Student 15% <br>
<input type="checkbox" name="korting[]" value=10 /> Senior 10% <br>
<input type="checkbox" name="korting[]" value=5 /> Klant 5% <br>
<hr />
</td>
</tr>
<tr>
<td>
betalingswijze
<input type="radio" name="betalingswijze" value="paypal"> Paypal
<input type="radio" name="betalingswijze" value="mastercard"> Mastercard
<input type="radio" name="betalingswijze" value="visa"> Visa
<hr />
</td>
<tr>
<td>
<img src="toshiba.jpg" alt=" " />
</td>
</tr>
<tr>
<td>
Toshiba Sattelite A100-510 Basisprijs 999.99
</td>
</tr>
<tr>
<td><!--Shopping Cart Begin-->
<input type="hidden" name="toshibaproduct" value="001" />
<input type="hidden" name="toshibamerk" value="toshiba" />
<input type="hidden" name="toshibamodel" value="Sattelite A100-510" />
Operating system <select name="toshibaos" value="Toshiba">
<option value="xp">Windows XP</option>
<option value="vista">Windows Vista</option>
<option value="linux">Linux</option>
</select>
Aantal: <input type="text" size=2 maxlength=3 name="toshibaaantal" value="0" />
<input type="hidden" name="toshibaprijs" value="999.99" />
<input type="image" src="bestel.jpg" border=0 value="bestellen" />
<hr />
<tr>
<td>
<img src="acer.jpg" alt=" " />
</td>
</tr>
<tr>
<td>
Acer Aspire 5735Z Basisprijs 529.99
</td>
</tr>
<tr>
<td>
<input type="hidden" name="acerproduct" value="002" />
<input type="hidden" name="acermerk" value="acer" />
<input type="hidden" name="acermodel" value="Aspire 5735Z" />
Operating system <select name="aceros" value="Acer">
<option value="xp">Windows XP</option>
<option value="vista">Windows Vista</option>
<option value="linux">Linux</option>
</select>
Aantal: <input type="text" size=2 maxlength=3 name="aceraantal" value="0" />
<input type="hidden" name="acerprijs" value="529.99" />
<input type="image" src="bestel.jpg" border=0 value="bestellen" />
<hr />
</td><!--Shopping Cart End-->
</tr>
</form>
Form 2
<form name="klant gegevens" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border=1 >
<tr>
<td colspan="2">
<b>Factuur klantgegevens</b>
</td>
</tr>
<tr>
<td width="100">Naam: </td>
<td>
<input type="text" sie="55" name="naam" />
</td>
</tr>
<tr>
<tr>
<td>Adres: </td>
<td>
<input type="text" sie="55" name="adres" />
</td>
</tr>
<tr>
<td>Woonplaats:</td>
<td>
<input type="text size="34" name="woonplaats">
Postcode:<input type="text" size="6" name="postcode">
</td>
</tr>
<tr>
<td>e-mail:</td>
<td>
<input type="text" size="55" name="email">
</td>
</tr>
<tr>
<td>Feedback:</td>
<td>
<textarea cols="40" rows="3" name="commentaar">
</textarea>
</td>
</tr>
</table>
<input type="image" src="checkout.png" value="send"/>
</form>
Both have functions which kick in on submit. Sorry for the spacings. I have them better in my own files but i just don't know how to get them right on this site.
Greetings,
Lennart
The action represent the page that will receive the posted data. You may use differents actions or the same action with different parameters.
If you use the same action, you had to insert a parameter that permit to manage different cases. You may insert an hidden field to do this.
Consider these simple forms:
<form name="form_a" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="form" value="A">
<button type="submit">Form A</button>
</form>
<form name="form_b" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="form" value="B">
<button type="submit">Form B</button>
</form>
To manage the different submission, you had to check the value of the hidden field:
if(isset($_POST['form'])){
switch ($_POST['form']) {
case "A":
echo "submitted A";
break;
case "B":
echo "submitted B";
break;
default:
echo "What are you doing?";
}
}
You can't submit two separate forms at the same time, because each submission represent a different request to the server.
You may merge manually the fields of the two forms, or use Javascript to do this for you.
Keep in mind that if you do this via Javascript, the field of the forms had to have differents names.
As you caan see here you can do simply via jQuery:
var str1 = $("form_a").serialize();
var str2 = $("form_b").serialize();
$.post(url, str1 + "&" + str2);
Where url is the action params of the form
Your form should be like this.
First form
<form method="post" >
...
<input type="hidden" name="form1submission" value="yes" >
<input type="submit" name="submit" value="Submit" >
</form>
Second form
<form method="post" >
...
<input type="hidden" name="form2submission" value="yes" >
<input type="submit" name="submit" value="Submit" >
</form>
And your php for first form.
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form1submission'])) {
// first form code.
}
And second form php code.
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form2submission'])) {
// second form code.
}
That's it.
DUPLICATE POST
Yes you can!
First, add the proper action to all forms, secondly, give them an unique name
HTML Side
Don't forget to add the http method what you want (GET or POST)
<form method="post">
<input type="hidden" name="orderform" />
<!-- rest of form goes here -->
</form>
<form method="post">
<input type="hidden" name="klant_gegevens" />
<!-- rest of form goes here -->
</form>
Leaving the action-attribute empty or removing it entirely will make the form submit to the current page (see this post), and usage of $_SERVER["PHP_SELF"] can lead to XSS-injection read under "Big Note on PHP Form Security"
Note:
Avoid using space for field names, it can make some problem to match them...
PHP Side
getting input values, by filtering on received form name
if (isset($_POST["orderform"])) {
// The first form was submitted
}
if (isset($_POST["klant_gegevens"])) {
// The second form was submitted
}
Note:
Use print_r() or var_dump(), to debug the content of exchanged values
what datatypes do i use for checkboxes and radio buttons and for input type=date in the pmySQL??
<pre>
<tr>
<td class="right_align">*Occupation : </td>
<td>
<input type="radio" name="occupation" value="student" />Student<br />
<input type="radio" name="occupation" value="business" />Business<br />
<input type="radio" name="occupation" value="pvtsect" />Priavte Sector<br />
<input type="radio" name="occupation" value="govtsect" />Govt. Sector<br />
<input type="radio" name="occupation" value="others" />Others<br />
</td>
</tr>
<tr>
<td class="right_align">*On which project you want to work :</td>
<td>
<input type="checkbox" name="" ="projectname" value="shine"/>Project Shine<br />
<input type="checkbox" name="" ="projectname" value="sahyog" />Project Sahyog<br />
<input type="checkbox" name="" ="projectname" value="smile" />Spreading Smiles
</td>
</tr>
</pre>
1) you will get string as the value of radio so it will be varchar type
2) for check boxes any one can have multiple values so you need to create a separate table(working_project) for this values( data type will be same as radio) and another table for mapping(user_working_project)
user_working_project table will containing user id and working project id
e.g one user has id=1, and working_project=1,2
then in your user_working_project table entries will be like this:
user_working_project_id working_project_id user_id
1 1 1
2 2 1
Ok here is problem, I am sending form data via radio buttons to a PayPal checkout but it works when you first upload the php file and takes the correct values, but if you return and select say the next radio button and submit it then changes the submit info to the value you sent before almost overiding it?
<table class="procut_item" border="0" cellpadding="4">
<tr>
<td width="auto"><h4>Product test</h4></td>
<td width="auto"><form method="post" action="process.php">
<input type="hidden" name="itemprice" value="10" />
<input type="hidden" name="itemQty" value="1" />
Size:<input type="radio" name="itemname" value="1">10 x 10</select>
<input class="dw_button" type="submit" name="submitbutt" value="Buy" />
</td>
<tr>
<tr>
<td width="auto"><h4>Product test</h4></td>
<td width="auto"><form method="post" action="process.php">
<input type="hidden" name="itemprice" value="30" />
<input type="hidden" name="itemQty" value="1" />
Size:<input type="radio" name="itemname" value="1">10 x 10</select>
<input class="dw_button" type="submit" name="submitbutt" value="Buy" />
</td>
<tr>
</table>
With seeing all of the code and how it is being handled, my first guess would be that you are usnig the same variable name, and it is overriding the first. You could could create an array if not already using one, or increment the variable name by appending a number to it.
I have a while loop generated table that looks like the following:
<table>
<tr>
<th>Date</th>
<th>Pushups</th>
<th>Delete</th>
</tr>
<tr>
<td>11-01-2012</td>
<td>28</td>
<td>
<form action="../workout_tracker/delete_pushups.php" method="get" id="Delete1">
<input type="hidden" value="253" name="pushups_id" />
<input type="hidden" value="/workout_tracker/pushups-tracker/" name="current_page" />
<input type="submit" value="Delete" form="Delete1" />
</form>
</td>
</tr>
<tr>
<td>11-02-2012</td>
<td>33</td>
<td>
<form action="../workout_tracker/delete_pushups.php" method="get" id="Delete2">
<input type="hidden" value="246" name="pushups_id" />
<input type="hidden" value="/workout_tracker/pushups-tracker/" name="current_page" />
<input type="submit" value="Delete" form="Delete2" />
</form>
</td>
</tr>
<tr>
<td>11-03-2012</td>
<td>43</td>
<td>
<form action="../workout_tracker/delete_pushups.php" method="get" id="Delete3">
<input type="hidden" value="39" name="pushups_id" />
<input type="hidden" value="/workout_tracker/pushups-tracker/" name="current_page" />
<input type="submit" value="Delete" form="Delete3" />
</form>
</td>
</tr>
</table>
The problem is that the first "Delete" form in the first row of the table isn't submitting. When I click, it doesn't do anything.
There is a form higher up on the page, but it closes and has validated. I get the same response in Chrome, Firefox, and IE.
UPDATE:
In a different form higher in the markup (not shown), the form tag had not been closed. I had </for>...
Do any of the other delete forms work or is it just the first one?
Open up your browser console/debugger and look for any errors on page load or when you click the button.
The only thing that looks as a possible problem is the form attribute of the submit input element. The link below shows that it's new in HTML5 and the only browser that doesn't support it yet is IE. It could be that you need to update to the latest version of your browser.
http://www.w3schools.com/tags/att_input_form.asp