Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我需要在函数之间使用PHP变量_Php_Html_Wordpress - Fatal编程技术网

我需要在函数之间使用PHP变量

我需要在函数之间使用PHP变量,php,html,wordpress,Php,Html,Wordpress,需要一个可湿性粉剂插件选项页,用户可以从下拉菜单中选择删除链接 将创建选项页,并在名为printSelection()的函数中设置变量。此函数用于返回WP仪表板下拉框中的值 function printSelection() { if(isset($_POST['selectbox'])){ return $_POST['selectbox']; } } $link = printSelection(); print($link); $link变量正在成功打印到WP选项页面。我想做的

需要一个可湿性粉剂插件选项页,用户可以从下拉菜单中选择删除链接

将创建选项页,并在名为printSelection()的函数中设置变量。此函数用于返回WP仪表板下拉框中的值

function printSelection() {
if(isset($_POST['selectbox'])){
    return $_POST['selectbox'];
}
}

$link = printSelection();
print($link);
$link变量正在成功打印到WP选项页面。我想做的是在前端也使用这个变量,就在表单之后。完整代码如下:

<?php
/*
Plugin Name: Gladstone Brookes Mortgage Calculator Widget
Plugin URI: http://gladstonebrookesmortgages.co.uk
Description: A simple mortgage calculator widget
Version: 1.0
Author: Ian Butler
Author URI: http://gladstonebrookesmortgages.co.uk
*/

/*-----------------------------------------------------------------------------------*/
/* Include CSS */
/*-----------------------------------------------------------------------------------*/

function gb_mortgage_calculator_css() {     
wp_enqueue_style( 'gb_mortgage_calculator', plugins_url( 'assets/style.css',  __FILE__ ), false, '1.0' );
}
add_action( 'wp_print_styles', 'gb_mortgage_calculator_css' );

/*-----------------------------------------------------------------------------------*/
/* Include JS */
/*-----------------------------------------------------------------------------------*/

function gb_mortgage_calculator_scripts() {
wp_enqueue_script( 'calc', plugins_url( 'assets/calculator.js', __FILE__ ),  array('jquery'), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'gb_mortgage_calculator_scripts' );

/*-----------------------------------------------------------------------------------*/
/* Register Widget */
/*-----------------------------------------------------------------------------------*/

class gb_mortgage_calculator extends WP_Widget {

function gb_mortgage_calculator() {
   $widget_ops = array('description' => 'Display a mortgage calculator.' );
   parent::WP_Widget(false, __('GB Mortgage Calculator', 'gladstonebrookes'),$widget_ops);      
}

function widget($args, $instance) {  
        extract( $args );
        $title = $instance['title'];


    echo $before_widget; 
    if ($title) { echo $before_title . $title . $after_title; }
        global $ct_options;
    ?>

<div id="calculator" class="grid-60 prefix-15 suffix-15">        
<form name="mortgageCalculator" id="mortgageCalculator">
    <label class="grid-60">Loan Amount (£):</label><input class="grid-40"  id="la" type="text" name="la" value="0" />
    <label class="grid-60">Interest Rate (%):</label><input class="grid-40" id="ir" type="text" name="ir" value="0" />
    <label class="grid-60">Mortgage Term (Years):</label><input class="grid-40" id="mt" type="text" name="term" value="0" />
    <select id="type"><option id="r" value="repayment">Repayment</option> <option id="io" value="interestOnly">Interest Only</option></select>&nbsp;
    <input onclick="checkForZero(this); calculatePayment(this)" type="button" name="cmdCalc" value="Calculate" />
    <input onclick="resetForm(this)" type="button" name="reset" value="Clear Form" />
    <label class="grid-60 bold">Total Repayable:</label><input class="grid-40  bold" id="payments" type="text" name="payments" />
    <label class="grid-60 bold">Monthly Payments:</label><input class="grid-40 bold" id="pmt" type="text" name="pmt" />
</form>
<p><?php echo $link ?></p>
</div>

<div id="overlay" onclick="modal(this)">
<h4>Please enter numeric values only</h4>
<h4>The value of the following fields cannot be zero:</h4>
<p><strong>Loan Amount</strong></p><p><strong>Interest Rate</strong></p>
<p><strong>Mortgage Term</strong></p>
<p style="text-decoration:underline; color:blue;">dismiss</p>
</div>

<div id="fade"></div>

<?php echo $after_widget; ?>   
<?php
}

function update($new_instance, $old_instance) {      return $new_instance;
}

function form($instance) {

        $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';

?>
    <p>
       <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:','gladstonebrookes'); ?></label>
       <input type="text" name="<?php echo $this->get_field_name('title'); ?>"  value="<?php echo $title; ?>" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" />
    </p>
    <?php
}
} 

add_action( 'widgets_init', create_function( '', 'register_widget("gb_mortgage_calculator");' ) );

/*-----------------------------------------------------------------------------------*/
/* Register Shortcode */
/*-----------------------------------------------------------------------------------*/

function gb_mortgage_calculator_shortcode($atts) {  ?>
    <div class="clear"></div>

<div id="calculator" class="grid-60 prefix-15 suffix-15">
<h2>Mortgage Calculator</h2>        
<form name="mortgageCalculator" id="mortgageCalculator">
    <label class="grid-60">Loan Amount (£):</label><input class="grid-40" id="la" type="text" name="la" value="0" />
    <label class="grid-60">Interest Rate (%):</label><input class="grid-40" id="ir" type="text" name="ir" value="0" />
    <label class="grid-60">Mortgage Term (Years):</label><input class="grid-40" id="mt" type="text" name="term" value="0" />
    <select id="type"><option id="r" value="repayment">Repayment</option>
    <option id="io" value="interestOnly">Interest Only</option></select>&nbsp;
    <input onclick="checkForZero(this); calculatePayment(this)" type="button" name="cmdCalc" value="Calculate" />
    <input onclick="resetForm(this)" type="button" name="reset" value="Clear Form" />
    <label class="grid-60 bold">Total Repayable:</label><input class="grid-40 bold" id="payments" type="text" name="payments" />
    <label class="grid-60 bold">Monthly Payments:</label><input class="grid-40 bold" id="pmt" type="text" name="pmt" />
</form>
<p><?php echo $link ?></p>
</div>

<div id="overlay" onclick="modal(this)">
<h4>Please enter numeric values only</h4><h4>The value of the following fields cannot be zero:</h4>
<p><strong>Loan Amount</strong></p><p><strong>Interest Rate</strong></p>
<p><strong>Mortgage Term</strong></p>
<p style="text-decoration:underline; color:blue;">dismiss</p>
</div>

<div id="fade"></div>

<?php }
add_shortcode('mortgage_calculator', 'gb_mortgage_calculator_shortcode');

add_action('admin_menu', 'create_menu'); 

/*-----------------------------------------------------------------------------------*/
/* Create Options Page */
/*-----------------------------------------------------------------------------------*/


function create_menu (){

add_management_page('GB Mortgage Calculator', 'GB Mortgage Calculator', 10, 'gbmc_setting_file', 'gbmc_setting');

}

function gbmc_setting() { ?>

<div class="wrap">

<form method="post" name="options" target="_self">

    <h2>Display Link</h2>

    <table width="100%" cellpadding="10" class="form-table">

    <tr>
    <td align="left" scope="row">

        <label>Display Link</label>
        <select name="selectbox">
            <option value='<a href="http://gladstonebrookesmortgages.co.uk">Powered by Gladstone Brookes Mortgages</a>'>block</option>
            <option value="">hide</option>
        </select> 

    </td> 
    </tr>

    </table>
    <p class="submit">

    <input type="submit" name="Submit" value="Update" />

    </p>

</form>

</div>

<?php
}
function printSelection() {
if(isset($_POST['selectbox'])){
    return $_POST['selectbox'];
}
}

$link = printSelection();
print($link);

?>

贷款金额(英镑):
利率(%):
抵押期限(年):
仅偿还利息
应偿还总额:
每月付款:

请仅输入数值 以下字段的值不能为零: 贷款金额利率

抵押期限

驳回


我认为问题在于,在文件末尾调用函数,然后声明变量。您应该在文件开头调用
printSelection()
,然后将返回值指定给
$link
。这应该可以解决问题。

全局中分配任务是不可能的。只是说谢谢但那没用。我现在已经编辑了这个问题,以包含该文件中的所有代码。我认为
global
将是唯一的选项。明天我会仔细看一看代码汉克斯。我还没有得到它的工作,所以一个答案使用全球将是伟大的!