dongzhan3937 2014-04-19 05:08
浏览 108
已采纳

发布包含可选和必填字段的表单

How can I add to my database information from a form if I have mandatory and optional fields? So far I have only done a few inserts but in all of those all the information was always present. In this case I am making a contact form where the name, surname and cellphone number are mandatory and where all the others are optional.

Also the person's information (name, surnames, nickname) goes into a table while the numbers (home, work, fax, cell etc) goes into another table.

A example (of another app) code I have used for POST:

jQuery (Ajax)

$("#btnAceptar").click(function() {
  var codigolab = $('#txtNumLab').val(),
  capacidad = $('#txtCapacidad').val(),
  carrera = $('#txtCarrera').val(),
  ubicacion = $('#txtUbicacion').val();


var request = $.ajax({
url: "includes/functionsLabs.php",
type: "post",
data: {

    'call': 'addLab',
    'pCodigoLab':codigolab,
    'pCapacidad':capacidad,
    'pCarrera':carrera,
    'pUbicacion':ubicacion},

    dataType: 'html',

   success: function(response){
    $('#info').html(response);


     }
  });
});

PHP I use in this example to add to the database

function addLab(){     
if(isset($_POST['pCodigoLab']) && 
    isset($_POST['pCapacidad']) &&
    isset($_POST['pCarrera']) &&
    isset ($_POST['pUbicacion'])){

    $codigolab = $_POST['pCodigoLab'];
    $capacidad = $_POST['pCapacidad'];
    $carrera = $_POST['pCarrera'];
    $ubicacion = $_POST['pUbicacion'];


    $query = "INSERT INTO labs VALUES" . "('null', '$codigolab', '$capacidad', '$carrera','$ubicacion', '1')";

    $result = do_query($query);

    echo "<p>¡El laboratorio ha sido añadido exitosamente!</p>";


}

}

In this case where I am making some fields mandatory and others not I need to insert into different tables, to add another insert into that query do I just use a AND or something like that?

Also if the user doesn't input anything in some fields do I leave the optional fields out of the isset statements? How would those variables be declared if I cannot be certain if they're going to be used?

Sorry if I am making this a bit hard to follow I am a bit confused and english is not my main language.

Thanks in advance.

FIDDLE OF the actual code:

Pressing Guardar button displays the fields that are mandatory the others are optional.

Thanks

  • 写回答

2条回答 默认 最新

  • dongta1824 2014-04-19 05:34
    关注

    Let's say I have the fields name, email, note where the first 2 are required and the last one is not.

    Here is one way to do it:

    <?php
    $error = array();
    if (!isset($_POST['name']))
    {
        $error[] = "The field name was not filled!";
    }
    
    if (!isset($_POST['email']))
    {
        $error[] = "The field email was not filled!";
    }
    
    if (sizeof($error) == 0)
    {
        // insert the data
        $query = sprintf("INSERT INTO users VALUES ('%s', '%s', '%s')",
                         mysql_real_escape_string($_POST['name']),
                         mysql_real_escape_string($_POST['email']),
                         mysql_real_escape_string((isset($_POST['note']) ? $_POST['note'] : '')));
    
        $result = do_query($query);
        echo "<p>¡El laboratorio ha sido añadido exitosamente!</p>";
    }
    else
    {
        // print the error
        foreach ($error as $msg)
        {
            echo $msg, "<br>
    ";
        }
    }
    

    For note I used a ternary operator:

    isset($_POST['note']) ? $_POST['note'] : ''
    

    Which essentially is a one liner if/else, which could have been written as:

    if (isset($_POST['note']))
    {
        $note = $_POST['note'];
    }
    else
    {
        $note = '';
    }
    

    Also make sure you sanitize your data in your case using mysql_real_escape_string to prevent SQL Injection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?