qwikad Posted March 5, 2012 Share Posted March 5, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/258282-is-it-possible-to-split-an-input-field-into-two-fields-see-details-inside/ Share on other sites More sharing options...
blacknight Posted March 5, 2012 Share Posted March 5, 2012 you could make 2 fields and name them the same adding the [] at the end of the name and make it an array to be handled by your post script... that should work if im correct Quote Link to comment https://forums.phpfreaks.com/topic/258282-is-it-possible-to-split-an-input-field-into-two-fields-see-details-inside/#findComment-1324027 Share on other sites More sharing options...
qwikad Posted March 5, 2012 Author Share Posted March 5, 2012 I just tried it. All it shows is Array Array Array (I used 3 fields). Can you post an exact code here how this can be done? Quote Link to comment https://forums.phpfreaks.com/topic/258282-is-it-possible-to-split-an-input-field-into-two-fields-see-details-inside/#findComment-1324094 Share on other sites More sharing options...
PaulRyan Posted March 5, 2012 Share Posted March 5, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258282-is-it-possible-to-split-an-input-field-into-two-fields-see-details-inside/#findComment-1324095 Share on other sites More sharing options...
qwikad Posted March 5, 2012 Author Share Posted March 5, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/258282-is-it-possible-to-split-an-input-field-into-two-fields-see-details-inside/#findComment-1324098 Share on other sites More sharing options...
cyberRobot Posted March 5, 2012 Share Posted March 5, 2012 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']; ?>"> Quote Link to comment https://forums.phpfreaks.com/topic/258282-is-it-possible-to-split-an-input-field-into-two-fields-see-details-inside/#findComment-1324106 Share on other sites More sharing options...
Psycho Posted March 5, 2012 Share Posted March 5, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/258282-is-it-possible-to-split-an-input-field-into-two-fields-see-details-inside/#findComment-1324166 Share on other sites More sharing options...
qwikad Posted March 5, 2012 Author Share Posted March 5, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/258282-is-it-possible-to-split-an-input-field-into-two-fields-see-details-inside/#findComment-1324176 Share on other sites More sharing options...
Psycho Posted March 5, 2012 Share Posted March 5, 2012 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/258282-is-it-possible-to-split-an-input-field-into-two-fields-see-details-inside/#findComment-1324180 Share on other sites More sharing options...
qwikad Posted March 5, 2012 Author Share Posted March 5, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/258282-is-it-possible-to-split-an-input-field-into-two-fields-see-details-inside/#findComment-1324184 Share on other sites More sharing options...
Psycho Posted March 5, 2012 Share Posted March 5, 2012 You can create as many input fields as you want using the same array name for each. however, you need to make sure the field in the database can support however much data the user can submit. Quote Link to comment https://forums.phpfreaks.com/topic/258282-is-it-possible-to-split-an-input-field-into-two-fields-see-details-inside/#findComment-1324205 Share on other sites More sharing options...
jcbones Posted March 5, 2012 Share Posted March 5, 2012 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]; ?>"> Quote Link to comment https://forums.phpfreaks.com/topic/258282-is-it-possible-to-split-an-input-field-into-two-fields-see-details-inside/#findComment-1324306 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.