PHP Possible to build form elements after a selection is made? - php

I have a form with a select box that determines if a div is hidden or shown. Each div has text boxes and a button. Since my validation is validating ALL divs hidden or shown, is it possible IF a select option is selected PHP can echo the form picked? For example, below I have
<div id="1" class="colors" style="display:none">
<input type="text" name="1">
<button type="submit" name="submit">Submit</button>
</div>
Is it possible to create an array to build the code above when that option is picked? Same goes for the second hidden div -
<div id="2" class="colors" style="display:none">
<input type="text" name="1">
<input type="text" name="2">
<button type="submit" name="submit">Submit</button>
</div>
This way only one form will be created at a time when a option is picked in the select box. Is this possible? Is this a good idea? The original form will have more form elements.. this is just a sample. Here's the code below.
<script>
$(function() {
$('#colorselector').change(function(){
$('.colors').hide();
$('#' + $(this).val()).show();
});
});
</script>
<form action="" onSubmit="return validateForm(this)" method="post" name="form">
<select id="colorselector">
<option selected></option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<div id="1" class="colors" style="display:none">
<input type="text" name="1">
<button type="submit" name="submit">Submit</button>
</div>
<div id="2" class="colors" style="display:none">
<input type="text" name="1">
<input type="text" name="2">
<button type="submit" name="submit">Submit</button>
</div></form>
If this is possible and a good idea, can you provide an example on how this can be accomplished?

Related

html form buttons redirect to same page

I have 2 buttons in my page. I just want to redirect to Addform.php page only when I click on submit button. But my code will redirect to that page from both submit and reset button.
<form id= "myForm" action= "Addform.php" method= "post">
First name:
<input type="text" id="fname"><br/><br/>
Last name:
<input type="text" id="lname"><br/><br/>
Age:
<input type="text" id="age"><br/><br/>
Major Subject:
<select id="select">
<option value="">---</option>
<option value="op1">Mathematics</option>
<option value="op2">Computer</option>
<option value="op3">Statistics</option>
</select><br/><br/>
GPA:
<input type="text" id="gpa"><br/><br/>
<button id="btnSubmit" onclick="validation()" value= "submit">Submit</button>
<button id="btnReset" onclick= "resetForm()">Reset</button><br/>
</form>
you must code like this
with type of button they dident submit the form
simple way is set type of button like this type = "reset"
<form id= "myForm" action= "Addform.php" method= "post">
First name:
<input type="text" id="fname"><br/><br/>
Last name:
<input type="text" id="lname"><br/><br/>
Age:
<input type="text" id="age"><br/><br/>
Major Subject:
<select id="select">
<option value="">---</option>
<option value="op1">Mathematics</option>
<option value="op2">Computer</option>
<option value="op3">Statistics</option>
</select><br/><br/>
GPA:
<input type="text" id="gpa"><br/><br/>
<button type="button" id="btnSubmit" onclick="validation()" value= "submit">Submit</button>
<button type="button" id="btnReset" onclick= "resetForm()">Reset</button><br/>
<!-- or -->
<!--<button type="reset" >Reset</button>-->
</form>
button by default submits the form. Use input instead:
<input type="button" id="btnSubmit" onclick="validation()" value= "Submit">
<input type="button" id="btnReset" onclick= "resetForm()" value="Reset">
Alternatively I think you can still use button but you must prevent the default behaviour.
Using plain javascript:
document.getElementsByTagName("button").forEach((item, index)=>{
item.addEventListener('onclick', function(event){
event.preventDefault()
});
})
Using jQuery:
$('button').click(function(event){
event.preventDefault();
});

jquery keep showing div after refresh if php validation fails

I have a select box that shows and hides form elements using jquery. The hide and show works. But when I submit the form, if there's PHP validation errors, the form that's showing is hidden after refresh. I've searched and search but can't find a solution that fits with my code. If someone could show me how to show the form submitted if validation errors exist that would be great. See code below.
<script>
$(function() {
$('#colorselector').change(function(){
$('.colors').hide();
$('#' + $(this).val()).show();
});
});
</script>
<form action="" onSubmit="return validateForm(this)" method="post" name="form">
<select id="colorselector">
<option selected></option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<div id="1" class="colors" style="display:none">
<input type="text" name="1">
<button type="submit" name="submit">Submit</button>
</div
><div id="2" class="colors" style="display:none">
<input type="text" name="1">
<input type="text" name="2">
<button type="submit" name="submit">Submit</button>
</div></form>

Save multiple selection and retrieve selected using select2

I have a profile edit form below in which I am asking the user to select multiple cities (I left 4, but on my page I am looking to add around 29,000) from a select2 dropdown menu using the following code, but the selected values do not save when I press the submit button and therefore when the page reloads there are no selected values.
<form action="/edit-my-profile/?action=edit&job_id=5792" method="post" id="submit-job-form" class="job-manager-form" enctype="multipart/form-data">
<fieldset class="fieldset-html">
<label for="html">HTML</label>
<div class="field ">
<select name='job_listing_region[]' id='regions' style='width:300px' multiple>
<option>Boca Raton,FL</option>
<option>New York City, NY</option>
<option>Los Angeles, CA</option>
<option>Boone, NC</option>
</select>
</div>
</fieldset>
<p>
<input type="hidden" name="job_manager_form" value="edit-job" />
<input type="hidden" name="job_id" value="5792" />
<input type="hidden" name="step" value="0" />
<input type="submit" name="submit_job" class="button" value="Save changes" />
</p>
</form>
and the following jquery script
<script>
jQuery(function($) {
$(document).ready(function() {
$("#regions").select2({
});
});
});
</script>
Is there a way I can have the selection box save the selected values?

in html post to php form: when button type = button, how to pass value?

You're setting up some attribute values for an object, such as colour, size, weight etc in a form. If using buttons to specify these values before wishing to post all the information to a php page for further processing - how do you get the values passed to php, if the buttons are not in themselves submitting the form?
For example:
<form action="processgivenvalues.php" method="post">
choose colour: <button type="button" name="colour" value="green"></button>
<button type="button" name="colour" value="blue"></button>
choose size: <button type="button" name="size" value="big"></button>
<button type="button" name="size" value="small"></button>
<input type="submit">
and on php page:
<?php
echo You specified:
echo $size;
echo $frame
?>
Many thanks.
The point of a type="button" is to be a thing you can hook JavaScript up to. It isn't designed to send values to the server.
Submit buttons are, but you want to let the user pick from multiple sets rather then just one, so you can't use those either.
While you use buttons with JavaScript that generates/updates hidden inputs with the values you want, you really should just use radio buttons instead. They are designed to let users pick one item from a group.
<form action="processgivenvalues.php" method="post">
<fieldset>
<legend>Colour</legend>
<label>
<input type="radio" name="colour" value="green"> Green
</label>
<label>
<input type="radio" name="colour" value="blue"> Blue
</label>
</fieldset>
<fieldset>
<legend>Size</legend>
<label>
<input type="radio" name="size" value="big"> Big
</label>
<label>
<input type="radio" name="size" value="small"> Small
</label>
</fieldset>
<input type="submit">
</form>
You could use jQuery for this.
html:
choose colour: <button type="button" class="colour" value="green">Green</button>
<button type="button" class="colour" value="blue">Blue</button>
choose size: <button type="button" class="size" value="big">Big</button>
<button type="button" class="size" value="small">Small</button>
<button id='submit' type="button">Submit</button>
jQuery:
You make the form on the run
$(document).ready(function(){
window.size = '';
window.colour = '';
$(".colour").click(function() {
window.colour = $(this).val();
});
$(".size").click(function() {
window.size = $(this).val();
});
$("#submit").click(function() {
if(window.size == '' || window.colour == ''){
return alert('Choose colour and Size');
}
var form = $('<form></form>');
$(form).hide().attr('method','post').attr('action',"processgivenvalues.php");
var input1 = $('<input type="hidden" />').attr('name',"size").val(window.size);
var input2 = $('<input type="hidden" />').attr('name',"colour").val(window.color);
$(form).append(input1);
$(form).append(input2);
$(form).appendTo('body').submit();
});
});
jsFiddle
This can also be done with only javaScript.
As Quentin stated, buttons are not useful for selecting between options, but you comment that you want something customizable with an image...
Option 1: Use <label for="id"></label> with a radio button in a table or list.
<table>
<tbody>
<tr>
<td style="background-color: green;">
<label for="green">
<input name="colour" value="green" id="green" type="radio">
</label>
</td>
</tr>
<tr>
<td style="background-color: blue;">
<label for="blue">
<input name="colour" value="blue" id="blue" type="radio">
</label>
</td>
</tr>
<tr>
<td>
<label for="big">
<input name="size" value="big" id="big" type="radio">
<big>Big</big>
</label>
</td>
</tr>
<tr>
<td>
<label for="small">
<input name="size" value="small" id="small" type="radio">
<small>Small</small>
</label>
</td>
</tr>
</tbody>
</table>
Option2: Use droplists with styling:
<select name="colour2">
<option value="0">Select a color</option>
<option value="green" style="background: green;"> </option>
<option value="blue" style="background: blue;"> </option>
</select>
<br>
<select name="size2">
<option value="0">Select a size</option>
<option value="big" style="font-size: 16px;">Big</option>
<option value="small" style="font-size: 12px;">Small</option>
</select>
Here's a demo fiddle I created (just signed up)

I have one select list, which should send data with a few forms

I have that kind of site: (set of forms)
<select id="group_dropdown" name="selectinput">
<option value=0>all</option>
<option value=1>1 option</option>
<option value=2>2 option</option>
</select>
<form method="post" class="form-inline" id="form11">
<input type="text" name="1forminput" placeholder="0"></label>
<button type="submit" class="btn btn-success ml40" >save</button>
</form>
<form method="post" class="form-inline" id="form22">
<input type="text" name="forminput" placeholder="0"></label>
<button type="submit" class="btn btn-success ml40" >save</button>
</form>
I want to send data from this select (which is "mutual" for both of these forms) when any of these forms is sent (like an additional field withind sending form).
Is it a simple solution for that?
Add hidden inputs, and have valid markup with starting label tags etc
<select id="group_dropdown" name="selectinput">
<option value="0">all</option>
<option value="1">1 option</option>
<option value="2">2 option</option>
</select>
<form method="post" class="form-inline" id="form11">
<label>
<input type="text" name="1forminput" placeholder="0" />
<input type="hidden" name="select_value" />
</label>
<button type="submit" class="btn btn-success ml40">save</button>
</form>
<form method="post" class="form-inline" id="form22">
<label>
<input type="text" name="forminput" placeholder="0" />
<input type="hidden" name="select_value" />
</label>
<button type="submit" class="btn btn-success ml40">save</button>
</form>
Then fill in the hidden inputs when the select's value changes:
$('#group_dropdown').on('change', function() {
$('[name="select_value"]').val(this.value);
}).trigger('change');
And the select's value will be sent with both forms when they are submitted, and accessed like:
$_POST['select_value'];
you can add a new parameter after serializing your form like this:
$('#form11').submit(function(){
$.ajax({
type : 'POST',
url : 'url',
data : $('#form11').serialize() + "&select="+$('#group_dropdown').val()
}).done(function( data ) {
//callback
});
return false;
});

Categories