0

I have a simple form in HTML that receives the values for a person's last name and first name. Then I use a PHP file that submits this form into a MySQL database.

I want to make a third variable called 'name' that consists of 'firstname' and 'lastname' together, and I want this 'name' to be submitted to the database.

However, I also have to display 'name' as the user types his last name and first name. For this, I'm using the 'oninput' and 'output' tag combinations. This is a shortened version of my HTML file:

<form 
 action="file.php" 
 method="post" 
 oninput="name.value=lastname.value + space.value + firstname.value">

 <input type="text" id="space" value=" ">

 <output id="name" name="name">
 <input type="text" class="text" name="lastname">
 <input type="text" class="text" name="firstname">

 <input type="submit" id="submit" name="submit">
</form>

And this is what I'm doing in the PHP file:

$name= $_POST['name'];
$sql = "INSERT INTO table1 (name) VALUES ('$name')";

I think the problem is that I can't simply submit the value of an output? Because this works fine if I want to submit 'lastname' or 'firstname' which are both inputs.

Please help!

PS: Is there an easier way to include a space on the 'oninput' part?

1 Answer 1

1

Why don't you simply concatenate them after receiving?

HTML

<form  action="file.php" method="post"> 
<input type="text" class="text" name="lastname">
<input type="text" class="text" name="firstname">
<input type="submit" id="submit" name="submit">
</form>

PHP

$name1  =  $_POST['firstname'];
$name2  =  $_POST['lastname'];

$name   =  $name1 . " " . $name2;

$sql    =  "INSERT INTO table1 (name) VALUES ('$name')";
Sign up to request clarification or add additional context in comments.

2 Comments

Because I have no knowledge of PHP whatsoever, clearly. Thank you so much!
@naranjja that's okay, we have all been there. Please accept my answer if it has helped you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.