Jump to content

Is it possible to split an input field into two fields? See details inside.


Recommended Posts

I would like to split the field specific location on a page into two fields. 

 

( This is how it looks now: http://qwikad.com/?view=post&cityid=269&lang=en&catid=3&subcatid=27&shortcutregion= )

 

I want keep the same MySQL table for that field but make it possible for visitors to enter 2 separate texts.  This is how the code looks right now:

 

<input name="area" type="text" size="40" class="input" maxlength="50" value="<?php echo $data['area']; ?>">

 

I am not strong in PHP programming at all, so I tried to copy and paste that field twice, but it only posts one entry out of the two.

 

Can this be done?

Instead of saying you tried it and it didn't work, how about you post the code you have, so we can see where you went wrong and try to help you?

We aren't standing next to you so we cannot see exactly what you see :)

 

Regards, PaulRyan.

Sure this is what i did:

 

I placed this into the post page:

 

<input name="area[]" type="text" size="40" class="input" maxlength="50" value="<?php echo $data['area']; ?>">
<input name="area[]" type="text" size="40" class="input" maxlength="50" value="<?php echo $data['area']; ?>">
<input name="area[]" type="text" size="40" class="input" maxlength="50" value="<?php echo $data['area']; ?>">

 

And then I tried to pull the data out with this (for simplicity purposes):

 

<?
echo($_POST['area'][0]);
echo($_POST['area'][1]);
echo($_POST['area'][2]);
?>

 

It showed Array Array Array.

 

The original code that pulls the data out is actually this:

 

<?php 
$loc = "";
if($ad['area']) $loc = $ad['area'];
if($xcityid < 0) $loc .= ($loc ? ", " : "") . $ad['cityname'];
if($loc) echo " <span class=\"adarea\">($loc)</span>";
?>

 

When I tried to add the array to it it still showed Array Array Array.

 

Can you please modify this code to pull all three entries the right way?

 

Thanks for your help!

On the page that displays the form, does $data['area'] contain an array? What do you get if you display the variable before the input field?

 

var_dump($data['area']);
<input name="area[]" type="text" size="40" class="input" maxlength="50" value="<?php echo $data['area']; ?>">

Let's back up a second. What do you mean you want to "split" the filed into two. Are you wanting the user to enter a single record into two fields (say the domain part of the url into one field and the rest into another) or are you wanting the user to be able to enter several different 'area" records?

 

I think you want the latter. The correct way to handle that would be to create a new table to store the 'areas' for the users. But, that would require modifications of any code that works with users and the area data. There is an easy fix, but if you have any code that does searches or lookups on the area values it may not work.

 

Anyway, the quick fix (not advised) is to create as many area fields as you wish - named as arrays lie you did above. Then in the code that processes the form data you should see something that references the original area POST field such as

$area = $_POST['area'];

 

You would change that to concatenate all the posted areas with a comma separator

//Trim posted areas and remove empty values
$areasAry = array_filter(array_map('trim', $_POST['area']));
//Convert multiple values into comma separated string
$area = implode(', ', $areasAry); //Use this value in the insert query

Psycho,

 

This is the code that calls that text area out:

 

<?php 
$loc = "";
if($ad['area']) $loc = $ad['area'];
if($loc) echo " <span class=\"adarea\">($loc)</span>";
?>

 

How would you modify it to call out let's say 3 input fields?

 

Also, when are saying to create as many arrays as I wish do you mean something like?:

 

<input name="area[]" type="text" size="40" class="input" maxlength="50" value="<?php echo $data['area']; ?>">
<input name="area[]" type="text" size="40" class="input" maxlength="50" value="<?php echo $data['area']; ?>">
<input name="area[]" type="text" size="40" class="input" maxlength="50" value="<?php echo $data['area']; ?>">

 

Thanks!

 

 

 

 

 

 

 

 

Again, this is not the preferred method of handling multiple values to be associated with a single record, but I'm not going to get into rewriting your application.

 

$loc = "";
if(isset($ad['area']))
{
    //Process areas array to remove empty values
    $areasAry = array_filter(array_map('trim', ad['area']));
    //Convert multiple values into comma separated string
    $loc = implode(', ', $areasAry);
}
if($loc != '') echo " <span class=\"adarea\">($loc)</span>";

One more thing, do I simply repeat the same input field like so?:

 

<input name="area" type="text" size="40" class="input" maxlength="50" value="<?php echo $data['area']; ?>">
<input name="area" type="text" size="40" class="input" maxlength="50" value="<?php echo $data['area']; ?>">
<input name="area" type="text" size="40" class="input" maxlength="50" value="<?php echo $data['area']; ?>">

 

Or do I modify it as well?

 

 

The name of the input must be an array.

INCORRECT:
<input name="area" type="text" size="40" class="input" maxlength="50" value="<?php echo $data['area']; ?>">
<input name="area" type="text" size="40" class="input" maxlength="50" value="<?php echo $data['area']; ?>">
<input name="area" type="text" size="40" class="input" maxlength="50" value="<?php echo $data['area']; ?>">
CORRECT:
<input name="area[]" type="text" size="40" class="input" maxlength="50" value="<?php echo $data['area'][0]; ?>">
<input name="area[]" type="text" size="40" class="input" maxlength="50" value="<?php echo $data['area'][1]; ?>">
<input name="area[]" type="text" size="40" class="input" maxlength="50" value="<?php echo $data['area'][2]; ?>">

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.