Jump to content

Juntar código postal de duas textbox


Go to solution Solved by pikax,

Recommended Posts

Posted (edited)

Como se faz uma junção de duas textboxs?

por exemplo para o codigo postal, como se junta as duas textboxs numa variavel?

eu c# isto ficaria mais ou menos assim :

string cpostal = txtcpostal1.text + '-' + txtcpostal2.text;

mas aqui em php ou html nao sei :/ alguem me pode ajudar?

Edited by yoda
Posted

Dá-me este erro :

Variable $cpostal seems to be unused in its scope

o meu código postal esta assim :

<tr>
   <td>Codigo Postal</td>
   <td align="left"><input id="input1" type='int' name='cpostal1' maxlength="4" /><label for='cpost' >-</label><input id="input1" type='int' name='cpostal2' maxlength="4" /></td>
</tr>

e no php está assim:

$cpostal = $_POST['cpostal1'] . "-" . $_POST['cpostal2'];
Posted

Tenta fazer assim

<tr>
<td>Codigo Postal</td>
<td align="left">
<input id="input1" type='int' name='cpostal1'  maxlength="4" />-<input id="input1" type='int' name='cpostal2'  maxlength="4" />
</td>
</tr>

$postal1 = $_POST['postal1'];
$postal2 = $_POST['postal2'];


$cpostal = $postal1.'-'.$postal2;

You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.

Posted

Uma questão.

Como validas o que os utilizadores inserem ?

O código postal que estás á espera pode não ser o que colocam.

You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.

Posted

Tenta fazer assim

<tr>
<td>Codigo Postal</td>
<td align="left">
<input id="input1" type='int' name='cpostal1'  maxlength="4" />-<input id="input1" type='int' name='cpostal2'  maxlength="4" />
</td>
</tr>

$postal1 = $_POST['postal1'];
$postal2 = $_POST['postal2'];


$cpostal = $postal1.'-'.$postal2;

Meti igualzinho ao teu e ja grava, mas qualquer que seja o registo so grava 1-1, nao entendo nao grava o codigo postal que nos introduzimos, introduz sempre 1-1 :/

que se passa?

Posted
<form name="input" method="post">
 <div id="formularios">
	 <div id="fornecedores">
		 <table id="table">
			 <tr>
				 <td>Nome da Empresa</td>
					 <td><input id="input" type='text' name='nome_empresa' maxlength="50" />*</td>
			 </tr>
			 <tr>
				 <td>Morada 1</td>
				 <td><input id="input" type='text' name='moradal1' maxlength="50" /></td>
			 </tr>
			 <tr>
				 <td>Morada 2</td>
				 <td><input id="input" type='text' name='moradal2' maxlength="50" /></td>
			 </tr>
			 <tr>
				 <td>Codigo Postal</td>
				 <td align="left">
				 <input id="input1" type='int' name='cpostal1' maxlength="4" />-<input id="input1" type='int' name='cpostal2' maxlength="3" />
				 </td>
			 </tr>
			 <tr>
				 <td>Localidade</td>
				 <td><input id="input" type='text' name='localidade' maxlength="50" /></td>
			 </tr>
			 <tr>
				 <td>Pais</td>
				 <td><input id="input" type='text' name='pais' maxlength="50" /></td>
			 </tr>
			 <tr>
				 <td>Telefone</td>
				 <td><input id="input" type='int' name='Telefone' maxlength="14" /></td>
			 </tr>
			 <tr>
				 <td>NIF</td>
				 <td><input id="input" type='int' name='nif' maxlength="9" />*</td>
			 </tr>
			 <tr>
				 <td>Email</td>
				 <td><input id="input" type='text' name='Email' maxlength="50" /></td>
			 </tr>
			 <tr>
				 <td>WebPage</td>
				 <td><input id="input" type='text' name='webpage' maxlength="50" /></td>
			 </tr>

			 <tr>
				 <td>Campos Obrigatórios *</td>
			 </tr>


		 </table>
	 </div>
	 <div id="botoes">
		 <input type='submit' name='Submit' value='Registar Fornecedor' />
			 <input type='submit' name='Submit' value='Apagar' />
	 </div>
 </div>
</form>






 <?php
 $cpostal1 = isset($_POST['cpostal1']);
 $cpostal2 = isset($_POST['cpostal2']);
 $cpostal = $cpostal1.'-'.$cpostal2;
		 if(isset($_POST['nome_empresa']) && isset($_POST['nif']))
			 {
				 $con=mysqli_connect("127.0.0.1","root","","gesinf");
				 // Check connection
				 if (mysqli_connect_errno())
					 {


					 }

				 $sql="INSERT INTO fornecedor (NomeEmpresa, MoradaL1, MoradaL2, CPostal, Localidade, Pais, Telefone, NIF, Email, WebPage)VALUES('$_POST[nome_empresa]','$_POST[moradal1]','$_POST[moradal2]','$cpostal','$_POST[localidade]','$_POST[pais]','$_POST[Telefone]','$_POST[nif]','$_POST[Email]','$_POST[webpage]')";
				 if (!mysqli_query($con,$sql))
					 {
						 die('Error: ' . mysqli_error($con));

					 }
					 echo "Registado com sucesso!";
					 mysqli_close($con);
			 }
	 ?>
Posted

Nao coloques int

<input id="input1" type='int' name='cpostal1' maxlength="4" />-<input id="input1" type='int' name='cpostal2' maxlength="3" />

coloca text e faz a validação com preg_match

<input id="input1" type='text' name='cpostal1' maxlength="4" />-<input id="input1" type='text' name='cpostal2' maxlength="3" />

You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.

  • Solution
Posted

Problema esta' aqui:

$cpostal1 = isset($_POST['cpostal1']); //estas a atribuir o valor de verdadeiro!!!
//$Cpostal1 == a true se existir cpostal1 ou falso se nao existir!
$cpostal2 = isset($_POST['cpostal2']);
//$Cpostal2 == a true se existir cpostal1 ou falso se nao existir!
$cpostal = $cpostal1.'-'.$cpostal2;

Correcto:

$cpostal1 = isset($_POST['cpostal1'])?$_POST['cpostal1']:"";
$cpostal2 = isset($_POST['cpostal2'])?$_POST['cpostal2']:"";
$cpostal = $cpostal1.'-'.$cpostal2;
  • Vote 1

Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender.

A beleza de um código está em decompor problemas complexos em pequenos blocos simples.

"learn how to do it manually first, then use the wizzy tool to save time."

"Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast."

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. 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.