mysql insert from generated names - php

I have a form:
<form>
<input type="text" name="aaa" id="aaa">
<input type="submit">
</form>
I have also jQuery script and can append this input.
Example:
Generating name.
<form>
<input type="text" name="aaa" >
<input type="text" name="aaa2" >
<input type="text" name="aaa3" >
<input type="submit">
</form>
Problems is MySQL insert:
How to insert from generated names?
I know $_POST['aaa'], but I don't know from generated names.

If you're generating dynamically text boxes from jQuery and you want to push them to mySQL, instead of creating them with names like 'aaa', 'aaa2' or 'aaa3' you can use square brackets like this:
<form method="POST">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<button type="submit">Send</button>
</form>
That will allow you to use "field_name" as an array of strings.
<?php
$field_name = $_REQUEST['field_name'];
foreach($field_name as $value) {
$query = sprintf("INSERT INTO table SET field_name = '%s'", $value);
/* Execute Insert Query */
}

OK. if i have form like this:
<form method="POST">
<input type="text" name="aaa">
<input type="text" name="bbb">
<input type="text" name="ccc">
and i apend 3 input's
<input type="text" name="aaa">
<input type="text" name="bbb">
<input type="text" name="ccc">
<button type="submit">Send</button>
</form>
in mysql i have table:
id
aaa
bbb
ccc
this is one insert

Related

How i can send few values from inputs with auto increment?

I have block:
<div>
<input type="text" name="account[surname][]">
<input type="text" name="account[address][]">
<input type="text" name="account[phone][]">
...
<input type="number" name="account[number][][1]">
<input type="number" name="account[number][][2]">
<input type="number" name="account[number][][3]">
</div>
After submit i get:
{"id":{"13":""},"surname":{"13":"Mike"},"number":{"13":{"1":"623"},"14":{"2":"432"},"15":{"3":"542"}}}
But i need:
{"id":{"13":""},"surname":{"13":"Mike"},"number":{"13":{"1":"623","2":"432","3":"542"}}}
Id - Auto Increment.
Every "account[number][]" create new array. How can I put this data into an one array?
This form is added dynamically (clone).
For static form work this code, but i need dinamic:
<form>
<input type="text" name="account[surname][10]">
<input type="text" name="account[address][10]">
<input type="text" name="account[phone][10]">
...
<input type="number" name="account[number][10][1]">
<input type="number" name="account[number][10][2]">
<input type="number" name="account[number][10][3]">
<button type="submit">Send</button>
</form>

Get results of multdimensional array from form post

So I have a form which is build up like:
<input name="website['menu']['id']" type="number" value="1">
<input type="text" value="#000000" name="website['color']['primary']['hex']">
Now i want to read out those values in php after they are submit.
I tried the following code, but when I var_dump it it gives me null.
if (isset($_POST['website'])) {
$result = $_POST['website'];
var_dump($result['menu']['id']);exit;
}
Remove the quote from field name
<input name="website[menu][id]" type="number" value="1">
<input type="text" value="#000000" name="website[color][primary][hex]">
And access the variable in php like:
echo $_POST['website']['menu']['id'];
Try this,
<?php
print_r($_POST['website']);
print_r($_POST['website']['menu']['id']);
?>
<form action="" method="POST"/>
<input name="website[menu][id]" type="number" value="1">
<input type="text" value="#000000" name="website['color']['primary']['hex']">
<input type="submit" value="submit"/>
</form>

Form action html to php

i have
<form action = "https://creator.zoho.com/api/xml/fileupload/scope=creatorapi&authtoken=**************" method="post" enctype="multipart/form-data">
<input type="text" name="applinkname" value="sample">
<input type="text" name="formname" value="Employee">
<input type="text" name="fieldname" value="File_upload">
<input type="text" name="recordId" value="1235667754455">
<input type="text" name="filename" value="profile.jpg" >
<input type="file" name="file">
<input type="submit" value="Update">
</form>
But i must send 2500 files, how can i transform this html to php, my problem is <input type="file" name="file">, how convert it to give a value in php ?
thanks you
You can use $_REQUEST['FILE']=$SOME VARIABLE;
And name is same what you use in your database
then apply enctype="multipart/form-data".

Values sent to PHP via POST are all empty strings

I have the following form:
<FORM action="http://url.to.mysite.com/doc.php" method="post">
<INPUT type="text" id="name">
<INPUT type="text" id="id">
<INPUT type="submit" value="Send">
</P>
</FORM>
Unfortunately, whatever is typed into the text fields, the PHP script receives only empty strings in every $_POST variable. All of the variables are set, just empty. What can be the cause of that?
You are missing the name properties in your html:
<FORM action="http://url.to.mysite.com/doc.php" method="post">
<INPUT type="text" name="name" id="name">
<INPUT type="text" name="id" id="id">
<INPUT type="submit" value="Send">
</FORM>
Only form elements with a name attribute will be sent to the PHP script (same with POST)
Try this:
<form action="http://url.to.mysite.com/doc.php" method="post">
<input type="text" id="name" name="name">
<input type="text" id="id" name="id">
<input type="submit" value="Send">
</form>
You need to add the name attribute to your input tags. Example: <input type="text" name="name" id="name" />

How to process multiple forms using one php script

I have multiple forms and I have one php script that I want to use to process these forms but when I click on submit for any of the forms...the script is processed by the number of forms with the submit button named 'submitForm' in this case, the alert will show 3 times instead of once! What am I not doing right?
NB. I hope this makes much sense?
html code
<form action="" name="form1" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
php script
<?php
if (isset($_POST['submitForm'])) {
echo('<script>alert("Form Submitted")</script>');
}
?>
when I click on submit for any particular form, it submits all the forms.
this is not true.
Once your forms have proper formatting, your browser will submit only current one.
(and PHP has nothing to do here)
however, whole page will be reloaded, if you mean that. That is okay - when you submit a form, a page is intended to reload. If you need another behavior, you have to explain your wishes.
Also note that none of your text fields being sent to the server as they have no names.
I guess the question I should be asking is, how do I pass a particular form to php instead of writing multiple php scripts to handle each form!!!
well, it seems you want to ask how to distinguish these forms.
add a hidden field into each
<input type="hidden" name="step" value="1" />
and then in PHP
if ($_POST['step'] == 1) {
//first form
}
if ($_POST['step'] == 2) {
//second
}
This submits one form of many to php. Copy, paste, test, and study.
<?php
if (isset($_POST['submitForm'])) {
print_r($_POST);
}
?>
<form action="" name="form1" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
Using a,b,c,d for the first form, e,f,g,h for the second form and i,j,k,l for the third form and submitting the second form yields the following output:
Array
(
[A] => e
[B] => f
[C] => g
[D] => h
[submitForm] => Submit Form
)
#Jay
Actually its not hard.
Once you supply form names, your work is done. the DOM does the rest.
write one php block to do your functions (create/update/retrieve/delete)
Whichever button is clicked, by default it submits only the elements enclosed together with it.
if(!empty($_POST)){
if(isset($_POST['submit'])){
print "<pre>";
var_dump($_POST); // write your code here as you would
print "<pre>";
}
}
try this with your form above.
I know this is an old post but here's how I solve this very problem.
All you need to do is make sure the submit buttons in each form have different names. Eg:
<form action="" name="form1" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm1" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm2" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm3" />
</form>
Then, you simply check which form's submit button was pressed.
<?php
if (isset($_POST['submitForm1'])) {
echo('<script>alert("Form 1 Submitted")</script>');
} elseif (isset($_POST['submitForm2'])) {
echo('<script>alert("Form 2 Submitted")</script>');
} elseif (isset($_POST['submitForm3'])) {
echo('<script>alert("Form 3 Submitted")</script>');
}
?>
If you need dynamic forms, you may try below code. While statement can be changed to fetch data from DB and use foreach instead. Hope you know this.
Here, I used while($n<10) for 10 dynamic forms.
You can also use tag as below if you need separate form names.
<form action="" name="form<?=$n?>" method="post">
This will create separate form names such as form1, form2, etc but not necessary here.
<?php
if (isset($_POST['submitForm'])) {
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
$n=0;
while($n<10) {
$n++;
?>
<form action="" name="form1" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<?php
}
?>
Sample page with output when I click row 5..

Categories