update_post_meta/add_post_meta de Inputs dinamicos

Asked

Viewed 186 times

1

I have the following problem, I have a shortcode that is a form, and inside this form have inputs that were created dynamically by the programmer himself through a function, but I have no idea how I will take the values of these inputs and save them in the bank? I’ll show you the codes.

shortcode function:

       function register_users_shortcode(){
         global $post;
       ?>
         <div id="pando_cpt_formulario">
           <form id="form" method="POST" ectype="multipart/form-data">
            <p>
                <label for="nome">Nome:</label>
                <br />
                    <input type="nome" name="nome" id="nome" value="<?php  echo get_post_meta( $post->ID, '_nome', true ); ?>" />
            </p>
            <p>
                <label for="email">E-mail:</label>
                <br />
                    <input type="email" name="email" id="email"value="<?php echo get_post_meta( $post->ID, '_email', true ); ?>" />
            </p>
            <p>
                <label for="tel">Telefone:</label>
                <br />
                <input type="text" name="tel" id="tel" value="<?php echo get_post_meta( $post->ID, '_telefone', true ); ?>" />
            </p>
            <p>
                <h2>Tipo de cadastro</h2>
                    Empregador <input type="radio" name="type_user" onclick="document.getElementById('empregador').style.display = 'block'; document.getElementById('trabalhador').style.display = 'none'"value="<?php echo get_post_meta( $post->ID, '_empregador', true ); ?>">
                    Trabalhador <input type="radio" name="type_user" onclick="document.getElementById('empregador').style.display = 'none'; document.getElementById('trabalhador').style.display = 'block'"value="<?php echo get_post_meta( $post->ID, '_Empregador', true ); ?>">
            </p>
            <div id="empregador" style="display:none">
                <h3>Identificação do empregador</h3>
                    <strong>*Tipo de indentificação:</strong><br />
                        <?php echo register_render("tipo_id_empregador", "radio","tipo_id_empregador" ,array("CNPJ","CPF","CEI")); ?><br />
                    <strong>*Número de indentificação:</strong>
                        <?php echo register_render('numeroIdEmpregador','text'); ?>

                <h3>Dados referente ao empregador</a></h3>
                    <strong>*Razão social:</strong> 
                        <?php echo register_render('razaoSocialEmpregador','text'); ?>
                    <strong>*Nome fantasia:</strong>
                        <?php echo register_render('nomeFantasiaEmpregador','text'); ?>

                <h3>Localização da empresa</h3>
                    <strong>*Logradouro:</strong>
                        <?php echo register_render('logradouroEmpregador','text'); ?>
            </div>

            <div id="trabalhador" style="display:none">
                <h3>Indentificação do Trabalhador</h3>
                    <strong>*Número de Indentificação (PIS/PASEP/NIS/NIT)</strong>
                        <?php echo register_render('numeroIdTralhador','text'); ?> <br />
                    <strong>*Nome da mãe do trabalhador</strong>
                        <?php echo register_render('maeTrabalhador','text'); ?>

            </div>
        </form>
    </div>
        <?php }

    add_shortcode('register_users_shortcode','register_users_shortcode');

dynamic input function:

        <?php 


           function register_render($name,$type=null,$id=null,$value=null){
             $types=array("text","email","password","radio");
              if (is_null($type)){
                $type = "text";
         }
              else {
                if(!in_array($type,$types)){
                   exit("type inválido");
              }
     }

         switch ($type) {
           case 'text':
              return "<input type='$type' name='$name' value='".get_post_meta( $post->ID, "_$name", true )."' id='$name'>";
           break;
           case 'email':
              return "<input type='$type' name='$name' value='".get_post_meta( $post->ID, "_$name", true )."' id='$name'>";
           break;
           case 'password':
              return "<input type='$type' name='$name' value='".get_post_meta( $post->ID, "_$name", true )."' id='$name'>"; 
           break;
           case 'radio':
              return register_radio($name,$value);  
           break;
           default: 
              return "error register render - invalid type";
           break;
       }
    }

   function register_radio($name,$var){
         $result = '';
            foreach ($var as $option){
               $result .= "<input type='radio' name='$name' value='".get_post_meta( $post->ID, "_$option", true )."' id='$name'> $option";
            }
          return $result;
   }
  • wouldn’t it be easier to create a custom post type

  • I don’t want to create inputs in the admin area, I want in a page for any user to register.

  • So you want to take the values of the fields generated dynamically ?

  • exactly that Silvio

1 answer

1

If what you are trying to do is pass data from the form to the database you can do it through this code:

<?php
//inclui os dados da database
require_once('config.php');

    //Conecta ao Mysql
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
    die('Falha ao conectar no servidor: ' . mysql_error());
}

//Seleciona a database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
    die("Não foi possível conectar à database");
}

//verifica os valores recebidos do formulário e Prevê SQL injection
function clean($str) {
    $str = @trim($str);
    if(get_magic_quotes_gpc()) {
        $str = stripslashes($str);
    }
    return mysql_real_escape_string($str);
}

//Qualifica a variável para ser postada
$nomedavariavel = clean($_POST['nomedocamponoformulario']);
//Faz a inserção
$qry = "INSERT INTO nomedatable(linhadatable) VALUES('$nomedavariavel')";
$result = @mysql_query($qry);

 //Posta notícia de confirmação
 echo "done=Dados Salvos com Sucesso!";

 //Fecha a conexão
 exit();
 mysql_close();

 ?>

I hope that’s what you’re looking for.

  • How do I do this by WP? Add the meta posts, and then show them... Take a new look at my code, I want to take the values of dynamic inputs and put as key so that when you give update_post_meta take the key and add in the database

Browser other questions tagged

You are not signed in. Login or sign up in order to post.