dynamic text field in php [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have created a table. There are dynamic text field for each row to fill the information. I know how to create and dynamic text field. But i am having a doubt in how to add them into database by clicking a submit button. In this picture you can get the idea. By clicking that add link we can add dynamic text field as we need. when i click submit all data are saved into same name(ex: saving in tr).

you should use a software like phpmyadmin and create a database there. once created, name a table for example 'mytable' and add the necessary columns corresponding to your data. so when you press the submit button, it should take action on a separate php file which would use POST method to take the values and contain them in a variable. once in a variable create an sql statement like this one "INSERT INTO mytable .............. " with ....... being your values. its a long process though, you should use a website to learn how to insert data into your database

Your field name should be array type.
Example [Row 1]
<input type="text" name="name[]" />
<select name="types[]">
<option>....</option>
<option>....</option>
</select>
Example [Row 2]
<input type="text" name="name[]" />
<select name="types[]">
<option>....</option>
<option>....</option>
</select>
Note : Follow above code for every rows.
After submitting form data, PHP array looks like.
array(2) {
["name"]=>
array(2) {
[0]=>"Name 1"
[1]=>"Name 2"
}
["types"]=>
array(2) {
[0]=>"Type 1"
[1]=>"Type 2"
}
}
Use PHP loop and separate every rows for saving data to your database.

Related

Add hidden value to imput form (probably JS) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
So, I have this html form which the user fills up with his/her info. There is the mobile number field.
I´d like to add the country code automatically, but hidden from the user (since it´s going to be just a local - geographic - marketing action, it´s a fixed code), so I can receive the mobile number with the country code added directly to the database.
All I need is that the form (or JS) adds the value "11" to the beginning of the mobile number without the user noticing it. The user writes "321-456789" and I will receive "11321456789".
How to code that?
Thanks!
You can use JS to prepend (kudos to Stephen P's amazing vocabulary) "11" to the value of the input field like so:
function append11(){
var mobilenumber = document.getElementById("phonenumber");
var front = document.getElementById("front").value;
mobilenumber.value=front+mobilenumber.value;
}
<input type="number" id="phonenumber"><input type="number" value="11" id="front" hidden><button onclick="append11()">
Append 11
</button>
In the example above, whatever you input, after clicking the 'Append 11' button, will add "11" to the front of the value. You can configure this to submit a form like so:
function append11(){
var mobilenumber = document.getElementById("phonenumber");
var front = document.getElementById("front").value;
mobilenumber.value=front+mobilenumber.value;
alert(mobilevalue.value);
}
<form action="something" method="GET">
<input name="phonenumber" type="number" id="phonenumber"><input type="number" value="11" id="front" hidden><button onclick="append11()">
Submit
</button>
</form>

How to pass multiple inputs to jsp

In my jsp file I generate dynamically multiple input tags, trough a database. I'd like to know the values of each. I've tried doing it in Javascript, but according to some answers in this website this is not possible.
Example:
<input type="number" id="age" class="v">
<input type="text" id="name class="v">
...
And on the jsp side I'd like to get:
"age" => 18
"name" => "Joe"
Any ideas on how to achieve this?
Edit
In case you are wondering, my Javascript is fairly simple, I can get all the values I need simply by doing:
var chars = document.getElementsByClassName("v");
EDIT 2
My (simplified) JSP looks something like this:
<%= session.getAttribute("chars")%>
<form action="hello" method="POST">
<c:forEach items="${chars}" var="ch">
<input type="number" id="${ch}" class="v">
</c:forEach>
<input type="submit">
</form>
"chars" is an array that was created through calls to the database, this array is displayed and created dynamically.
So what I want to do is pass all those values, like ("age" => 18) to another my hello servlet, so I can work on that info. For example if the value of the inputs is something like this:
//ID value
"age" => 18
"name" => "Joe"
On hello I should have access to that.
Using pure JavaScript you can get the field values using querySelector which allows you to retrieve properties from multiple elements with the same class identifier
document.querySelector('.v').value;
You will then have access to both field values via js. To read those values using jsp you the JavaScript will need to be included within the same file. You can do something like:
JSP:
You will need to add the HTML Name="myFieldName" to the input fields.
<%= request.getParameter("myFieldName") %>
If you are in a form, you could use a POST method to submit the datas of the different inputs inside your form.
Here is a previous post on StackOverlow that might help you get the datas you want :
How can I get form data with JavaScript/jQuery?

Dynamically displaying the column names

I have a requirement where I have to display questions dynamically based on the buttons clicked. I have coded a table where in one column I display the question and another column I want the user to enter answer. Now when I save this form data I actually need to save the question id and not the question. So I thought I can display the question in the name area like <td> question 1 </td> and in the value field I can keep the question id.
<td>
<input type="text" id="q1" path="genericq1" value=""> dynamic question name
</td>
I am getting the questions by making an ajax call
$('#q1').val(data.question[key1].question);
Can anyone please hint me a way by which I can dynamically display question in the column and keep the question value in the value field which goes in the back end?
Have you heard about hidden field? Use hidden field to store your id. And for displaying name use label instead.
Like in your code
<td>
<input type="hidden" name="qid" id="qid"/>
<label id="qname"></label>
</td>
And when you getting data from ajax call write it as below
$("#qid").val(data.question[key1].questionid);
$("#qname").text(data.question[key1].questionname);
You can easily associate data with an element in jQuery
$('#q1').data('qid',data.question[key1].questionid);
Which can then be retrieved using
$('#q1').data('qid');

How do you add radio button answers to an array of user answers?

I'm trying to create a user questionnaire where, similar to a quiz on BuzzFeed, a user selects their preferences and is given an answer based on how their preferences match up to a table on a database.
For example, here is one of the questions:
<form class="testAns" id="heightAns">
<input type="radio" name="answer" value="short">-25%
<input type="radio" name="answer" value="short">-10%
<input type="radio" name="answer" value="mid">normal
<input type="radio" name="answer" value="tall">+10%
<input type="radio" name="answer" value="tall">+25%
</form>
In the "values" section, I have added the description that I want the answer to be associated with. These "values": short, tall, and mid will show up on the database, but I don't understand enough about Javascript or PHP to connect a user answer on the HTML to an entry on a database.
I think what I need to do is create a Java Script function for each question where the answer that the user selects is logged as its corresponding value. Then, once all tests are completed, I need a PHP function that logs the user profile into the database
You might be right, you might be wrong with your assumptions. It all depends on how exactly you want to display the questions and process the answer.
The simplest solution I would recommend so far, is not to use javascript at all and just use the form element for what it was designed: sending form data to a server.
Extend the form element with the attributes method="POST" and action="url_of_your_page processing_the_data"
Then on that php page you get the value via $_POST["answer"]. If you got that working, you might want to spice it up to your hearts content.
The value of the radio button should me the value which you are going to store in the database. If -25% is short and -10% is also short why add another radio button just use one
<input type="radio" name="answer" value="short">-10% or less
Same goes for tall.
Now to answer you question you can use ajax to submit the form to a php file which stores the result in mysql OR Make one Form place all the questions in the separate div tags and on submit the form will be submited to a php file
<form action='answer.php' method="POST">
<div>
Question 1 ............
<input ......
</div>
<div>
Question 2 ............
<input ......
</div>
</form>
The above code is just an example, you'll have to make a php file named answer.php to process your form

How to add another option to website and

I have a javascript that shows and hides options from a html form. What I am trying to do is be able for the user to add a new category and then write that into the database as an option and add that subject to the option form.
Here is an example of the html code,
<select id="team1" name="team1" style="display:none">
<option value="subcat1">subcat1</option>
<option value="subcat2">subcat2</option>
<option value="subcat3">subcat3</option>
I then want them to be able to add another subcat4 via the website.
the javascript i'm using is an if statement with show and hides from different selects.
The user needs to be able to enter the category name and add it to the relevant select.
Currently im unsure on how to get this to add and save the new subcat4 to the database and to the html.
Any help would be appreciated.
When you first load the page you construct the select element by using the values stored in the database.
Then you can have a form that posts the filled info and stores subcategories into the database. This form may contain the identifier of your select for knowing in which select you are assigning the new option. It could be in a hidden input.
here comes a simple example of that form
<form method='post' action='/your/action/here' >
<input type='hidden' name='main_category' value='team1' />
Subcategory: <input type='text' name='subcategory' value='' />
<input type='submit' name='submit_btn' value='submit' />
</form>
You could also do it without page refresh by using an ajax post (this should be your next step, make it first work the simple way) to store the info and then by appending the option to your select using javascript. Check the jquery append or the appendChild() if you are not using jquery.
With jquery you can do this code to append new child in your select element.
$('#team1').append('<option value="subcat4">subcat4</option>');

Categories