0

I am trying to expand an existing form within PHPmyDirectory, and what I need to do is use the information entered earlier in the form to autopopulate some fields further down so re-entering data is not required.

For example: a "stage Name" field is automatically populated with a combination of the First nd Last Name Initial unless something specific is needed instead.

This code:

$form->addField('last_name','text',array('label'=>'Last Name','fieldset'=>'listing',
'onblur'=>'$.ajax({data:({action:\'rewrite\',text:$(\'#first_name\').val()+ \' \' 
+$(\'#last_name\').val().substring(0,1)}),success:function(text_rewrite)
{$(\'#stage_name\').val(text_rewrite)}});'));

generates: mary-l
onblur in the Stage Name field if someone has typed Mary Lambert in the first_name/last_name fields above

which is almost great - but I need it to simply generate: Mary L (no hyphen, no lowercase)

I'm picking this code up from a field which generates a friendly URL so I imagine that's why I'm getting lowercase and hyphens, I just can't figure out what is generating that formatting.

I've looked around quite a bit - I assume the '/rewrite'/ is doing it, but I can't find anywhere in the site code that assigns a function to '/rewrite'/ so is that a standard js command? And if so, is there another I should use in its place to get the output formatting I need?

Can anyone point me in the write direction for outputting a string that keeps the capitalization and gives me white space where I get hyphens now?

Thanks very much. This is probably a really obvious question, so sorry.

Haikukitty
  • 162
  • 2
  • 15

1 Answers1

1

No need for AJAX there. Just use javascript/jQuery.

I used the jQuery method blur() to detect when the lastname field is vacated. The rest should be straight-forward.

AJAX is used when you need to communicate with a back-end server, such as to perform a database lookup, and insert that new data into the current page. Here is a post that links to some simple AJAX examples.

$('#ln').blur(function(){
   var fn = $('#fn').val();
   var ln = $('#ln').val();
   if (fn.length && ln.length){
      var sn = fn + ' ' + ln; //full name (not used, just for e.g.)
      var sn2 = fn + ' ' + ln.slice(0,1); //first name, last name first letter only
      $('#sn').val(sn2);
   }
});
#d1{height:100px;width:250px;}
#d2{height:100px;width:250px;background:wheat;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="d1">
  First Name: <input id="fn" type="text" /><br>
  Last Name : <input id="ln" type="text" />
</div>
<div id="d2">
  Stage Name: <input id="sn" type="text" />
</div>
Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • Does it matter at all that this form is being generated via PHP? I actually tried what you are suggesting and that did not work either, so I figured there must be a reason the existing code was using ajax. I tried: $(document).ready(function(){ $("#description_short").focus(function(){ $("#first_name").val(function(i,origText) { return origText + "is one of our performers "; }); }); Perhaps I just need to structure it the way you did? I'll give it a shot! – Haikukitty Mar 30 '16 at 23:41
  • No, because you are interacting with the user. PHP code is rendered only once, as the page is being constructed before display. After that, you are dealing with js/jQuery, esp. when responding to user actions. If additional PHP processing is required (e.g. getting additional data from a database), then AJAX is used as the communication method. (For more on that, see AJAX examples in my post) – cssyphus Mar 30 '16 at 23:43
  • Ha! Stupid mistake - I didn't include the jquery script because I just assumed it was included in the site's template code - that did it! Brilliant - you have no idea how long I've spun my wheels on something so simple! – Haikukitty Mar 30 '16 at 23:48