0

Please help me,

I have a a field "alias" type radio "yes" "no" when I select "yes" the form show the hidden field "family name" "first name" this work perfect, the file php send good the information, but I've added a bottom for "add alias" and the form show other two fields "family name" "first name" but the problem is that the php file dont send the information in the extra fields.

<span id="aliaser">
No<input type="radio" name="otheralias" value="0" checked  />&nbsp;&nbsp;Yes<input type="radio" name="otheralias" value="1"  />
</span>
<div id="morealias">
<br><div data-toggle="tooltip" title="">Family Name * <i class="fa fa-question-circle" aria-hidden="true"></i></div>
<input type="text" name="alias_lastname" id="aliaslastname" />
<br><div data-toggle="tooltip" title="">First (Given) Name * <i class="fa fa-question-circle" aria-hidden="true"></i></div>
<input type="text" name="alias_firstname" id="aliasfirstname" /><br>
<div id="aliasextra"></div>
<input type="hidden" name="aliasadded" id="aliasadded" value="0"/>
<br>
<div class="fullrow">
<div class="leftcol">
<span>
<a onclick="add_alias()" class="button medium dark">Add alias</a>&nbsp;&nbsp;
</span>
</div>
<div class="rightcol">
<span>
<a onclick="removealias()" class="button medium dark">Remove alias</a>
</span>
</div>
</div>
</div>

code js

    //more aliases
        $("#aliaser input[type='radio']").click(function(){
            if($(this).attr("value")=="1"){
                $("#morealias").css("display","block");
                $("#aliaslastname").prop('required',true);
                $("#aliasfirstname").prop('required',true);
                //$("input").prop('required',true);
            }
            else {
                $("#aliaslastname").val("");
                $("#aliasfirstname").val("");
                 $("#aliaslastname").prop('required',false);
                $("#aliasfirstname").prop('required',false);
                $("#morealias").css("display","none");
            }
        });
    //

    //more alias
    function add_alias() {
    var crd=$("#aliasadded").val();
    var newblock='<div class="fullrow" id="alremove' +crd + '">';
    newblock +='<br><b>Other Alias:</b><br>';
    newblock +='Family Name:';
    newblock +='<input type="text" name="alias_lastname' +crd + '" id="aliaslastname' +crd + '" />';
    newblock +='First (Given) Name:';
    newblock +='<input type="text" name="alias_firstname' +crd + '" id="aliasfirstname' +crd + '" />';
    newblock +='</div>';


    $("#aliasextra").append(newblock);
    var whichlastname="#aliaslastname" + crd ;
    var whichfirstname="#aliasfirstname" + crd ;
    $(whichlastname).prop('required',true);
    $(whichfirstname).prop('required',true);
    crd++;
    $("#aliasadded").val(crd);
    }

    function removealias(){
    var crd=$("#aliasadded").val();
    crd --;
    var whichal="#alremove" +crd ;
    $(whichal).remove();
    $("#aliasadded").val(crd);
    }

Code php

    $texter .="Known by other aliases?:  " . $data_array["otheralias"] . "<br>";
    if($data_array["otheralias"]=="Yes") {
    $texter .="Alias last name:  " . $data_array["alias_lastname"] . "<br>";
    $texter .="Alias first name:  " . $data_array["alias_firstname"] . "<br>";
    }
    if($aliasadded >0) {
    for($i=0;$i<$aliasadded;$i++) {
    $b=$i+1;
    $texter .="<b>Extra alias No.  " . $b . ":</b><br>";
    $texter .="Alias last name:  " . $data_array["alias_lastname$i"] . "<br>";
    $texter .="Alias first name:  " . $data_array["alias_firstname$i"] . "<br>";
    }
    }

Thanks a lot!

2
  • 1
    The 'alias_lastname' and 'alias_firstname' should be arrays (alias_lastname[], alias_firstname[])otherwise when you submit the form only the last values will be sent Commented May 4, 2016 at 8:19
  • Hi, could you explain me a little more please, exactly where I should put this?, I'm not so good in this, I just follow an example of the other form similar Commented May 4, 2016 at 8:48

1 Answer 1

2

Use following scenario:

HTML:

Alias 1 Firstname: <input type="text" name="firstname[]" />
Alias 2 Firstname <input type="text" name="firstname[]" />

name attribute contains [ ] brackets. It means, that you can take values in PHP as array:

$names = $_POST['firstname']
$alias1 = $names[0];
$alias2 = $names[1];
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, could you explain me a little more please, exactly where I should put this?, I'm not so good in this, I just follow an example of the other form similar
update input tag names everywhere. in your html instead of alias_firstname and in your js instead of alias_firstname+cdr put 'alias_firstname[]'. do the same for lastname. in your php file get values like this: $data_array["alias_firstname"][0], $data_array["alias_firstname"][1]... Start debugging and u will get the point.

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.