How to post textbox value after selecting particular checkbox - php

Here is the snippet of html code.....
1)After checking each check box the value's is posting to the database.
2)But the problem is when i checked other, i need to take the value of the text box and it is posting the value of the checkbox instead of the textbox value
but i don't know where i did mistake...
<form action="purchase.php" name="form1" id="form1" method="POST">
<ul class="n_ul"> <span>*</span>
What is your Primary goal?
<br>
<br>
<li>
<input name="goal" id="goal" value="Add a popular customer service to attract/retain more customers"
type="checkbox">
</li> <span>*</span>
Popular customer Services
<br>
<br>
<li>
<input name="goal" id="goal" value="Add a turnkey revenue sources for my location(s)"
type="checkbox">
</li> <span>*</span>
trunkey revenue source
<br>
<br>
<li>
<input name="goal" id="goal" type="checkbox" value="other">
</li> <span>*</span>
Other (Please specify below)
<br>
<br>
<input name="other" id="goal" type="text" class="new">
</ul>
<input type="submit" name=submit value="submit">
</form>
Any suggestions are acceptable....

Try something like
$_POST['goal'] = ($_POST['goal']=='other') ? $_POST['other'] : $_POST['goal'];
This will overwrite the value of goal with the value of other only when the 'other' radio is ticked
Also id attributes of html elements should be unique on the page
EDIT
Your question is a little vague. It seems like you may want the form to submit when a checkbox button is clicked.
If this is the case the other field will be unlikely to be filled as the form will often be submitted before the user gets to populate it
Try adding a button or input to submit the form
Like this
<button type="submit">Submit</button>

You can check the value of your checkbox, and if it equals to other, you can grab your value from textbox. Do it following way:
if (isset($_POST['goal']) && $_POST['goal'] == 'other')
{
// do something with $_POST['other']
}

Try this
<form action="" name="form1" id="form1" method="POST">
<ul class="n_ul"> <span>*</span>
What is your Primary goal?
<br>
<br>
<li>
<input name="goal[]" id="goal" value="Add a popular customer service to attract/retain more customers"
type="checkbox">
</li> <span>*</span>
Popular customer Services
<br>
<br>
<li>
<input name="goal[]" id="goal" value="Add a turnkey revenue sources for my location(s)"
type="checkbox">
</li> <span>*</span>
trunkey revenue source
<br>
<br>
<li>
<input name="goal[]" id="goal" type="checkbox" value="other">
</li> <span>*</span>
Other (Please specify below)
<br>
<br>
<input name="other" id="goal" type="text" class="new">
</ul>
<input type="submit" name=submit value="submit">
</form>
<?php
$other="";
$goal=$_REQUEST['goal'];
if(in_array("other", $goal)){
$other=$_REQUEST['other'];
}
echo $other;
?>

Related

Passing href link to php form

I've got a small form where I store information regarding some user choices. I'm not sure though how I can pass href links that are different for each user.
Here is a small example of the code:
<form method="post" action="save.php" class="form">
<fieldset>
<input type="checkbox" name="checkthing" value="g1">
<label>Option 1</label>
</fieldset>
<fieldset>
<input type="checkbox" name="checkthing" value="g2">
<label>Option 2</label>
</fieldset>
<fieldset>
<input type="checkbox" name="checkthing" value="e1">
<label>Option 3</label>
</fieldset>
<fieldset>
<input type="checkbox" name="checkthing" value="e2">
<label>Option 4</label>
</fieldset>
<a data-column="1" href="link here" style="color: green;">Text</a> <br>
<a data-column="2" href="link here">Text</a> <br>
<a data-column="3" href="link here" style="color: green;">Text</a> <br>
<a data-column="4" href="link here">Text</a> <br>
<button type="submit" name="save">
<i class="ft-check"></i> Save
</button>
</form>
When href got a style, it means that is customized for the user. What I want is to store the data-column ID for that user but I am not sure how I can achieve that through PHP.
So for the above example I would store the checked checkboxes + data-column 1 + 3.
You could create a hidden form element with javascript like this, and pull it off when the form is submitted as $_POST['style_ele']
$("form.form").find("a").each(function(){
if($(this).attr("style")!==undefined){
$("form.form").append("<input type='hidden' name='style_ele[]' value='"+$(this).data('column')+"'/>");
}
});
But, if you are generating that style attribute from server-side code like php, you could just create additional hidden input along with the a tag

Processing of an html-form that consists of buttons

I have an html-form which includes some information to be sent to the php-script. The type of an input of this information is "button". It is important that all the values are saved.
<form name="CoordinatesReceiving" method="get" action="MainScript.php">
<p> Insert the coordinates of the point: </p>
<label> Insert X <input type="button" name="xCoordinate" id="xCoordinate" value="1"> </label> <br>
<label> Insert R <input type="button" name="radius" id="radius" value="5"> </label> <br>
<input type="submit" name="send" value="Send.">
</form>
The form has to "remember" that I pressed these buttons and send them to the server.
How can I implement this?
An example for the x-button:
<label for="xCoordinate">Insert X</label>
<input type="button" name="xCoordinate" id="xCoordinate" onclick="this.value=1">

Parsing HTML Checkbox checked values into php using Post

I am in a silly position where I can't figure out to get the values of the checked checkboxes.
<form id="civilForm" method="POST" action="form.php" enctype="multipart/form-data">
<p>
<label>
<input id="12D" name="programsRequested[]" type="checkbox" />
<span>12D</span>
</label>
</p>
<p>
<label>
<input id="xp" name="programsRequested[]" type="checkbox" />
<span>XPStorm</span>
</label>
</p>
<p>
<label>
<input id="autoTurn" name="programsRequested[]" type="checkbox" />
<span>AutoTurn</span>
</label>
</p>
<p>
<label>
<input id="hecras" name="programsRequested[]" type="checkbox" />
<span>HEC RAS</span>
</label>
</p>
Then I am using a php loop as there are a bunch more checkboxes coming.
It spins through fine, but only give me a list that says: on, on, on which correctly tells me how many I checked, however does not give me the value of the checked box.
$selectedPrograms = 'None';
if(isset($_POST['programsRequested']) && is_array($_POST['programsRequested']) && count($_POST['programsRequested']) > 0){
$selectedPrograms = implode(', ', $_POST['programsRequested']);
}
Is there something obvious I missing on how to get the values here?
add every input element value
<input id="12D" name="programsRequested[]" type="checkbox" value="1" />
form not closed .
submit button also missing .
<input type="submit" name="submit" >

Assign input to a div?

I am trying to create a multi step form by getting questions and answer alternatives from a mysql database through php/ajax (No page reload is necessary). I do however seem to have problems submitting the data if a .php page generates all the divs(at least that is a theory as to why it won't work). The way I am trying to do it seems to work fine if I write it all directly in HTML, but that would not be dynamic and therefore useless for this particular task. Is it possible to create a form like psuedo-coded underneath?
<div>
<div id="stepone" class="section"> </div>
<div id="steptwo" class="section"> </div>
<div id="stepthree" class="section"> </div>
<div id="stepfour" class="section"> </div>
</div>
And then have a PHP site generate the input tags and assign it to the correct div, so that the divs are created in HTML/JS but the inputs like checkboxes and textareas are generated dynamically through PHP. I can't seem to think of a good way to do this?
Worth mentioning that this page is made in JQM (jQuery Mobile) so I think the different div-roles can appear problematic for this task.
A generated question in my PHP script will be something like this:
<form id="eval_form">
<h3>Hva tenkte du om møtet?</h3>
<fieldset data-role="controlgroup" data-type="vertical" data-mini="true" id="2">
<input type="checkbox" name="res[2][1]" id="2_1" value="1"><label for="2_1">asd1</label>
<input type="checkbox" name="res[2][2]" id="2_2" value="2"><label for="2_2">asd2</label>
<input type="checkbox" name="res[2][3]" id="2_3" value="3"><label for="2_3">asd3</label>
</fieldset>
<h3>Hva følte du om møtet?</h3>
<fieldset data-role="controlgroup" data-type="vertical" data-mini="true" id="3">
<input type="checkbox" name="res[3][1]" id="3_1" value="1"><label for="3_1">test1</label>
<input type="checkbox" name="res[3][2]" id="3_2" value="2"><label for="3_2">test2</label>
<input type="checkbox" name="res[3][3]" id="3_3" value="3"><label for="3_3">test3</label>
<input type="checkbox" name="res[3][4]" id="3_4" value="4"><label for="3_4">test4</label>
</fieldset>
<input type="button" id="submit" value="Submit" class="submit-btn">
</form>
The code for my demo program looks like this, and it has no problems being posted:
<form id="eval_form">
<!-- STEP 1-->
<div data-role="content" id="form1" class="section">
<input type="text" name="answer[1]" placeholder="Write something..." class="required"></input><p/>
<input type="text" name="answer[2]" placeholder="Write something..." class="required"></input><p/>
<input type="button" name="next1" value="Next" id="next1" onClick="toggleVisibility('form2')" class="next-btn"/>
</div>
<!-- STEP 2-->
<div data-role="content" id="form2" class="section">
<fieldset data-role="controlgroup" data-type="horizontal" data-mini="true">
<input type="radio" name="answer[4]" id="1" value="1" class="required"/><label for="1">Value 1</label>
<input type="radio" name="answer[4]" id="2" value="2" class="required"/><label for="2">Value 2</label>
<input type="radio" name="answer[4]" id="3" value="3" class="required"/><label for="3">Value 3</label>
</fieldset>
<input type="button" id="back2" value="Back" onClick="toggleVisibility('form1')" class="back-btn">
<input type="button" name="next2" value="Next" id="next2" onClick="toggleVisibility('form3')" class="next-btn"/>
</div>
<!-- STEP 3-->
<div data-role="content" id="form3" class="section">
<fieldset data-role="controlgroup" data-type="vertical" data-mini="true" class="required">
<input type="checkbox" name="answer[5][1]" id="1" value="1"/><label for="1">Testie</label>
<input type="checkbox" name="answer[5][2]" id="2" value="2"/><label for="2">Testoe</label>
<input type="checkbox" name="answer[5][3]" id="3" value="3"/><label for="3">Tester</label>
</fieldset>
<input type="text" name="answer[3]" placeholder="Write something..." class="required"></input><p/>
<input type="button" id="back3" value="Back" class="back-btn" onClick="toggleVisibility('form2')">
<input type="button" id="submit" value="Submit" class="submit-btn"/>
</div>
</form>
Ajax function to send the data:
$(document).ready(function()
{
$("#submit").click(function()
{
var data_string = $('#eval_form').serialize();
$.ajax(
{
type:'POST',
url:'add.php',
data:data_string,
success:function(response)
{
$("#eval").html(response);
}
});
})
});
The way id do it is just have a single div to contain your steps
<div id="stepContainer" >
<input type="text" id="step1Input" />
</div>
Something like that.
The ajax query will take any elements within the step container and submit them appropriatley via GET/POST to your php/asp whatever page does your server logic. When it returns it can return some confirmation or the html required for the next step.
Once you have this returned html or built the new html in javascript based on the response from the server you can replace the contents of the stepContainer with the new html. This will then act as step 2.
You may want to have a hidden div or some counter in javascript to keep track of which is your current step.
It may even be wise to use json return from the server which can allow you to pass more information across (well more easily anyway), allowing you to have error messages, confirmations, html etc embedded in the single response.

How can I split a PHP form into two sections, before submission to a database?

I have set up a PHP form for a competition for users to enter all information to be stored in a database. I used a NetTut+ tutorial to do so.
I've got the form submitting to the database as required, but with so many additional questions being asked, I would like to split the form into two separate sections. Obviously the first page would say continue to the next step before the second step allowing for the form to be submitted to the database.
The content that the user sees should be split, but should all be a part of the same form. Step 1 > Step 2 before submission.
Would anyone know of or recommend any methods to do this?
I've attached the code below.
<form method="post" action="">
<fieldset>
<ul>
<li>
<label for="code">Entry Code On-Pack</label>
<input type="text" name="code" />
</li>
<li>
<label for="name">Name</label>
<input type="text" name="name" />
</li>
<li>
<label for="email">Email</label>
<input type="text" name="email" />
</li>
<li>
<label for="addressone">Address</label>
<input type="text" name="addressone" />
</li>
<li>
<label for="addressone"> </label>
<input type="text" name="addresstwo" />
</li>
<li>
<label for="addressone"> </label>
<input type="text" name="addressthree" />
</li>
<li>
<label for="telephone">Telephone</label>
<input type="text" name="telephone" />
</li>
<li>
<label for="dob">Date of Birth</label>
<input name="dob" type="text" value="[dd/mm/yy]" />
</li>
<li>
<label for="q1">Where have you seen Cookstown advertised?</label><br />
<input type="checkbox" name="q1cb1" /><label for="q1cb1">Magazines</label><br />
<input type="checkbox" name="q1cb2" /><label for="q1cb2">Billboards</label><br />
<input type="checkbox" name="q1cb3" /><label for="q1cb3">Television</label><br />
<input type="checkbox" name="q1cb4" /><label for="q1cb4">Radio</label><br />
<input type="checkbox" name="q1cb5" /><label for="q1cb5">Online</label><br />
<input type="checkbox" name="q1cb6" /><label for="q1cb6">Public Transport</label><br />
<input type="checkbox" name="q1cb7" /><label for="q1cb7">Bus Stops</label><br />
</li>
<li>
<label for="q2">How well do you remember those advertisments?</label><br />
<input type="radio" name="q2" value="VeryWell"/><label for="q1cb1">Very well</label><br />
<input type="radio" name="q2" value="FairlyWell"/><label for="q1cb2">Fairly well</label><br />
<input type="radio" name="q2" value="FewDetails"/><label for="q1cb3">A few details</label><br />
<input type="radio" name="q2" value="NotAtAll"/><label for="q1cb4">Not at all</label><br />
</li>
<label for="tc">Do you accept the terms and conditions</label>
<input type="checkbox" name="tc" class="styled" />
</li>
<li> </li>
<li>
<input type="submit" value="Enter Competition" class="large blue button" name="signup" />
</li>
</ul>
</fieldset>
</form>
Use sessions mechanism to store 1 step data
You could pass them to page two and then put them in as hidden variables. You could also use session variables.
example with hidden fields
Sessions are usually the preferred way to do this, but hidden form fields would work just as well.
It's pretty easy to do - after the first submission, store the values into the session - validate them first if you like, it's probably a good idea to do so in fact. Then go to the next page, and once submitted, validate the second bunch of answers and put them into the database.
Hidden form fields work too, but I prefer the session-based approach.
Good luck!
One other option would be to put the additional fields in a hidden div on the same page and use javascript to show them once the first set have been completed. The advantage for you is that it keeps your form processing simpler. Also for your users, they won't have a round trip to the server to get the other part of the form.
This, of course, requires that your users have javascript turned on. The best practice would be to show all fields on the same page by default, and then use JS to hide the second batch before the page renders.

Categories