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

Pass cakephp form variables to javascript to display in confirmation message?

本文关键字:显示 确认 消息 javascript 表单 cakephp 变量      更新时间:2023-09-26

如何将 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));?>

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

控制器

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

视图

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

我已经很久没有使用蛋糕 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>