Francis D Cunha
Francis D Cunha

Reputation: 111

how to post two field values in one variable

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'];
?>

Upvotes: 5

Views: 26996

Answers (9)

Saddam Hussain
Saddam Hussain

Reputation: 13

This One will help You...! I also implemented this and it works...!

Firstname: <input type="text" name="firstName" />

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

$fullname = $_post['firstName']. ' ' .$_post['lastName'];

Upvotes: 1

Roger
Roger

Reputation: 3256

A shortcut way to concatenate variables is this:

$name = "$_POST[first_name] $_POST[last_name]"

Upvotes: 0

frosty
frosty

Reputation: 13

I had this same problem, i have a form with the name of the person reporting the issue and it takes the first name and the last name from my database of users and adds them together, but then when it came time to post both names to the database it would only post the first name. my solution was to first of all call the first name and last name from the database of users, then i called just the first name and last name and concat them together to produce reportername.

so this is the first part of the code calling for the user details i require for the form:

// Select the member from the users table
$sql = "SELECT * FROM users WHERE username='$log_username' AND activated='1' LIMIT 1";
$user_query = mysqli_query($db_conx, $sql);
// Now make sure that user exists in the table
$numrows = mysqli_num_rows($user_query);
if($numrows < 1){
echo "That user does not exist or is not yet activated, press back";
//    exit();   
}
// Fetch the user row from the query above
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
$profile_id = $row["id"];
$first_name = $row["First_Name"];
$last_name = $row["Last_Name"];
$userlevel = $row["userlevel"];


}

Next i Concat the first name and last name:

$reporter_sql = "SELECT CONCAT (First_name,' ', Last_name) AS reportername FROM users WHERE username='$log_username' AND activated='1' LIMIT 1";
$reporter_results = mysqli_query($db_conx, $reporter_sql);
       while ($row = mysqli_fetch_array($reporter_results, MYSQLI_ASSOC)){
       $reportername = $row['reportername'];
       }

then you can post it to your database:

$reportername = mysqli_real_escape_string($db_conx, $reportername);

$sql = "INSERT INTO yourform  (`reportedby`) Value ('$reportername')";

I have striped my code down so it gives you an idea and I'm sure coders with more experience could tell you a simpler way to do this i just know it worked for me.

Upvotes: 0

Dukeatcoding
Dukeatcoding

Reputation: 1393

You can give the text inputs the same name with []

Firstname: <input type="text" name="name[]" />

Lastname: <input type="text" name="name[]" />

then you can

$name = $_POST['name'][0].$_POST['name'][1];

but i would prefer

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

Upvotes: 1

trickwallett
trickwallett

Reputation: 2468

General comment. A good proportion of the world don't have first and last names.

Better practice is just to ask for "Name", and stick it in one field.

If you must split 'em, then "given name" and "family name" are better labels.

Upvotes: -3

Mild Fuzz
Mild Fuzz

Reputation: 30731

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.

Upvotes: 1

planetjones
planetjones

Reputation: 12633

Just concatenate the two values e.g.

<?php
    $name = $_POST['firstname'] . $_POST['lastname'];
?>

Upvotes: 5

Felix Kling
Felix Kling

Reputation: 816770

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.

Upvotes: 7

check123
check123

Reputation: 2009

$name = $firstname . " " . $lastname;

Then post $name in whatever field you want.

Upvotes: 0

Related Questions