I am trying to insert the data of text area of tabs in MYSQL using PHP. I am able to insert the data of only predefined tab but I am not able to insert the data of newly created tabs when user clicks on ADD TAB button and create his own tab.
Any ideas how it can be done?
here is my HTML output: http://jsfiddle.net/HMv9S/1/
I have tried this MySQL code for inserting the data and successfully can insert the data of predefined tab:
<?php
if ($_POST['submit'])
{
$con=mysqli_connect("localhost","root","","my_db");
$texts=array($_POST['txt']);
foreach($texts as $value)
{
$allTextArea = implode(",", $value);
$text = mysql_real_escape_string($allTextArea);
$query= "INSERT INTO Sections(data) VALUES('$text')";
mysqli_query($con,$query);
}
}
?>
You should name the textarea as an array like txt[] instead of txt. Now on form submit you will get all the textarea values.
if ($_POST['submit']){
$allTextArea = $_POST['txt'];
print_r($allTextArea);
}
Input elements attribute name must be unique (semantically also id). Then iterate through possible keys received in $_POST.
In single form you are creating multiple input elements (in your case <textarea>) with same name attribute. in $_POST, values are sent in format 'key' => value with input element attribute name as key. All your textareas has 'txt' as name.
Related
What I am trying to do is I have a form which inserts data into a table. But now I want to add a preview button to the form to show a preview of the data to be inserted and uploader can check the formatting he has done before inserting data.
Now I want to know how to submit form to two different pages and use the form data.
I am confused as Using action attribute of form will make both buttons to POST to same page.
I have also tried code below, but it will also fail if the data is too much and as data3 is paragraph and can have long essay data
if( $_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['preview']) ){
$data1 = trim($_POST['data1']);
$data2 = trim($_POST['data2']);
$data3 = trim($_POST['data3']);
header("location: preview.php?preview=true&data1=$data1&data2=$data2&data3=$data3");
exit();
}
Use a session, and store the post data in an array, or output the variables in a hidden form on preview page.
Example 1:
session_start();
$_SESSION['form_data'] = [
'data1' => $_POST['data1']
...
];
Example 2:
echo '<input type="hidden" name="data1" value="', htmlspecialchars($_POST['data1']), '" />';
I have been doing a tutorial on posting to a database with Ajax and now I have it working with 1 form field, but I would like to learn how to get it to work with 2 form fields.
The form field I have working is named content_txt and it is inserted into add_delete_record(content). The second form is named balance_txt and would also be inserted in the same table as content_txt was.
This is the block of code that I'm trying to figure out how to update with the extra form field. I have added notes in it to explain what I am using each thing for and what I want to do:
if(isset($_POST["content_txt"]) && strlen($_POST["content_txt"])>0)
{ //checks $_POST["content_txt"] is not empty, but I am not sure how to add the other field into this
$contentToSave = filter_var($_POST["content_txt"],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
//using this to strip stuff from the post, but I am not sure how I should added the other form field to this, should I create another variable line or can I add it to this.
// Insert sanitize string in record and I think I have it correct, except if I need to add a variable following content
if(mysql_query("INSERT INTO add_delete_record(`content`,`balance`) VALUES('".$contentToSave."')"))
{
//This returns the results back to the page, do I need to have a duplicate section for the additional record or will this work like a loop.
$my_id = mysql_insert_id(); //Get ID of last inserted row from MySQL
echo '<li id="item_'.$my_id.'">';
echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$my_id.'">';
echo '<img src="images/icon_del.gif" border="0" />';
echo '</a></div>';
echo $contentToSave.'</li>';
mysql_close($connecDB); //close db connection
if you use two form just named same field name "content_txt" and post it .
or when you ajax data change it name on ajax field .
I'm making a supplies requisition form where the user can request supplies. I did this tutorial that allowed me to dynamically add form elements. I was able to produce duplicates of Stock_No, Quantity_Available and Quantity text fields and an Item_Name dropdown list. How am I suppose to insert the values that the user inputs in the dynamically added form elements?
Each dynamically added form element should be using array syntax for the name of each field. Then when the form is submitted your PHP script should be receiving array values for each item. At that point you just need to loop through the arrays to add them to the database.
Basic Example:
$item_names = $_POST['item_names'];
$item_prices = $_POST['item_prices'];
$item_qty = $_POST['item_qty'];
$num_items = count($item_names);
for ($i = 0; $i < $num_items; $i++)
{
// Create your SQL
}
I've made complex form which is valid in action. I'd like to get value from this input to save file on server.
<input type="text"
name="produkty[pForm][1][caption]"
id="produkty_pForm_1_caption" />
I've tried something like that:
$this->form=new ProduktyForm();
if ($request->isMethod(sfRequest::POST))
{
$this->form->bind($request->getParameter('produkty'),$request->getFiles('produkty'));
if ( $this->form->isValid())
{
$file=$this->form->getValue('produkty[pForm][1][src]');
$filename='u';
$extension = $file->getExtension($file->getOriginalExtension());
$file->save(sfConfig::get('sf_upload_dir').'/'.$filename.$extension);
}
}
But it doesn't work.
'produkty' is the name of your form. Are you using a subform to capture an array of possible files to be input?
You could get the values of the entire form by doing this.
$form_vals = $this->form->getValues();
Then you could could see what variables you have by your output.
You'll probably be able to get the input this way.
$caption = $form_vals['pForm'][1]['caption'];
this is working fine but how to fetch the values from file attributes . i cant get the values from file input
my main form name is slide and subform is mslide
here is the my code
$this->multiSlideForm->bind($request->getParameter('slide'), $request->getFiles('slide'));
$form_vals = $this->multiSlideForm->getValues();
echo $form_vals['mslide'][0]['slide_name']; //working
echo $this->multiSlideForm->getValue('[mslide][0][file_name]')->getOriginalName(); //not working
I have a PHP form which shows one 'User details' block by default. 'User details' section has first name, last name and address etc. I have a requirement to repeat the same block after clicking on the button so the user can enter 'n' number of users into on the form. Also I need to save all the information in MySQL database. What is the best way to achieve this? Please let me know. I appreciate any help.
Thank you.
I would use jQuery to add additional form elements. You'll need to give each element a different 'name' attribute though- I'd use something like name='user1name' (and in the next username field name='user2name'). Comment on my post if you need specific help with that. When the form submits, I'd use php to loop through each set of fields with an insert statement at the end of the loop.
PHP is something like this:
for ($i = 1; is_null($_POST['name' . $i]) == false; $i++) {
$name = $_POST['user' . $i . 'name'];
// repeat for all your fields
// do your insert statement here - 1 insert per user created
mysql_query("INSERT INTO table_name (Name, Address, ...) VALUES ('$name', '$address', ...)");
}
jQuery is something like this:
Existing HTML
Should only have 1 div with class=user at this point
<div id='fields'>
<div class='user'>
<input type='text' name='user1name' />
<input type='text' name='user2name' />
... more elements ...
</div>
</div>
jQuery
var userFields = $("div.user").clone();
$(userFields).appendTo("div#fields");
$("div#fields").children("div.user:last").children().each(function() {
// change names here!
});
This will copy the existing field and add its copy to the fields div. It will also set the new names for the second set of user fields. Note: you only need ot set userFields once, and then you can use $(userFields).appendTo("div#fields"); as many times as you need to (as long as the userFields variable is in scope).
If you have event handlers attached to your elements in the user field, use $("div.user").clone(true, true); instead of $("div.user").clone();.