Jump to content

Create a input box that saves data into DB


pemoamsi

Recommended Posts

I have a input box, a check box, a add button and a submit.

 

When I click in add I am creating a new input and a new checkbox,.

 

The thing i'm having troubles with is, how to insert into database the values that are only associated with the checkbox's checked.

 

I was tring with jQuery only, but I think I need ajaz, and I not familiar with this language, please help me out :D

 

<script type="text/javascript">
        $(document).ready(function() {
            $('#btnAdd').click(function() {
                var num     = $('.clonedInput').length;
                var newNum  = new Number(num + 1);   
                var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);
                newElem.children(':first').attr('id', 'name_process' + num).attr('name_process', 'name_process' + num);
                newElem.children(':first').attr('id', 'proc_check_' + num).attr('proc_check_', 'proc_check_' + num);
       
                $('#input' + num).after(newElem);
            });  
    </script>
<form id="myForm">
    <div id="input" style="margin-bottom:4px;" class="clonedInput">
        Processo: <input type="text" name="name_process1" id="name_process1" />
        <input type="checkbox" name="proc_check_1" id="proc_check_1" checked="" />
  	<input type="submit" value="Submit" />
    </div>
</form>
<?php 
while (name_process/*something here*/ != NULL ) {
      if (proc_check_/*something here*/ ! = NULL) {
      $process = $_POST(name_process);
      $sql = "INSERT INTO process_table (name_process) VALUES ('" . $processo . "');
      }
}

 

 

Link to comment
Share on other sites

<script type="text/javascript">
        $(document).ready(function() {
            $('#btnAdd').click(function() {
                var num     = $('.clonedInput').length;
                var newNum  = new Number(num + 1);   
                var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);
                newElem.children(':first').attr('id', 'name_process' + num).attr('name_process', 'name_process' + num);
                newElem.children(':first').attr('id', 'proc_check_' + num).attr('proc_check_', 'proc_check_' + num);
       
                $('#input' + num).after(newElem);
            });  
         }
    </script>
<form id="myForm">
    <div id="input" style="margin-bottom:4px;" class="clonedInput">
        Processo: <input type="text" name="name_process1" id="name_process1" />
        <input type="checkbox" name="proc_check_1" id="proc_check_1" checked="" />
  	<input type="submit" value="Submit" />
    </div>
</form>
<?php 
include ("autentication.php");
while (name_process/*something here*/ != NULL ) {
      if (proc_check_/*something here*/ ! = NULL) {
      $process = $_POST(name_process);
      $sql = "INSERT INTO process_table (name_process) VALUES ('" . $processo . "');
      $result = pg_query($conn, $sql);
      }
}

 

Yes, I do know PHP, I just don't know how to retrieve this values from java(jquery)

 

These errors you noticed are erros because I changed the code to post here, the original goes like theses

 

<?php
/*
session_start();
define(ADMIN,$_SESSION['name']); 
if(!session_is_registered("admin")){
header("location:index.php?do=login");
}
*/
?>


<!DOCTYPE HTML>
<html>
<head>

    <title></title>
    <script src="jquery.min.js"></script>

    <script type="text/javascript">
   
   //
   //
   //Apenas fuciona com a primeira função Add, todos os outros não adicionam linhas
   //It only works on the first add funcion, it doesn't allow me to add more 
   //
   
        $(document).ready(function() {
            $('#btnAdd').click(function() {
                var num     = $('.clonedInput').length;
                var newNum  = new Number(num + 1);   
                var newElem = $('#input' + num).clone().attr('id', 'input1' + newNum);
                newElem.children(':first').attr('id', 'nome_processo' + num).attr('nome_processo', 'nome_processo' + num);
                newElem.children(':first').attr('id', 'proc_check_' + num).attr('proc_check_', 'proc_check_' + num);
       
                $('#input' + num).after(newElem);
            });        

            $('#btnAdd2').click(function() {
                var num2     = $('.clonedInput2').length;
                var newNum2  = new Number(num2 + 1);   
                var newElem2 = $('#input' + num2).clone().attr('id', 'input2' + newNum2);
                newElem2.children(':first').attr('id', 'nome_documento' + num2).attr('nome_documento', 'nome_documento' + num2);
       			newElem2.children(':first').attr('id', 'doc_check_' + num2).attr('doc_check_', 'doc_check_' + num2);
       
                $('#input' + num2).after(newElem2);
            });

            $('#btnAdd3').click(function() {
                var num3     = $('.clonedInput3').length;
                var newNum3  = new Number(num3 + 1);   
                var newElem3 = $('#input' + num3).clone().attr('id', 'input3' + newNum3);
                newElem3.children(':first').attr('id', 'nome_ficheiro' + num3).attr('nome_ficheiro', 'nome_ficheiro' + num3);
       			newElem3.children(':first').attr('id', 'fic_check_' + num3).attr('fic_check_', 'fic_check_' + num3);
       
                $('#input' + num3).after(newElem3);
            });

            $('#btnAdd4').click(function() {
                var num4     = $('.clonedInput4').length;
                var newNum4  = new Number(num4 + 1);   
                var newElem4 = $('#input' + num4).clone().attr('id', 'input' + newNum4);
                newElem4.children(':first').attr('id', 'nome_anexo' + num4).attr('nome_anexo', 'nome_anexo' + num4);
       			newElem4.children(':first').attr('id', 'ane_check_' + num4).attr('ane_check_', 'ane_check_' + num4);
       
                $('#input' + num4).after(newElem4);
            });
        });
    </script>
</head>

<body>

<form id="myForm">
    <div id="input1" style="margin-bottom:4px;" class="clonedInput">
        Processo: <input type="text" name="nome_processo1" id="nome_processo1" />
        <input type="checkbox" name="proc_check_1" id="proc_check_1" checked="" />
    </div>
    <div>
        <input type="button" id="btnAdd" value="add" />
    </div>
    <div>
	Tipo de documento: <br /><input type="text" name="nome_documento1" id="nome_documento1" size="33"/>
							<input type="checkbox" name="doc_check_1" value="doc_check_1" checked=""/>
    </div>
    <div>
    	<input type="button" id="btnAdd2" value="add" /><br /> 
    </div>
    <div>
<input type="checkbox" name="processo" value="processo" /> Processo<br />
<input type="checkbox" name="formulario" value="formulario" /> Formulário<br />
<input type="checkbox" name="relatorio" value="relatorio" /> Relatório<br />
<input type="checkbox" name="query" value="query" /> Query<br />
    </div>
    <div>
Nome do ficheiro: <br /><input type="text" name="nome_ficheiro1" id="nome_ficheiro1" size="33"/>
						<input type="checkbox" name="fic_check_1" value="fic_check_1" checked=""/> 
    </div>
    <div>
    	<input type="button" id="btnAdd3" value="add" /><br /> 
    </div>
    <div>
Descrição do Update: <br /><textarea rows="2" cols="25" name="descricao"/></textarea> <br />
    </div>
    <div>
Texto para Email: <br /><textarea rows="3" cols="25" name="texto"></textarea> <br />
    </div>
    <div>
Anexos: <br /><input type="text" name="anexo1" id="anexo1" size="33"/>
						<input type="checkbox" name="ane_check_1" value="ane_check_1" checked=""/>
    </div>
    <div>
    	<input type="button" id="btnAdd4" value="add" /><br /> 
    </div>
    <div>
  		<input type="submit" value="Submit" />
    </div>
</form>

</body>
</html>


<?php 
if (isset($_POST['Submit'])) {

include ("autentication.php");


if (isset($_POST['processo'])) {$processo = 1;}
else {$processo = 0;}
if (isset($_POST['formulario'])) {$formulario = 1;}
else {$formulario = 0;}
if (isset($_POST['relatorio'])) {$relatorio = 1;}
else {$relatorio = 0;}
if (isset($_POST['query'])) {$query = 1;}
else {$query = 0;}


$descricao = $_POST['descricao'];
$texto = $_POST['texto'];

if ($conn)
{
// Espero que esta funcão esteja a funcionar para retornar o id da linha inserida
//This function is getting me the id when I insert into database
$sql = "INSERT INTO updates (processo, formulario, relatorio, query, descricao, texto) 
	VALUES ('" . $processo . "', '" . $formulario . "', '" . $relatorio . "', '" . $query . "', '" . $descricao . "', '" . $texto . "') RETURNING id";
    $result = pg_query($conn, $sql);
	$id_update = pg_fetch_result($result, 0, 'id');


//
//
//Faltam os ciclos (while presumo) para que ele insira todos os dados na base de dados de forma correcta
//The cicles which I need to create to save the data into BD
//Tem ainda de verificar dentro do ciclo se a checkbox está activada, se não estiver, não adiciona
//It has to verify it the related checkbox is checked
//

$nome_processo = $_POST['nome_processo'];
$sql2 = "INSERT INTO processos (id_update, nome_processo) VALUES ('" . $id_update . "', '" . $nome_processo . "')"	; 
    pg_query($conn, $sql2);


$nome_documento = $_POST['nome_documento'];
$sql3 = "INSERT INTO documentos (id_update, nome_documento) VALUES ('" . $id_update . "', '" . $nome_documento . "')"	; 
    pg_query($conn, $sql3);

$nome_ficheiro = $_POST['nome_ficheiro'];
$sql4 = "INSERT INTO ficheiros (id_update, nome_ficheiro) VALUES ('" . $id_update . "', '" . $nome_ficheiro . "')"	; 
    pg_query($conn, $sql4);

$nome_anexo = $_POST['nome_anexo'];
$sql5 = "INSERT INTO anexos (id_update, nome_anexo) VALUES ('" . $id_update . "', '" . $nome_anexo . "')"	; 
    pg_query($conn, $sql5);
pg_close($conn);
}
}
?>

Link to comment
Share on other sites

Javascript.

 

Are you asking how to use AJAX? since you're using jQuery you should be able to look up their documentation on their AJAX functionality and use that. I skimmed your full code but don't see anywhere you attempt to use jQuery's AJAX stuff.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.