0

how do i get the user's input value and submit it to ajax then to controller? Right now, in my controller it says that id is an undefined index in $_POST['id']

This input textfield is actually within another form, kind of like a form inside a form.

<?php echo $form->textField($model,'email',array('id'=>'email')); ?>
        <?php echo $form->error($model,'email'); ?>
          <?php echo CHtml::ajaxButton ( 'Request Code', 
                CHtml::normalizeUrl(array('site/requestResetCode', 'render'=>true),
                array (
                    'type' => 'POST',
                    //'data'=> array('id'=> 'js:$("#ResetPasswordForm_email").val()'),
                    'data'=> array('id'=> 'js:$("#email").val()'),
                    'success'=>'function(data){
                        if(data.status == "sent")
                            bootbox.alert("Code is sent. Please check your email.");
                        else (data.status == "failed")
                            bootbox.alert("Request Failure");
                        }',
                    'error'=> 'function (xhr, ajaxOptions, thrownError) {
                        alert(xhr.status);
                        alert(thrownError);
                        }',
                )
            ))
        ?>

controller:

public function actionRequestResetCode()
    {
        $id = $_POST['id'];
//stuff..
hammies
  • 1,344
  • 2
  • 22
  • 46

1 Answers1

0

your POST array is empty because value is not being sent. You need to make a javascript function like

<script type="text/javascript">

    function getVal()
    { 
    var value= $("#myEmail").attr('value');
    alert(value);
    return value;

    }
    </script>

check if the alert shows correct value then remove this alert statement. If it does not show the value then there is a problem with the id of your textfield.
If you get it working then use it like

 'data'=> array('id'=> 'js:getVal()'),
Rafay Zia Mir
  • 2,116
  • 6
  • 23
  • 49
  • ok, when the page renders, right click on the textField and click inpect element. There u can see the id of the textField. tell me what is that id in the rendered html. – Rafay Zia Mir Mar 10 '14 at 13:08
  • same thing.. undefined... i also don't understand why it would be necessary to add another function like this? – hammies Mar 10 '14 at 13:12
  • @user3232194 i may not be in the position to describe that because when i work with ajax, sometimes the code you mentioned does not work. thats y i suggested a function. secondly a function is more readable. – Rafay Zia Mir Mar 10 '14 at 13:16
  • i think the id of textField is conflicting with some other element having same id. Try to change it to "myEmail" and try $("#myEmail").val(); andlet me know – Rafay Zia Mir Mar 10 '14 at 13:17
  • ok please post the rendered html of your page in your question so it may be more clear – Rafay Zia Mir Mar 10 '14 at 13:44
  • please see edit if it helps. i have changed the line $("#myEmail").val() to $("#myEmail").attr('value'); – Rafay Zia Mir Mar 10 '14 at 14:25
  • not working :/.. i think i am going to rewrite everything all over. thank you so much for your help. – hammies Mar 10 '14 at 14:44
  • you are welcome. Actually this is not a good approach to rewrite. Try to learn debugging. When you debug your code, you learn more. If you rewrite code, it may work but you will not able to find what the problem was. I think there is a problem in your rendered html. You need to check that. there are many options for value at http://stackoverflow.com/questions/4088467/get-value-in-input-text-box . look at the answer of RJD22. It may work for you. thanks – Rafay Zia Mir Mar 10 '14 at 14:49