how to store dynamically created textbox value to db - php

i have a code for dynamically created textbox,radiobutton,checkbox..and my question is How can i save the dynamically created textbox checkbox ,radiobutton into MYSQL Database .
<div class="maindiv">
<div id="header"></div>
<div class="menu">
<button id="namebutton"><img src="images/name-img.png">Name</button>
<button id="emailbutton"><img src="images/email.png">Email</button>
<button id="addressbutton"><img src="images/contact-img.png">Address</button>
<button id="checkboxbutton"><img src="images/check-img.png">CheckBox</button>
<button id="radioaddbutton"><img src="images/radio-img.png">Radio</button>
<button id="reset">Reset</button>
</div>
<div class="InputsWrapper1">
<div id="yourhead">
<div id="your">
<h2 id="yourtitle">Your Form Title<img src="images/edit-form.png"></h2>
<h4 id="justclickid">Just Click on Fields on left to start building your form. It's fast, easy & fun.</h4>
</div>
</div>
<div id="InputsWrapper"></div>
</div>
</div>
here is the link for my code link ....and its working fine for me but not working in jsfiddle above link

I would suggest serializing your form's content into a string and simply storing that string in a field called something like form_data.
To do this, you would need to ensure that all of your elements that you want to save are nested within a <form> tag. Once you have that you can call the .serialize() function on your form element.
From the documentation:
The .serialize() method creates a text string in standard URL-encoded
notation. It can act on a jQuery object that has selected individual
form controls, such as <input>, <textarea>, and <select>:
$( "input, textarea, select" ).serialize();
It is typically easier, however, to select the <form> itself for
serialization
var form_string = $("#my_dynamic_form").serialize();
This serialization will give you a string in the following format:
single=Single&multiple=Multiple&multiple=Multiple3&check=check2&radio=radio2
As you can see, this string can be easily saved into the database in a single column. To decode the values in PHP (for example), you can use the parse_url() function:
$form_string = "single=Single&multiple=Multiple...";
parse_str($form_string, $form_data);
The $form_data will now contain the following data:
Array (
[single] => "Single"
[multiple] => "Multiple"
...
)

Related

How can I get the value from a cell when clicking other cell in the row?

I have a page where I make a table from php deploying the result of a previous query. It shows the ID in the first column. I want to have some colums clickable to edit the value, changing it in the database and refreshing the page. For that it appears a Bootstrap modal when you click the link columns (the ones with ). When you fill the modal it sends the information to a JQuery script that calls a php page via AJAX. That php page can be called asynchronously to edit the value in the database and it requires the new value and the ID of the row to make the UPDATE SQL statement.
I need to send the ID of the row you click to the JQuery method that uses AJAX. But in the moment i call the function, i don't know how to send the ID as parameter.
I have tried to find the ID of the row you click by touching the DOM vía JQuery adding ids to the rows but it's just too complicate for my level.
This is where php deploys the table with the information.
$resISDEFE = mysqli_query($conexion,"SELECT * FROM personal_isdefe WHERE proyecto=".$idProyecto);
if($resISDEFE->num_rows>0){
$index=0;
while($isdefe = mysqli_fetch_assoc($resISDEFE)){
$resCategoria = mysqli_query($conexion,"SELECT * FROM categoria_isdefe WHERE id=".$isdefe['categoria']);
$categoria = $resCategoria->fetch_row();
$idISDEFE = $isdefe['id']; <-- I save the row's ID here -->
echo "<tr id='isdefe'>";
echo "<td><p>".$idISDEFE."</p></td>";
echo "<td><p>".$categoria[1]."</p></td>";
echo "<td><p>".$isdefe['edg']."</p></td>";
echo "<td><p><a data-toggle='modal' data-target='#modalConcepto'>".$isdefe['porcentaje']."%</a></p></td>";
echo "<td><p>".$isdefe['horas_contratadas']."</p></td>";
$importe_prestacion = $isdefe['horas_contratadas'] * $categoria[2];
echo "<td><p>".number_format($importe_prestacion,2,",",".")." €</p></td>";
echo "<td><p>".number_format(($isdefe['importe_variable']+$importe_prestacion),2,",",".")." €</p></td>";
echo "</tr>";
}
This is the modal it appears.
<div class="modal fade" id="modalConcepto" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Editar concepto</h4>
</div>
<div class="modal-body">
<div class="form-group">
<textarea class="form-control" rows="10" id="newConcepto"></textarea><br>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" onclick='editConcepto(<?php $idISDEFE ?>)'>Guardar</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>
</div>
</div>
</div>
And this is the JQuery method that is called when you click "Guardar".
function newConcepto(id, newConcepto){
if(newConcepto!=""){
$.get("ajax/editarConceptoISDEFE.php?id="+id+"&con="+newConcepto, function(data){
if(data=="1"){
var time = setTimeout(function(){
location.reload();
},500);
}
else alert("Error al actualizar el concepto");
});
}
}
function editConcepto(id){
var newConcepto = $('#newConcepto').val();
newConcepto(id,newConcepto);
}
}
The expected result is that when the page is reloaded, you could see the changed value exactly in the row you clicked before.
There's a lot going on in this question, but I'm going to give it a shot and you can steer me in a different direction if this isn't what you're looking for.
How to get the value of another cell in a row with jQuery
Wrap the cell you want to get in some sort of identifier span or div (in my example it's .span-of-other-cell). Use the jQuery .closest function to get the "row," and then use .find to locate your identifier and its value:
$(document).on('click', '.some-cell', function () {
var id = $(this).closest('.table-row').find('.span-of-other-cell').val();
});
Some helpful links:
.closest: https://api.jquery.com/closest/
.find: https://api.jquery.com/find/
How I personally store IDs for later use
I use html's data- prefix for element attributes.
<div data-id="12345"></div>
If you add a data- attribute to your row, the javascript above is simplified, and the ID visually hidden (though easy to access for anybody with basic browser skills, so this is not meant to obfuscate or be secure if needed):
$(document).on('click', '.some-cell', function () {
var id = $(this).closest('.table-row').attr('data-id');
});
Let me know if this helps, or if I misunderstood and need to go a different direction with my answer.
As for passing the ID back as a parameter...
This really depends upon your goals. If the ID doesn't need to be secure (and it sounds like it's displayed in a table, so in no need of being secured), then you could just pass it back to PHP with AJAX using the GET protocol:
// assume that var "id" is accessible from this ajax function
$.ajax({
url: "script.php?id=" + id
});

PHP - dropdown list content showing href link content without selection

I have a variable numbr forms I want a user to be able to choose to view via a drop down list. I have assigned each of the form content to array $forms & the associated form name to array $formnames
The idea was to display the variable number of form names as buttons in the drop down selection, and once selected it would display the form content.
I've tried the below code but this leads to the drop down selection immediately showing both the form content as well as name. Could someone point me in the right direction given my limited understanding?
<div class="dropdown">
<button onclick="dropdownfunction()" class="dropbtn">Available List</button>
<div id="avaiablelist" class="dropdown-content">
<?php
$keys = array_keys($forms);
$namekeys = array_keys($formnames);
$arraysize = count($forms);
for($i=0; $i<$arraysize;$i++) { ?>
<?php echo $formnames[$i]['formname']; ?>
<?php
}
?>
</div>
</div>
In this case i would use
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> method="post">
</form>
to send $forms[$i]['form'] via Post method

Populating select tag from selected items in php

i am totally new on PHP and I have an issue, i hope i can explain my problem exactly.
I have two HTML5 forms where one of the form is being populated by php for loop and inside the same form there is there is a submit button code as follows:
<form class="" name="upgradeChosen" role="form" method="post" action='index.php?action=upgradeChosen'>
<div class="col-md-5">
<select name="upgradeSelected" id="ID_UPGRADE_SELECTED">
<?
foreach ($arrayUpgrade as $value) {
$pro = $value->getnewProduct();
echo '<option value="'.$value->getId().'">'.$pro->getName()." "."===>". $pro->getPrice(Currency::getCurrentCurrency()).'</option>';
}
?>
</select>
</div>
<button style="margin-top: 40px" id="ID_CREATE_ESTIMATE" type="submit" class="btn btn-default"><i class="icon-rocket"></i>Create estimate</button>
</form>
Inside the another PHP file there is a function which is called function upgradeChosen and it parses the selected data from select tag. at the second form i would like to generate a list and populate the selected items from first listbox or select tag. Is this possible ?
Here is my second form code as follows:
<form class ="" name ="upgradeEstimate" role="form" method="post" action='index.php?action=Frontend_upgradeEstimate'>
</form>
and the function which is called upgradeChosen code as follows:
public function upgradeChosen (){
$idUpgradeLine = $this->getRequestAttribute("upgradeSelected");
$upgrade = new UpgradeRates($idUpgradeLine);
$product = $upgrade->getNewProduct();
}
please help me to achieve this if there is a way of doing it only with php i will be happy i do not want to use Jquery, JavaScript or AJAX at first stage if there is a way how to do it with php only.
For this you must use javascript.
You can use onchange event of your select for call a javascript function and reload your page with a post param. Then write your second select in php.
I think there aren't any other method.
Fabio Del Rosso.

How to pass php variable to modal window

I am making a simple page and I have found this little problem. I have this in my template:
<?php foreach ($this->vypis_serie as $value) : ?>
<div class="serie">
<div id="serie_header">
<?= $value['nazev_cviceni'] ?>
</div>
<div id="serie_info">
<p>Number of excercises: TODO</p>
<p>Sport type: <?= $value['typ'] ?></p>
<p>KCal summary: <?= $value['kcal'] ?></p>
</div>
<div class="button_upravit">Edit</div>
<div class="button_smazat">Delete</div>
</div>
<?php endforeach; ?>
basically it is a block that fills in information about particular exercise (it is a sport app). SO If I have 3 entries in DB, it will print this code three times with corresponding info.
The problem I have is with the edit button, which upon clicking opens modal window. It is made purely with CSS, so no Javascript.
When I click the button, it jumps to this code:
<div id="openModal_edit" class="modalDialog">
<div>
X
<div id="editace">
<form id="platba" action="serie/edit/" method="post" enctype="multipart/form-data">
<fieldset>
<legend>Edit serie</legend>
<ol>
<li>
<label for="name">Name of the series</label>
<input id="name" name="nazev_cviceni" type="text" required autofocus>
</li>
<li>
<label for="typ">Sport type</label>
<select name="typ">
<option value="Kolo">Bike</option>
<option value="Běhání" selected="selected">Running</option>
</select>
</li>
</ol>
</fieldset>
<fieldset>
<button type="submit">Save</button>
</fieldset>
</form>
</div>
</div>
</div>
But since I jump to div id and I am not using a new page where I could choose a controller and pass a variable, I need somehow to pass the variable (id of the exercise) to the modal window, so I can know which of the possible buttons I have clicked. Is there any way to do it, without the need to rewrite all other pages where I have used this modal window?
I can't use another foreach like in the first part, because the modal window is always a single object that appears, unlike all the entries on the page that are there as many times as there are entries in the DB.
Hope that it is understandable, sorry for my English :)
The simplest way to do this using a single modal window involves adding some javascript code to your page.
First, add the relevant information to the edit link, with a new data-serie-<name> for each piece of data you want to pass:
<a href="#openModal_edit" data-serie-name="<?= $value['nazev_cviceni'] ?>" ...>
Next, add an onclick event handler to that same link. This handler will extract the embedded data from the <a> element and inject it in the modal window. The dataset element provides access to the data-* attributes from javascript
// Example
onclick="serieName=this.dataset.serieName;document.querySelector('#openModal_edit input#name').value = serieName;return true;"

Ajax with bootstrap.TbSelect2 (Yii)

I have a Select whose contents are changed when another Select is changed.
On change ajax runs a function in my controller to do this:
$this->renderPartial("_townsselect", array('country'=>$country));
It gets $country from a $_GET.
_townselect.php
$towns= $this->townsselect($country);
$this->widget('bootstrap.widgets.TbSelect2', array(
'name'=>'clienttown',
'asDropDownList' => true,
'data' => $towns
));
townsselect() creates an array structured as: $towns[town-id]='TownName'
When the page is loaded normally it also runs the previous renderPartial with $country set manually as a default.
So it is calling the same code both times. However on page load the Tbselect2 is shown correctly (correct styling, includes js support which gives it a search box, etc...), but when I use ajax a standard Select is used with limited styling. In fact the code produced is different:
Result after page load
<div class="select2-container" id="s2id_clienttown" style="width: 220px">
<a href="#" onclick="return false;" class="select2-choice" tabindex="-1">
<span>Accrington</span>
<abbr class="select2-search-choice-close" style="display:none;"></abbr>
<div>
<b></b>
</div>
</a>
<div class="select2-drop select2-with-searchbox select2-drop-active select2-offscreen" style="display: block;">
<div class="select2-search">
<input type="text" autocomplete="off" class="select2-input" tabindex="-1">
</div>
<ul class="select2-results"></ul>
</div>
</div>
<select name="clienttown" id="clienttown" style="display: none;">
<option value="Select">Select</option>
...
</select>
</div>
Where as after the ajax only the Select is generated.
Temporary Solution
I managed to get this working in some form. Instead of recreating the whole select box I have altered the JQuery code to clear all options inside the Select and refill it with the new list.
The new list comes comes from a JSON string returned by the ajax. Which is processed as follows:
var towns = $.parseJSON(resp);
$('#clienttown option:gt(0)').remove(); //remove all options, but leave default 'Select A Town' option
var sel = $('#clienttown');
$.each(towns, function(id, town){
sel.append($("<option></option>").attr("value", id).text(town));
});
I've put this here in case it helps someone else trying to achieve a similar thing.
Howver I would still be interested to know why yiibooster/bootstrap does not work when rendered after ajax. And if there is a way to make it work, as this solution wont work unless the input is first rendered on page load.
I know this is old but have you tried the following to get the after Ajax rendering to work.
// Notice the ,false, true at the end of renderpartial
$this->renderPartial("_townsselect", array('country'=>$country), false, true);

Categories