0

hi guys I am pretty newbish in javascript and php for that matter. I am making a page where user will choose either to create a radio or input fields for others to solve.

Everything works fine except, when I save the form, fields are not in the order I added them because I first loop over the 'input' fields and then over the 'radio' fields. I know this is probably not the way to do it, feel free to give me an alternative.

Any help would be appreciated, thanks in advance.

VIEW

  <h1>Add questions</h1>
<form action="" method="post">
<div id='pit'>
       <span id="add_input"><a href="#" class='button' style='font-size:1.5em;'><span>&raquo; add input </span></a></span><br>
        <span id="add_radio"><a href="#" class='button'style='font-size:1.5em;'><span>&raquo; Dodaj yes/no question</span></a></span>
</div>
<input type="hidden" name="id" value="<?= $this->uri->segment(3); ?>" />
   <input id="go" class="button" name="submit" type="submit" value="Save" />
</form>
<script type="text/javascript">
var count = 0;
var a=0;
$(function(){
    $('span#add_input').click(function(){
        count += 1;
        $('#pit').append('<p><strong>Pitanje #' + count + '</strong>'+ '<input id="field_' + count + '" name="fields[]' + '" type="text" /></p>' );
        a=count;
        document.write(a);
    });
});</script>
<script type="text/javascript">
var count = 0;
$(function(){
    $('span#add_radio').click(function(){
        count += 1;
        $('#pit').append('<p><strong>DA/NE #' + count + '</strong>'+ '<input id="radio_' + count + '" name="radios[]' + '" type="text" /></p>' );
    });
});</script>

CONTROLLER

$id=$this->input->post('id');
    if($_POST['fields']){
                    foreach ( $_POST['fields'] as $key=>$value ) {
                        $tip='input';
                        if($value!=''){
                            $this->page_model->add_questions($id,$value,$tip);
                        }

                    }
                }
                if($_POST['radios']){


                    foreach ( $_POST['radios'] as $key=>$value ) {
                        $tip='radio';
                        if($value!=''){
                            $this->page_model->add_questions($id,$value,$tip);
                        }

                }
            }

1 Answer 1

1

Something like this might work.

Maintain the same count variable in the JavaScript to keep track of which input is created.

Instead of using name="fields[]", use name="field_' + count + '" so you can iterate with a loop in the controller.

VIEW

<script type="text/javascript">
var count = 0;
var a=0;
$(function(){
    $('span#add_input').click(function(){
        count += 1;
        $('#pit').append('<p><strong>Pitanje #' + count + '</strong>'+ '<input name="field_' + count + '"  type="text" /></p>' );
        a=count;
        document.write(a);
    });

    $('span#add_radio').click(function(){
        count += 1;
        $('#pit').append('<p><strong>DA/NE #' + count + '</strong>'+ '<input name="radio_' + count + '" type="text" /></p>' );
    });
});
</script>

CONTROLLER

Use a regular expression to extract the necessary values.

$inputs = array();
$id=$this->input->post('id');
foreach($_POST as $key => $value) {
    $matches = array();
    if (preg_match('/(field|radio)_([\d]+)', $key, $matches)) {
        $tip = $matches[1];
        $count = $matches[2];
        $inputs[$count] = array($id, $value, $tip);
    }
}

Loop through the new $inputs array to call your add_questions method.

ksort($inputs);
foreach($inputs as $array) {
    $id = $array[0];
    $value = $array[1];
    $tip = $array[2];
    $this->page_model->add_questions($id,$value,$tip);
}
Sign up to request clarification or add additional context in comments.

Comments

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.