Jump to content

[SOLVED] Space in ['This Name'] stops it working


Recommended Posts

Sorry, couldn't think of a more appropriate subject.

 

I'm trying to teach myself to write a simple form which submits the data to itself for validation, and then emails the confirmed entries to an email address, before forwarding on to a URL... I can do these things separately by copy and pasting code, but as I really want to understand how it works, I'm trying to write the code and make a combination of all those things... Ok, far too much back story already, I just wanted you to know I wasn't just asking all the time, I'm actually trying to learn for myself...

 

So, I'm sure this is embarrassingly simple. I have my html form with text boxes mainly, "First Name" "Last Name" etc... This posts to my php file and the php file then has:

 $first_name = $_REQUEST['First Name'] ;

 

etc...

 

I then have echo to display all that... However it appears that it won't do this with ['First Name'], as it doesn't like the space between the words... I didn't think that the space mattered as long as it was the same name as the input box on the html form. If I change both to be ['First'] then it works. Why is this? I understand that you can't have spaces in a $variable, but I thought I could have spaces in ['this bit'] What is that called by the way? I'm still learning terminology too.

 

Anyway, thank you in anticipation. It's a steep learning curve, but I'm enjoying the climb!

 

 

How have you coded your form? Make sure you wrap quotes around the name attribute for the input tag, eg don't so this:

<input type=text name=first name>

MAke sure you do this:

<input type="text" name="first name">

 

if you don't use quotes around values for attributes your browser will ignore the spaces and use treat each individual word as an attribute and not a value.

Hi thanks, yes I have. My form looks like this

<form action="form_handler.php" method="post">
   First Name: <input name="First Name" type="text" /><br>
   Last Name: <input name="Last Name" type="text" /><br>
   <input type="submit" />
</form>

 

 

I'm not completely sure on where you can and can't use spaces, but I'm sure they aren't allowed in many places. Even if you can put a space there, it wouldn't be the common practice of doing things.

 

Usually you just use an underscore to represent a space (_). So instead of "First Name" do "first_name".

Ok, cool, if that's the answer I'm fine with that... I'm trying to graduate away from FormMail, which did allow the spaces in input names. I can live with underscoring stuff instead, but I just wanted to check I wasn't doing anything wrong.

 

Thanks!

Ooh, shoot! Hold on. the name attribute can't have spaces. You should use underscores (_) in replacement. Then use $_POST['first_name'] to get the value for that field.

 

Sorry about my last post, thinking about the wrong thing

Ok, cool thanks... So that means I will need to underscore in the html form names too, adding the underscore in the php file won't just translate that?  Easy enough to do. Thank you all. Like I said, I'm learning, so it's good to learn this basics first!!

Actually adding the underscore in the attribute does appear to translate the non-underscored name from the input box on the html form.

 

So,

<input type="text" name="First Name">

 

Can be called with:

$first_name = $_REQUEST['First_Name'] ;

 

Is this a safe practice? Or should I underscore both?

 

Thanks

 

 

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.