Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to post two field values in one variable

Tags:

php

I have two fields

<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname" />

Lastname: <input type="text" name="lastname" />

Age: <input type="text" name="age" />
<input type="submit" />
</form>

how to post name and lastname in one variable meaning in one field of database is it

<?php
    $name=$_post['firstname']['lastname'];
?>
like image 463
Francis D Cunha Avatar asked May 06 '11 09:05

Francis D Cunha


3 Answers

Actually you have three fields. Use string concatenation (or implode):

$name = $_POST['firstname'] . ' ' . $_POST['lastname'];

And don't forget to use mysql_real_escape_string (or what @ThiefMaster says) if you store the values in a database. Never trust user input.

like image 182
Felix Kling Avatar answered Sep 24 '22 23:09

Felix Kling


Just concatenate the two values e.g.

<?php
    $name = $_POST['firstname'] . $_POST['lastname'];
?>
like image 24
planetjones Avatar answered Sep 24 '22 23:09

planetjones


keep an array, and serialize it to store it.

$name['firstname']=$_post['firstname'];

$name['lastname']=$_post['lastname'];

//storage and retrieval methods 
$stored_name = serialize($name);

$name = unserialize($stored_name);

This way you don't lose the functionality of having the variables separate in an array, and you can always concatenate them later for display if you need to.

like image 30
Mild Fuzz Avatar answered Sep 25 '22 23:09

Mild Fuzz