Simple Checkbox values to SQL database - mysql

Sorry for asking something that seems to have been asked many times, but is because I'd not found anything so... simple as my case, so I'm not so sure about that path to follow.
The thing is, I have a form, in the page "estimate.php" with most of its input types as Text, and some checkboxes. Its action is set to "insert.php", where is the code that sends the e-mail, and sends the data to the database, and on the end of it, an alert and a redirection to the form page.
When I fill the form without checking anything on the checkboxes, everything works just fine, the database columns are filled perfectly, and the e-mail is correctly sent, the fields that were supposed to receive the checkboxes values are blank, as it was supposed to be, since I've not filled them. But once I try to use the checkboxes, when I hit Submit, I get a empty page, and the address freezes on insert.php. I'm not sure what is happening, but since I haven't worked with Checkbox to SQL before, and I'm pretty newbie on coding, I think it has something to do with the way I'm trying to process the data from the checkboxes to send it to the db. I understood that, setting values to each checkbox, it would post them as text.
Here are the codes (the most important parts of if, to try to summarize. [...] are in place of parts I took away, to make it more simple for you to help me.)
-
FORM:
<form id="contact-form" action="../painel/insert.php" method="POST">
//[...]
<div style="margin-top: 20px;">
<p style="margin-bottom: 0;">Quantidade de pares por numeração</p>
<br />
<div class="table-responsive">
<table class="table table-bordered">
<tr>SANDÁLIAS INFANTIS</tr>
<thead>
<tr>
<th class="text-center">Numeração</th>
<th class="text-center">Quantidade</th>
<th class="text-center">Alça</th>
</tr>
</thead>
<tbody>
<tr>
<td>25/26</td>
<td><input type="text" name="i2526_quant"></td>
<td style="text-align: left;">
<input type="checkbox" name="i2526_a" value="Slim"> Slim<br>
<input type="checkbox" name="i2526_a" value="Tradicional"> Tradicional
</td>
</tr>
<tr>
<td>27/28</td>
<td><input type="text" name="i2728_quant"></td>
<td style="text-align: left;">
<input type="checkbox" name="i2728_a" value="Slim"> Slim<br>
<input type="checkbox" name="i2728_a" value="Tradicional"> Tradicional
</td>
</tr>
<tr>
<td>29/30</td>
<td><input type="text" name="i2930_quant"></td>
<td style="text-align: left;">
<input type="checkbox" name="i2930_a" value="Slim"> Slim<br>
<input type="checkbox" name="i2930_a" value="Tradicional"> Tradicional
</td>
</tr>
<tr>
<td>31/32</td>
<td><input type="text" name="i3132_quant"></td>
<td style="text-align: left;">
<input type="checkbox" name="i3132_a" value="Slim"> Slim<br>
<input type="checkbox" name="i3132_a" value="Tradicional"> Tradicional
</td>
</tr>
</tbody>
</table>
</div>
//[...]
<input type="submit" class="submit no-margin-bottom" value="Enviar" style="font-size:20 px; padding: 10px; border: 3px solid #555555;"/>
</form>
-
THE CODE, on Insert.php:
<?php
include "conexao.php";
//[...]
$i2526_quant = $_POST['i2526_quant'];
$i2526_a = $_POST['i2526_a'];
$i2728_quant = $_POST['i2728_quant'];
$i2728_a = $_POST['i2728_a'];
$i2930_quant = $_POST['i2930_quant'];
$i2930_a = $_POST['i2930_a'];
$i3132_quant = $_POST['i3132_quant'];
$i3132_a = $_POST['i3132_a'];
//[...]
$inserir = "INSERT INTO orcamento VALUES([...],'$i2526_quant','$i2526_a','$i2728_quant','$i2728_a','$i2930_quant','$i2930_a','$i3132_quant','$i3132_a',[...])";
//[...](here is the part of the code that sends the e-mail. As I said, everything is working fine, except for the checkboxes (actually, I took away the part from the code that would send the checkbox values to the e-mail and tested it again, and it didn't work. Since the code is pretty long, I decided not to post it (if you need to, I will).
$enviado = $mail->Send();
if ($enviado) {
//$envia = mail($recipient, $subject, $message, $headers);
echo '<script>alert("Obrigado! Orcamento enviado com sucesso, responderemos o mais breve possível.");location="../estimate.php";</script>';
}else{
echo '<script>alert("Erro ao enviar orcamento.");location="../estimate.php";</script>';
} }
?>
--
Thank you very much in advance. I'm sorry for the poor english, and for being such a newbie, as I'm pretty sure this is a simple thing to solve, but hey, here I am trying to learn from the bests (:

Try to add [] after the checkbox name in your HTML file. This will post them as an array to your PHP file. If you don't do that there is no possibility to have several checked values.
Not sure if this solves your error. You should be able to get more information though by adding the following to the top of your insert.php file:
error_reporting = E_ALL;
ini_set('display_errors', 'On');
This enables error reporting for the file and should output what's going wrong. Make sure to remove that after fixing your error.

Related

Submit Buttons in two different forms go to the same file, they shouldn't

I recently decided to make a little game, and I'm trying to design the first page, to either create an account or go to an existing account.
Here's what I have:
<center>
<table>
<tr>
<td>
<form action = "findMyAccount.html" method = "post">
<input type = "submit" value = 'Login to an Existing Account' />
</form>
</td>
<td>
<form action = "accountMaker.html" method = "post">
<input type = "submit" value = 'Make a new Account'/>
</form>
</td>
</tr>
</table>
</center>
But when I click the "Make a new Account" input, it takes me to the findMyAccount.html instead of accountMaker.html. I'm not sure what's going wrong, I'm still a newbie when it comes to this stuff, so bear with me. Thanks
I observed your code and I think you must remove backslash at the end of input tag,that might solve your problem.
Solution:
do this instead the last line code
<input type="submit" value='Make a new Account'>
instead of
<input type="submit" value='Make a new Account'/>

form doesnt fires submit when i press enter

I have a little question... I dont know whats happening but submit event is working because when I click "ingresar" submit button, it sends all the data.
But when I press enter nothing happens.
I think the problem is that i have the form inside of a table....
Am I wrong? I think the problem is here because,as I said, when i press Ingresar submit button it sends the data.
Thx for your help! :)
<form id="forma-recurso" method="POST">
<table >
<tr>
<td><p>Nombre recurso:</p></td>
<td><input type="text" class="campo nombre-recurso" maxlength="50"></td>
</tr>
<tr>
<td><p>Dirección:</p></td>
<td><input type="text" class="campo direccion-recurso" maxlength="50"></td>
</tr>
<tr>
<td><p>Descripción:</p></td>
<td><input type="text" class="campo descripcion-recurso" maxlength="100"></td>
</tr>
<tr>
<td><p>Tipo de punto de interés:</p></td>
<td><select type="text" class="campo tipo-pto-interes">
<?php
$conexion=pg_connect("host= localhost port=5432 dbname=EmergenciesResponse user=postgres password=asdf");
$registros=pg_query("select tipo_pto, tipo_pto_interes from tipos_ptos_interes where tipo_pto_interes BETWEEN 1 AND 4");
while ($reg=pg_fetch_array($registros))
{
echo "<option value=\"$reg[tipo_pto_interes]\">$reg[tipo_pto]</option><br>";
}
pg_close($conexion);
?>
</select></td>
</tr>
<tr>
<td><p>Latitud/longitud</p></td>
<td><input type="text" class="latlng" disabled></td>
</tr>
<tr>
<td class="salida-btn"><button class="btn-salir">Salir</button></td>
<td class="ingreso-boton"> <input type="submit" class="ingresar-recurso"value="Ingresar"></td>
</tr>
</table>
</form>
Your HTML code seems to be okay, so there must be something in your browser or in the PHP code: the "enter submit" doesn't work when the submit button is not present in the form, otherwise, it should work.
You can see if it is working using inspector tool, press f12 to open it in your browser, change to networking tab, and try to click on submit button if something happens its ok.
I notice that in your form you are not using action form atribute, try it like this example: http://www.w3schools.com/html/html_forms.asp

2 HTML forms within 1 table

I have a single table that is generated dynamically by looping through a result set and creating rows. For each row, I need to include 2 checkboxes. Checkbox A on each row needs to correspond to form A, and checkbox B to form B.
I know this isn't valid, but the below pseudocode is essentially what I want. I know that in HTML5 I can specify which form an input element belongs to, but my users will primarily be using IE8, which, as far as I can tell, doesn't support this feature.
<form name="formA">
<form name="formB">
<table>
<tr>
<th>
<th>
<th>
</tr>
LOOP
<tr>
<td><input type="checkbox" name="chkA" value="1"></td>
<td>Something</td>
<td><input type="checkbox" name="chkB" value="1"></td>
</tr>
END LOOP
</table>
<input type="submit" /> //formA
<input type="submit" /> //formB
</form> //formA
</form> //formB
Any ideas on how I can accomplish this? I suppose one way would be to use a single form and change the action depending on which submit button is clicked, but I want to see if anyone else has any ideas before I do that.
Thanks,
Tom
Probably the best way is to just make it all in one form and act differently later according to the options selected.
For example
<form action="doStuff.php" method="post">
<table>
<tr>
<th></th>
<th></th>
</tr>
<!-- LOOP -->
<tr>
<td><input type="checkbox" name="chkA" value="1"></td>
<td><input type="checkbox" name="chkB" value="1"></td>
</tr>
<!-- END LOOP -->
</table>
<div>
<input type="submit" name="submit" />
</div>
</form>
doStuff.php
if (isset( $_POST["chkA"] ) ){
// Checkbox A is checked.
}
if (isset( $_POST["chkB"] ) ){
// Checkbox B is checked.
}

how to make a checkbox enable and disable a text box in multiple cases

I have done something like this.
<html>
<head>
<script type="text/javascript">
<!--
function toggleTB(what){
if(what.checked){document.theForm.theTB.disabled=1}
else{document.theForm.theTB.disabled=0}}
//-->
</script>
</head>
<body>
<form name="theForm">
<input type="checkbox" name="theCB" onClick="toggleTB(this)">Toggle The Text Box<br>
<input type="text" name="theTB" value="asdf">
</form>
</body>
</html>
But this is only used for one time.i need this function repeatedly in another rows also so how can i used this function for multiple times.
My form goes like this:
<tr>
<td style="border-top:none; text-decoration:underline;" >Specific operations/procedures</td>
<td>
<input type="checkbox" name="sd3[]" value="mfi_nam9" />Other(please specify):
<input type="text" name="mfi_nam9" class="text required" id="mfi_name"
</td>
</tr>
<tr>
<td style="border-top:none; text-decoration:underline;" >General principles/strategies</td>
<td>
<input type="checkbox" name="sd2[]" value="mfi_nam8" />Other(please specify):
<input type="text" name="mfi_nam8" class="text required" id="mfi_name"
</td>
</tr>
i will be waiting for ur response and i am very thankful to u guys for helping me previously and hope u will help me this time too.
Read this article
i would prefer jQuery
Here is DEMO
Another DEMO
We can take the code and do the modifications like
1. Javascript modifications :
function toggleTB(what,elid)
{
if(what.checked)
{
document.getElementById(elid).disabled=1
}
else
{
document.getElementById(elid).disabled=0
}
}
2. Checkbox HTML code modifications
<input type="checkbox" name="sd3[]" value="mfi_nam9" onClick="toggleTB(this,'mfi_name1')" />Other(please specify):
<input type="text" name="mfi_nam9" class="text required" id="mfi_name1" />
Note that we have used the ID's to be varying for each of the textboxes which can be generated even when you are generating these textboxes from the php codes.
Add onclick to each of the checkbox
<input type="checkbox" name="sd2[]" value="mfi_nam8" onClick="toggleTB(this)" />Other(please specify):
and declare toggleTB as
function toggleTB(what){
what.form.elements[what.value].disabled = what.checked;
}
Java Script modification :
function toggleTB(what){
var theTB = document.getElementById(what.value);
if(what.checked){theTB.disabled=1}
else{theTB.disabled=0}
}
HTML Modification :
<table>
<tr>
<td style="border-top:none; text-decoration:underline;" >Specific operations/procedures</td>
<td>
<input type="checkbox" name="sd3[]" onClick="toggleTB(this)" value="mfi_nam9" />Other(please specify):
<input type="text" name="mfi_nam9" id="mfi_nam9" class="text required" />
</td>
</tr>
<tr>
<td style="border-top:none; text-decoration:underline;" >General principles/strategies</td>
<td>
<input type="checkbox" name="sd2[]" onClick="toggleTB(this)" value="mfi_nam8" />Other(please specify):
<input type="text" name="mfi_nam8" id="mfi_nam8" class="text required" />
</td>
</tr>
</table>
Note: Here I have used ID rather than NAME to verify the form input box element.
I think this doesn't make sense to disable the TEXT BOX on checked event of the related CHECK BOX. You maybe want to enable the TEXT BOX whenever some one checked the check box to specify some other thing, I am not sure what you want to do with this.
If you want to do like what I guess, just change the JAVA SCRIPT lines as bellow -
if(what.checked){theTB.disabled=0} // have placed 0 in place of 1
else{theTB.disabled=1} // have placed 1 in place of 0
}
HTML INPUT-BOX as bellow -
OR if you think to toggle (enable/disable) the checkbox, this is not possible cause you know after disable an element the click event will not do action on the element so how it will be disable :)

Auto submitting to cfm

I've created a form that posts to a cfm file. When running a script onLoad that fills in the form values and tries to submit...The site takes me back to the login screen.
<script>
function f()
{
document.getElementById("email").value = "asdf#asdf.com";
document.getElementById("password").value = "asdf";
document.getElementById("form1").submit();
}
</script>
Please help!
update:
Here is the code....When the values for email and password are filled ini manually and the enter button is pressed it will direct me to the home page. However, when writing a script that submits the form onload, it redirects me to the login page.
<form action="https://www.asdf.com/dev/mem/login/login.cfm" method="post" id="caring" name="login" style="margin: 0px; padding: 0px;">
<input name="page" type="hidden" id="page" value="https://www.asdf.com/dev/mem/home/home.cfm">
<table>
<tr>
<td rowspan="2" class="title"></td>
<td class="label"><label for="email"></label></td>
<td class="element"><input name="email" id="email" value ="asdf#asdf.com" /></td>
<script>
//<td rowspan="2" class="action"><input type="submit" name="login" value="submit" alt="go" title="go" /></td>
</script>
</tr>
<tr>
<td class="label"><label for="username"></label></td>
<td class="element"><input name="password" value = "asdf" id="password" /></td>
</tr>
<td rowspan="2" class="action"><input type="submit" name="login" value="enter" alt="go" title="go" /></td>
<tr>
<td></td>
<td colspan="2"> </td>
<td> </td>
</tr>
</table>
</form>
It's hard to tell without the HTML of the form itself, but my guess would be that the action="" attribute of your form is blank. If that attribute is blank, the browser will post the form back to the same page.
Since you're using coldfusion, check to see if there is code generating your action="" value, and look for bugs in it if so. It may help to view the rendered HTML source of the page.
Make sure you aren't using a <cflocation> (or some other redirect) on your destination cfm page that could be redirecting the user back to the form page.
(It would help if you would post your full code)
Make sure to set the action. Here's the code:
<script type="text/javascript">
function f() {
document.getElementById("email").value="asdf#asdf.com";
document.getElementById("password").value="asdf";
document.getElementById('caring').action=document.getElementById("page").value;
document.getElementById('caring').submit();
}
</script>
And of course add the onload attribute to your body tag:
<body onload="f();">
There's a couple of other things to fix in your code. Check it with Total Validator. It's a great tool that will make you a better coder.