So I've had to make a simple phonebook in PHP, but right now it simply echos the text beneath the form, I however, want it to fill it in the form which says: Phonenumber (When you type in the exact name of someone you it echos their phonenumber)
Here's the Form:
<form action="" method="post">
Naam: <br><input type="text" name="name" /><br>
Telefoonnummer: <br><input type="text" name="phonenumber" disabled />
<input type="submit" value="submit" />
</form>
and here's the PHP (I'm Dutch btw so some of the text is in Dutch):
<?php
if(isset($_POST['name'])) {
$formNaam = $_POST['name'];
$naamPersoon = array ("Ilja Clabbers","Piet Paulusma","Gerrit Zalm");
$telefoonNummer = array ("038-4699776","0568-121212","010-2311512");
if(empty($formNaam)) {
echo 'Vul een veld in.';
} else if ($formNaam == $naamPersoon[0]){
echo "Het telefoonnummer van " . $naamPersoon[0] . " is " .$telefoonNummer[0];
} else if ($formNaam == $naamPersoon[1]){
echo "Het telefoonnummer van " . $naamPersoon[1] . " is " .$telefoonNummer[1];
} else if ($formNaam == $naamPersoon[2]){
echo "Het telefoonnummer van " . $naamPersoon[2] . " is " .$telefoonNummer[2];
} else {
echo "Deze naam staat niet in het archief";
}
}
?>
So basically what I'd like to know is; How do you get the phonenumber belonging to a persons name to be shown in the Form where it says 'Telefoonnummer:'?
A neater solution would be to make a single associative array with key => value pairs:
$naamPersoon = array (
"Ilja Clabbers" => "038-4699776",
"Piet Paulusma" => "0568-121212",
"Gerrit Zalm" => "010-2311512",
);
Then your code would be:
$phonenumber = '';
if (array_key_exists($formNaam, $naamPersoon)) {
echo "Het telefoonnummer van " . $formNaam . " is " . $naamPersoon[$formNaam];
$phonenumber = $naamPersoon[$formNaam];
} else {
echo "Deze naam staat niet in het archief";
}
Alternatively you could keep your two arrays as they are and use array_search to find the index of the name in the first array, then use it as the index you check in the second array
$phonenumber = '';
$index = array_search($formNaam, $naamPersoon);
if ($index === false) {
echo "Deze naam staat niet in het archief";
} else {
echo "Het telefoonnummer van " . $formNaam . " is " . $telefoonNummer[$index];
$phonenumber = $telefoonNummer[$index];
}
Either way you can then use the assigned variable $phonenumber to add the number to the form by echoing it out as the input's value. At which point you can take out the echos if you wish.
<form action="" method="post">
Naam: <br><input type="text" name="name" /><br>
Telefoonnummer: <br><input type="text" name="phonenumber" value="<?=htmlspecialchars($phonenumber)?>" disabled />
<input type="submit" value="submit" />
</form>
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to make a html form with php verification but trying to submit the form with at least one filled textfield will say that the emails dont match (self made part of the code that should not be displayed.
<?php
error_reporting(0);
if(isset($_POST["submit"])){
//File Verification
if(empty($_POST['username']) && empty($_POST['password1']) && empty($_POST['password2']) && empty($_POST['email1']) && empty($_POST['email2']) && empty($_POST['bday'])){
echo"Kom op, vul alles in";
echo "<meta http-equiv='refresh' content='5;URL=register.php' />";
exit();
}
else{
$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$pass1 = $_POST['password1'];
$pass2 = $_POST['password2'];
if(email1 == email2){
if(pass1 == pass2){
}
else{
echo "<meta http-equiv='refresh' content='5;URL=register.php' />";
echo"Je wachtwoorden komen niet overeen";
exit();
}
}
else{
echo "<meta http-equiv='refresh' content='5;URL=register.php' />";
echo "Je email gegevens komen niet overeen";
exit();
}
}
}
else{
$form = <<<EOT
<form method="post" action="register.php">
Gebruikersnaam: <input type="text" name="username" placeholder="type hier je gebruikers naam"/><br /><br />
wachtwoord: <input type="password" name="password1" placeholder="type hier je wachtwoord"/><br /><br />
wachtwoord opnieuw: <input type="password" name="password2" placeholder="type je wachtwoord opnieuw in"/><br /><br />
email: <input type="text" name="email1" placeholder="type hier je email"/><br /><br />
email opnieuw: <input type="text" name="email2" placeholder="type hier je email opnieuw"/><br /><br />
Geboorte datum: <input type="date" name="bday"/ placeholder="type je geboorte datum hier"><br /><br />
<input type="submit" name="submit"/>
</form>
EOT;
echo $form;
}
?>
It just shows
else{
echo "<meta http-equiv='refresh' content='5;URL=register.php' />";
echo "Je email gegevens komen niet overeen";
Try changing:
if(email1 == email2){
if(pass1 == pass2){
to:
if($email1 == $email2){
if($pass1 == $pass2){
You've a typo pass1 == pass2.
Also, you may want to change from:
if(empty($_POST['username']) &&...
to
if(empty($_POST['username']) OR...
I'm trying to make a simple form that sends the user input to my email. I don't know PHP so I'm having some trouble here. I can't make the form include the checkboxes' results in the mail. I tried several times but I can't make it work. It's in spanish, sorry for that!
Here is the code:
contactoformescritorio.php:
<?php
$where_form_is = "contacto.html".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));
mail("MY#MAIL.com","Formulario de pedido de copias","Form data:
Nombre: " . $_POST['cd-name'] . "
Email: " . $_POST['cd-email'] . "
Tamanio: " . $_POST['tamanio'] . "
Acabado: " . $_POST['acabado'] . "
Incluir en la cotizacion: " .implode(',',$_POST['agregados'])."\n" . "
Foto elegida e información adicional: " . $_POST['cd-textarea'] . "
.
");
include("confirm.html");
/*
* Procesar el formulario unicamente si el usuario lo envió. En cambio,
* si se accede directamente a esta página, redirigir al formulario.
*/
if ($_SERVER['REQUEST_METHOD'] == "POST") {
function check_input_value($input_value) {
// Remove extra spaces of strings (beginning and end)
$input_value = trim($input_value);
// Prevent XSS
$input_value = htmlspecialchars($input_value);
return $input_value;
}
// Obtenemos los valores que el usuario ingresó
$tamanio = $_POST['tamanio'];
$acabado = $_POST['acabado'];
$tamanio = check_input_value($tamanio);
$acabado = check_input_value($acabado);
if (empty($tamanio) || (empty($acabado)) || (empty($agregados))) {
echo "Error: sin completar";
exit;
}
echo $tamanio . "<br />";
echo $acabado . "<br />";
// Muestra los checkbox seleccionados por el usuario
if (!empty($_POST['agregados'])) {
foreach ($_POST['agregados'] as $agregados) {
echo $agregados . "<br />";
}
}
} else {
header("Location: formulario.php");
}
?>
A friend helped me with the form so it may be a little messy as I copied and pasted some parts.
contacto.html
<form class="cd-form floating-labels" name="htmlform" method="post" action="contactoformescritorio.php">
<fieldset>
<legend>Información personal</legend>
<div class="error-message">
<p>Por favor ingresa un email valido</p>
</div>
<div class="icon">
<label class="cd-label" name="cd-name" for="cd-name">Nombre</label>
<input class="user" type="text" name="cd-name" id="cd-name" required>
</div>
<div class="icon">
<label class="cd-label" name="cd-email" for="cd-email">Email</label>
<input class="email error" type="email" name="cd-email" id="cd-email" required>
</div>
</fieldset>
<fieldset>
<legend>Informacion de la impresion</legend>
<div>
<h4>Tamaño</h4>
<p class="cd-select icon">
<select class="size" name="tamanio" id="cd-size">
<option value="0">Seleccionar Tamaño</option>
<option value="1">Impresion 20x30</option>
<option value="2">Iman de 6 fotos 5x5</option>
<option value="3">Tamaño 3</option>
</select>
</p>
</div>
<div>
<h4>Acabado</h4>
<ul class="cd-form-list">
<li>
<input type="radio" name="acabado" value="mate" id="mate" checked="checked"/><label for="mate">Mate</label>
</li>
<li>
<input type="radio" name="acabado" value="brillo" id="brillo"/><label for="brillo">Brillo</label>
</li>
</ul>
</div>
<div>
<h4>Agregar a la cotización</h4>
<ul class="cd-form-list">
<li><input type="checkbox" name="agregados[]" value="marco" id="marco"><label for="marco">Marco</label></li>
<li><input type="checkbox" name="agregados[]" value="envio" id="envio"><label for="envio">Envio (indicar direccion)</label></li>
</ul>
</div>
<div class="icon">
<label class="cd-label" for="cd-textarea">Foto elegida e información adicional</label>
<textarea class="message" name="cd-textarea" id="cd-textarea" required></textarea>
</div>
<div>
<input type="submit" value="Enviar mensaje">
</div>
</fieldset>
</form>
Your checkboxes are both of the name agregados[], which makes them an array. You currently handle them via implode, then later with a foreach.
Before your mail, run a foreach like this
if (!empty($_POST['agregados'])) {
foreach ($_POST['agregados'] as $key=>$value) {
if ($key > 0) {
$agregados .= ", $value";
} else {
$agregados .= "$value";
}
}
} else {
$agregados .= "(nothing selected)";
}
This will set the variable $agregados with the values that was selected from the checkboxes, and if nothing is selected, display such a message instead.
Then, in your mail, replace the line that handles the checkboxes with the variables.
Replace:
Incluir en la cotizacion: " .implode(',',$_POST['agregados'])."\n" . "
with
$agregados
Also, please know that your mail will be sent regardless when you enter contactoformescritorio.php, as there is no checking if a form has been sent to it. The check you are performing (if ($_SERVER['REQUEST_METHOD'] == "POST")) comes after that.
As a final note, not one that's related to your question directly, your select has no require-attribute, so your PHP script exits if nothing is selected (where you are checking of one of the three statements are empty). Just put a required attribute in your select, such as this
<select class="size" name="tamanio" id="cd-size" required>
Then your Seleccionar Tamaño can be set to a line like this
<option value="" selected style="display:none;">Seleccionar Tamaño</option>
This will make your form more "user-friendly", and not make the user fill in the form all over again if they forget to select something from the dropdown. Just a tip, nothing you really have to do.
I'm using a array to get a range between 1011-2371 but I'didnt need all digits. It's a long list thats why I import the CSV to the database.
I want to create a array from a database table.
But I can't get it.
Here's a rough mockup of the old php:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="text" placeholder="1234AB" name="postcode" />
<input type="submit" value="verstuur" />
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
$postcode = range(1011,2371);
if(preg_match('/^[1-9][0-9]{3} ?[a-zA-Z]{2}$/', $_POST['postcode'])) {
if(in_array($_POST['postcode'],$postcode)) {
echo 'FreshFoods is beschikbaar bij jou in de buurt.';
} else {
echo 'FreshFoods is nog niet beschikbaar bij u in de buurt.';
}
} else {
echo 'Voer 4 cijfers en 2 letters in als postcode. Voorbeeld 1234AB';
}
}
?>
and this is what I try do get the array from the database:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="text" placeholder="1234AB" name="postcode" />
<input type="submit" value="verstuur" />
</form>
<?php
require_once 'db_config.php';
if($_SERVER['REQUEST_METHOD'] == "POST") {
$postcode = array();
$result = mysql_query("SELECT postcode FROM postcode_check");
if(preg_match('/^[1-9][0-9]{3} ?[a-zA-Z]{2}$/', $_POST['postcode'])) {
if(in_array($_POST['postcode'],$postcode)) {
echo 'FreshFoods is beschikbaar bij jou in de buurt.';
} else {
echo 'FreshFoods is nog niet beschikbaar bij u in de buurt.';
}
} else {
echo 'Voer 4 cijfers en 2 letters in als postcode. Voorbeeld 1234AB';
}
}
?>
the db_config.php file looks like:
<?php
$db = array (
'host' => 'localhost',
'user' => 'root',
'pass' => 'root',
'dbname' => 'testzip'
);
if(!mysql_connect($db['host'], $db['user'], $db['pass']))
{
trigger_error('Fout bij verbinden: '.mysql_error());
}
elseif(!mysql_select_db($db['dbname']))
{
trigger_error('Fout bij selecteren database: '.mysql_error());
}
else
{
$sql = "SET SESSION sql_mode = 'ANSI,ONLY_FULL_GROUP_BY'";
if(!mysql_query($sql))
{
trigger_error('MySQL in ANSI niet mogelijk');
}
}
?>
you have to use mysql_fetch_array or mysql_fetch_assoc to fetch the result from database.
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="text" placeholder="1234AB" name="postcode" />
<input type="submit" value="verstuur" />
</form>
<?php
require_once 'db_config.php';
if($_SERVER['REQUEST_METHOD'] == "POST") {
$postcode = array();
$result = mysql_query("SELECT postcode FROM postcode_check");
while ($row = mysql_fetch_array($result)) {
$postcode[] = $row['postcode'];
}
if(preg_match('/^[1-9][0-9]{3} ?[a-zA-Z]{2}$/', $_POST['postcode'])) {
if(in_array($_POST['postcode'],$postcode)) {
echo 'FreshFoods is beschikbaar bij jou in de buurt.';
} else {
echo 'FreshFoods is nog niet beschikbaar bij u in de buurt.';
}
} else {
echo 'Voer 4 cijfers en 2 letters in als postcode. Voorbeeld 1234AB';
}
}
?>
Try this code.
You need to fetch the result. Your regex will never match your sample data from the db though so you will always get the nog niet message. or voorbeeld. Never 'FreshFoods is beschikbaar bij jou in de buurt'
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="text" placeholder="1234AB" name="postcode" />
<input type="submit" value="verstuur" />
</form>
<?php
require_once 'db_config.php';
if($_SERVER['REQUEST_METHOD'] == "POST") {
$postcode = array();
$result = mysql_query("SELECT postcode FROM postcode_check");
while($row = mysql_fetch_assoc($result)){
$postcode[] = $row['postcode'];
}
if(preg_match('/^[1-9][0-9]{3} ?[a-zA-Z]{2}$/', $_POST['postcode'])) {
if(in_array($_POST['postcode'],$postcode)) {
echo 'FreshFoods is beschikbaar bij jou in de buurt.';
} else {
echo 'FreshFoods is nog niet beschikbaar bij u in de buurt.';
}
} else {
echo 'Voer 4 cijfers en 2 letters in als postcode. Voorbeeld 1234AB';
}
}
?>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
if($_SERVER['REQUEST_METHOD'] != 'POST')
{
/*formularen er ikke blevet postet endnu, vis den.
bemærke, at action = "" vil medføre, at formularen til at sende til den samme side det er på */
echo '<form method="post" action="">
<table border="0" >
<tr><td> Dit navn: </td><td> <input type="text" class="ed" name="bruger_navn" />
<tr><td> Din adresse: </td><td> <input type="text" class="ed" name="bruger_adresse" />
<tr><td> Din email: </td><td> <input type="email" class="ed" name="bruger_email" />
<tr><td> Vælg password: </td><td> <input type="password" class="ed" name="bruger_kode"/>
<tr><td> Bekræft password: </td><td> <input type="password" class="ed" name="bruger_kode_check"/>
<br> <tr><td>
<div class="check">
<label for="checkbox">Jeg har læst og er acceptere centrets regler</label>
<input type="checkbox" class="checkbox" name="check" required />
<input type="submit" value="Send" id="button1" />
</form></td><td>';
}
else
{
/* Formularen er blevet postet. Dataen bliver behandlet i tre trin:
1. Tjek dataen
2. Lad brugeren udfylde de tomme felter, hvis det er nødvendigt
3. Gem dataen
*/
$errors = array(); /* erklære arrayet til senere brug */
if(isset($_POST['bruger_navn']))
{
//Brugernavnet findes ctype_alnum betyder at den skal indholder kun tal og bogstaver "Ingen Mellemrum".
if(!ctype_alnum($_POST['bruger_navn']))
{
$errors[] = 'Brugernavnet kan kun indeholde bogstaver og tal.';
}
if(strlen($_POST['bruger_navn']) > 30)
{
$errors[] = 'Brugernavnet må ikke være længere end 30 tegn.';
}
}
else
{
$errors[] = 'Du skal udfylde et brugernavn.';
}
if(isset($_POST['bruger_kode']))
{
if($_POST['bruger_kode'] != $_POST['bruger_kode_check'])
{
$errors[] = 'Adgangskoden matcher ikke.';
}
}
else
{
$errors[] = 'Du skal udfylde en adgangskode.';
}
if(!empty($errors)) /*kontrollere, om der er et tomt array, hvis der er fejl, hvis der er, er de i dette array*/
{
echo 'Du mangler at udfylde nogle felter.<br /><br />';
echo '<ul>';
foreach($errors as $key => $value) /* går gennem arrayet, så alle fejl bliver vist */
{
echo '<li>' . $value . '</li>'; /* Her laves en liste med fejlene */
}
echo '</ul>';
}
else
{
// formularen er nu postet uden fejl, så det bliver gemt
// bemærk brug af mysql_real_escape_string, holder alt sikkert!
// SHA1 funktionen, hasher adgangskoden
$sql = "INSERT INTO
bruger(bruger_navn, bruger_adresse, bruger_email, bruger_kode, fk_roller_id)
VALUES('" . mysql_real_escape_string($_POST['bruger_navn']) . "',
'" . mysql_real_escape_string($_POST['bruger_email']) . "',
'" . mysql_real_escape_string($_POST['bruger_adresse']) . "',
'" . sha1($_POST['bruger_kode']) . "',
NOW(),
1)";
you have a 5 fields in your query and you insert 6 values into it.
INSERT INTO
bruger(bruger_navn, bruger_adresse, bruger_email, bruger_kode, fk_roller_id)
VALUES('" . mysql_real_escape_string($_POST['bruger_navn']) . "',
'" . mysql_real_escape_string($_POST['bruger_adresse']) . "',
'" . mysql_real_escape_string($_POST['bruger_email']) . "',
'" . sha1($_POST['bruger_kode']) . "',
1)
so remove unused value.(according to me you have a no field of time so i remove now() from your query) or if you have a time related field into database then add into your query.
note: i adjust the order of your values.
I'm making a basic lotery script and i'm getting the same error the whole time: Unexpected T_Variable on line 5. Here is my script, I hope someone can help me:
<?php
$invulcijfer = '';
if (isset($_POST['sumbitBtn']))
{
$invulcijfer = $_POST['cijfer'];
$pinda = preg_replace("/[^0-9]/", "", $invulcijfer);
$lotnummer = "1234"; // Hier je 4 cijfers voor lotnummer
if($invulcijfer = '') {
echo "<font color='#FF000'>Je moet alles invullen</font>";
} else if($pinda !== $invulcijfer) {
echo "<font color='#FF000'>Dat zijn geen cijfers</font>";
} else {
if ($pinda == $lotnummer) {
echo "<font color='green'>WAUW! Het is je gelukt!</font>";
} else {
echo "<font color='#FF000'>Sorry, het is niet gelukt..</font>";
// Maybe update query van dat ze - points hebben ofso? q wat jij wilt
}
}
}
}?>
<br><br>
<h3>Loterij Script</h3>
<font color="green">Typ 4 cijfers in en misschien win jij!</font><br><br>
<form action="" method="post">
<input type="text" id="naam" name="naam" maxlength="4"/><br>
<input type="text" id="cijfer" name="cijfer" maxlength="4"/><br>
<input type="submit" id="submitBtn" name="submitBtn" value="Check je lot"/>
</form>
EDIT
I spotted a few errors:
THIS:
if (isset($_POST['sumbitBtn']))
it needs to read as
if (isset($_POST['submitBtn']))
there was a spelling mistake.
Also if($invulcijfer = '') { needs to be if($invulcijfer == '') {
You have one closing brace too many.
Remove the one this one in }?> and your script will work.
This is the code that I ran, deleting the extra closing brace.
EDIT #2 (fixed conditions and spelling mistake for submit button.
<?php
$invulcijfer = '';
if (isset($_POST['submitBtn']))
{
$invulcijfer = $_POST['cijfer'];
$pinda = preg_replace("/[^0-9]/", "", $invulcijfer);
$lotnummer = "1234"; // Hier je 4 cijfers voor lotnummer
if($invulcijfer == '') {
echo "<font color='#FF000'>Je moet alles invullen</font>";
}
elseif ($pinda !== $invulcijfer){
echo "<font color='#FF000'>Dat zijn geen cijfers</font>";
} else {
if ($pinda == $lotnummer) {
echo "<font color='green'>WAUW! Het is je gelukt!</font>";
}
else {
echo "<font color='#FF000'>Sorry, het is niet gelukt..</font>";
// Maybe update query van dat ze - points hebben ofso? q wat jij wilt
}
}
}
?>
<br><br>
<h3>Loterij Script</h3>
<font color="green">Typ 4 cijfers in en misschien win jij!</font><br><br>
<form action="" method="post">
<input type="text" id="naam" name="naam" maxlength="4"/><br>
<input type="text" id="cijfer" name="cijfer" maxlength="4"/><br>
<input type="submit" id="submitBtn" name="submitBtn" value="Check je lot"/>
</form>