Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Colegas, pesquisei e encontrei um código que adiciona quantos campos eu quiser. Funciona muito bem. Ocorre que que preciso inserir os inputs no campo acompanhhante. Não sei como fazer para o sistema inserir vários acompanhantes. Também não entendi como recuperar o valor do campo para inserir no BD. Preciso de ajuda
<script type="text/javascript">
var qtdeCampos = 0;
function addCampos() {
var objPai = document.getElementById("campoPai");
//Criando o elemento DIV;
var objFilho = document.createElement("div");
//Definindo atributos ao objFilho:
objFilho.setAttribute("id","filho"+qtdeCampos);
//Inserindo o elemento no pai:
objPai.appendChild(objFilho);
//Escrevendo algo no filho recém-criado:
document.getElementById("filho"+qtdeCampos).innerHTML = "<input type='text' id='campo"+qtdeCampos+"' name='acompanhante[]' value='Campo com id: "+qtdeCampos+"'> <input type='button' onClick='removerCampo("+qtdeCampos+")' value='Apagar campo'>";
qtdeCampos++;
}
function removerCampo(id) {
var objPai = document.getElementById("campoPai");
var objFilho = document.getElementById("filho"+id);
//Removendo o DIV com id específico do nó-pai:
var removido = objPai.removeChild(objFilho);
}
</script>
Meu código completo
<?php require_once('../Connections/conn.php'); ?>
<?php
//MX Widgets3 include
require_once('../includes/wdg/WDG.php');
header("Content-Type: text/html; charset=ISO-8859-1",true);
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_conn, $conn);
$query_buscanome = "SELECT * FROM visitante";
$buscanome = mysql_query($query_buscanome, $conn) or die(mysql_error());
$row_buscanome = mysql_fetch_assoc($buscanome);
$totalRows_buscanome = mysql_num_rows($buscanome);
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO visitante (nome, origem, acompanhante, evangelico, data) VALUES (%s, %s, %s, %s, NOW())",
GetSQLValueString($_POST['nome'], "text"),
GetSQLValueString($_POST['origem'], "text"),
GetSQLValueString($_POST['acompanhante'], "text"),
GetSQLValueString($_POST['evangelico'], "text"));
mysql_select_db($database_conn, $conn);
$Result1 = mysql_query($insertSQL, $conn) or die(mysql_error());
if($insertSQL){
$nome = $_POST['nome'];
$insertSQL = "Cadastro de $nome realizado com sucesso!";
} else{
$insertSQL = "Não foi possivel cadastrar os dados";}}
?>
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wdg="http://ns.adobe.com/addt">
<head>
<meta http-equiv="Content-Type" content="text/html; " />
<title>Cadastro de Visitantes</title>
<script src="../SpryAssets/SpryCollapsiblePanel.js" type="text/javascript"></script>
<script src="../SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<script type="text/javascript">
var qtdeCampos = 0;
function addCampos() {
var objPai = document.getElementById("campoPai");
//Criando o elemento DIV;
var objFilho = document.createElement("div");
//Definindo atributos ao objFilho:
objFilho.setAttribute("id","filho"+qtdeCampos);
//Inserindo o elemento no pai:
objPai.appendChild(objFilho);
//Escrevendo algo no filho recém-criado:
document.getElementById("filho"+qtdeCampos).innerHTML = "<input type='text' id='acompanhante"+qtdeCampos+"' name='acompanhante[]' value='Campo com id: "+qtdeCampos+"'> <input type='button' onClick='removerCampo("+qtdeCampos+")' value='Apagar campo'>";
qtdeCampos++;
}
function removerCampo(id) {
var objPai = document.getElementById("campoPai");
var objFilho = document.getElementById("filho"+id);
//Removendo o DIV com id específico do nó-pai:
var removido = objPai.removeChild(objFilho);
}
</script>
<script type="text/javascript">function MM_validateForm() { //v4.0
if (document.getElementById){
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');function MM_displayStatusMsg(msgStr) { //v1.0
window.status=msgStr;
document.MM_returnValue = true;
}</script>
<link href="../SpryAssets/SpryCollapsiblePanel.css" rel="stylesheet" type="text/css" />
<link href="../SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../includes/common/js/sigslot_core.js"></script>
<script src="../includes/common/js/base.js" type="text/javascript"></script>
<script src="../includes/common/js/utility.js" type="text/javascript"></script>
<script type="text/javascript" src="../includes/wdg/classes/MXWidgets.js"></script>
<script type="text/javascript" src="../includes/wdg/classes/MXWidgets.js.php"></script>
<script type="text/javascript" src="../includes/wdg/classes/JSRecordset.js"></script>
<script type="text/javascript" src="../includes/wdg/classes/DynamicInput.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.1.min.js"></script>
<?php$jsObject_buscanome = new WDG_JsRecordset("buscanome");
echo $jsObject_buscanome->getOutput();?>
<link href="../includes/skins/mxkollection3.css" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<?php
if(isset($insertSQL)){
print '<div style="width:100%; background:#ff6600; color:#fff; padding: 5px 0px 5px 0px; text-align:center; margin: 0 auto; onclick="MM_effectAppearFade(this, 500, 100, 0, false)">'.$insertSQL.'</div>';
}
?>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<table align="center">
<tr valign="baseline">
<td nowrap="nowrap" align="right">Nome:</td>
<td colspan="2"><input name="nome" type="text" id="nome" onmouseover="MM_displayStatusMsg('Digite o nome');return document.MM_returnValue" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Origem:</td>
<td colspan="2"><input name="origem" type="text" id="origem" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right" valign="top">Acompanhante:</td>
<td colspan="2"><div id="campoPai"></div>
<input type="button" value="Adicionar campos" onClick="addCampos()"> </td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Evangélico:</td>
<td colspan="2" valign="baseline"><table>
<tr>
<td><input name="evangelico" type="radio" onclick="MM_validateForm('nome','','R','origem','','R','acompanhante','','R');return document.MM_returnValue" value="S" />
Sim</td>
</tr>
<tr>
<td><input type="radio" name="evangelico" value="N" />
Não</td>
</tr>
</table></td>
</tr>
<tr valign="baseline">
<td colspan="3" align="right" nowrap="nowrap"><input type="submit" value="Cadastrar Visitante" />
<label>
<div align="right"></div> </label></td>
</tr>
</table>
<div align="right"><a href="visita.php"><img src="../imagens/Botao-Voltar.gif" alt="Voltar" width="41" height="41" hspace="0" vspace="0" border="0" /></a>
<input type="hidden" name="MM_insert" value="form1" />
</div>
</form>
<div id="CollapsiblePanel1" class="CollapsiblePanel">
<div class="CollapsiblePanelTab" tabindex="0">Procura por Visitante </div>
<div class="CollapsiblePanelContent">
<form action="detalhe.php?id=<?php echo $row_buscanome['idVisitante']; ?>" method="post" name="form2" id="form2">
<span id="sprytextfield1">
<label>
<input name="nomeVisitante" id="nomeVisitante" wdg:recordset="buscanome" wdg:subtype="DynamicSearch" wdg:type="widget" wdg:displayfield="idVisitante" wdg:norec="10" wdg:defaultoptiontext="no" />
</label>
<span class="textfieldRequiredMsg">A value is required.</span></span>
<a href="detalhe.php?nome=<?php echo $row_buscanome['idVisitante']; ?>"><img src="../imagens/lupa.png" width="12" height="15" /></a>
<label></label>
</form>
</div>
</div>
<p> </p>
<script type="text/javascript">
<!--
var CollapsiblePanel1 = new Spry.Widget.CollapsiblePanel("CollapsiblePanel1");
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1");
//-->
</script>
</body>
</html>
<?php
mysql_free_result($buscanome);
?>Carregando comentários...