将cakephp表单变量传递给javascript以显示确认消息?

时间:2014-12-09 10:57:58

标签: javascript cakephp

如何将cakephp表单的变量(test1,test2,test3 ...)传递给javascript函数status(),以在window.confirm中显示所有变量信息(“在此显示所有变量...”)。

这是代码:     

function status() {

             var status_id = document.getElementById("testId").innerHTML; 

            var keep = window.confirm("Display all variables here...");

            if(keep == true){

                return true;

            }else{

                return false;

            }
        }

</script>

<?php echo $form->create('Product', array('url' => '/myproducts/products/add/'.$product['Product']['id'] , 'onSubmit'=>'return status(this)'));?>
    <fieldset>
        <legend><?php echo "Product Title";?></legend>

        <?php
            echo $form->input('test1', array('label' => __('Test 1', true)));
            echo $form->input('test2', array('label' => __('Test 2 ', true)));
            echo $form->input('test3', array('label' => __('Test 3', true)));

            echo $form->input('test_price', array('label' => __('Test Price?', true)));
            echo $form->input('testId', array('label' => __('Test ID',  true)));    

        ?>
</fieldset>
<?php echo $form->end(__('Add Product >>', true));?>

2 个答案:

答案 0 :(得分:0)

如何从控制器生成字符串,将其发送到视图,并将其显示在那里?

<强>控制器

$this->set('my_variables', "$test1, $test2, $test3");

查看

var keep = window.confirm("Display all variables here: <?php echo $my_variables; ?>");

答案 1 :(得分:0)

我还没有在很长一段时间内使用过蛋糕1.2,但我会将ID分配给所有关注的DOM并从那里耙出来。以下内容可以让您接近自己所追求的目标。 (此示例必须包含jQuery!)

<?php echo $form->create('Product', array('url' => '/myproducts/product/add/'.$product['Product'['id'] , 'id'=>'form'));?>
<fieldset>
<legend><?php echo "Product Title";?></legend>

<?php
    echo $form->input('test1', array('label' => __('Test 1', true),'id' => 'test_1')); //assign ID's else they would be ProductTest1
    echo $form->input('test2', array('label' => __('Test 2 ', true),'id' => 'test_2'));
    echo $form->input('test3', array('label' => __('Test 3', true),'id' => 'test_3'));

    echo $form->input('test_price', array('label' => __('Test Price?', true)));
    echo $form->input('testId', array('label' => __('Test ID',  true)));

?>
</fieldset>
<?php echo $form->end();?>
<a href="javascript:void(0)" class="" id ="submit_button">Submit</a>


<script>
    jQuery(function ($){
        $('#submit_button').click(function(){
            //gather the current values 
            var value_1 = $('#test_1').val();
            var value_2 = $('#test_2').val();
            var value_3 = $('#test_3').val();
            var display_text = 'Submit: '+ value_1 + ' ' + value_2 + ' ' +value_3 + '?';
            if(window.confirm(display_text)){
                //if confirmed, submit the form
                $('#form').submit();
            }else{
                //cancel logic here
            }
        });
    });
</script>