Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to finish a simple script that will run on localhost only I'll use it to store some variables 'cause I would like to use wordpress also as 'money manager' (as a proof of concept). But maybe I'm doing something wrong when I try to sum 2 variables. So here is my code:

<?php
/*
Template Name: Money count
*/
?>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>

<div class="fee-clearfix fee-initialized">

        <script type="text/javascript"> 
    var $j = jQuery.noConflict();
    $j(window).load(function(){     
        // this is the ID of your FORM tag
        $j(".contanti").submit(function(e) {
                    e.preventDefault(); // this disables the submit button so the user stays on the page
            // this collects all of the data submitted by the form
            var str = $j(this).serialize();                  
               $j.ajax({               
               type: "POST", // the kind of data we are sending
               url: "../wp-content/themes/slotlandia/n1/sendmoney.php", // this is the file that processes the form data
               data: str, // this is our serialized data from the form
               success: function(msg){  // anything in this function runs when the data has been successfully processed

                    // this sets up our notification area for error / success messages
                    $j("#note").ajaxComplete(function(event, request, settings)
                    { 
                        if(msg == "OK") // Message Sent? Show the Thank You message and hide the form
                        {
                            // This is shown when everything goes okay
                            result = "Your message has been sent. Thank you!";

                            // now hide the form fields
                            $j("#fields").hide();
                        }
                        else // if there were ewrrors
                        {
                            result = msg; // msg is defined in sendmail.php
                        }                                
                        $j(this).html(result); // display the messages in the #note DIV                          
                    });                  
                }                    
             });                     
        });         
    });
</script>
        <?php 
        $importaid = $post->ID;?>
        <?php echo 'importa id &egrave; pari a: '.$importaid;?>
        <div id="post-a-comment" class="clear"><div id="fields">
            <h4>Save you money</h4>
            <div class="expanded" >
                <form class="contanti" action="">
                        <p>
                            <input name="5e" type="text" id="5e">
                                <label for="name">5 &euro;</label>
                        </p>
                        <p>
                            <input name="10e" type="text" id="10e">
                                <label for="10e">10 &euro;</label>
                        </p>
                        <p>
                            <input name="20e" type="text" id="10e">
                                <label for="10e">20 &euro;</label>
                        </p>
                        <p>
                            <input name="50e" type="text" id="10e">
                                <label for="10e">50 &euro;</label>
                        </p>
                        <p>
                            <input name="100e" type="text" id="10e">
                                <label for="10e">100 &euro;</label>
                        </p>
                        <p>
                            <input name="200e" type="text" id="10e">
                                <label for="10e">200 &euro;</label>
                        </p>
                        <p>
                            <input name="500e" type="text" id="10e">
                                <label for="10e">500 &euro;</label>
                        </p>
                        <p>
                            <input type="submit" value="Submit" class="button" id="contact-submit">
                        </p>
                    <input type="hidden" value="<?php echo $importaid;?>" name="importatore" id="importatore">
                </form>
        </div><!--end fields--><div id="note"></div> <!--notification area used by jQuery/Ajax --></div>
        <br /><br />
        Var di controllo prima: <?php $meta_values = get_post_meta($importaid, 'Banconote5', true); 
        echo $meta_values;?> 
        <br /><br />
</div>
</body>

The to retrieve variables using ajax I have sendmoney.php

<?php

// allow us to use core wordpress functions
include '../../../../wp-load.php';
if ( isset( $_POST['importatore'])){
$numeropost = $_POST['importatore'];
        echo 'Importa ID  '.$numeropost.'  ';
};
$post = (!empty($_POST)) ? true : false;

if($post)
{
if (isset ($_POST['5e'])){
    $euro5 = $_POST['5e'];
    update_post_meta($numeropost,'Banconote5',$euro5);
    };
if (isset ($_POST['10e'])){
    $euro10 = $_POST['10e'];
    update_post_meta($numeropost,'Banconote10',$euro10);
    };
if (isset ($_POST['20e'])){
    $euro20 = $_POST['20e'];
    update_post_meta($numeropost,'Banconote20',$euro20);
    };
if (isset ($_POST['50e'])){
    $euro50 = $_POST['50e'];
    update_post_meta($numeropost,'Banconote50',$euro50);
    };
if (isset ($_POST['5e'])){
    $euro100 = $_POST['100e'];
    update_post_meta($numeropost,'Banconote100',$euro100);
    };
if (isset ($_POST['200e'])){
    $euro200 = $_POST['200e'];
    update_post_meta($numeropost,'Banconote200',$euro200);
    };
if (isset ($_POST['500e'])){
    $euro500 = $_POST['500e'];
    update_post_meta($numeropost,'Banconote500',$euro500);
    };
        $somma = get_post_meta($numeropost,'Banconote5',true);
        $checkvariabile20 = get_post_meta($numeropost,'Banconote20',true);
        $checkvariabile50 = get_post_meta($numeropost,'Banconote50',true);
        $checkvariabile100 = get_post_meta($numeropost,'Banconote100',true);
        $checkvariabile200 = get_post_meta($numeropost,'Banconote200',true);
        $checkvariabile500 = get_post_meta($numeropost,'Banconote500',true);

        echo '<h3>Somma : </h3';
        echo $somma;
$somma2 = $checkvariabile50 + $checkvariable100;
echo $somma2;
}

?>

I expected variable '$somma' to give me at least the value of 'get_post_meta' but even if the meta is correctly updated I can't understand why if I try to echo it (inside sendmoney.php) it doesn't work, the only way to have it echo something is to assign the value to the variable and immediately echo it...but this is useless 'cause I need to sum it to other variables. Is there anyone who can help me?

share|improve this question
Cannot see no sum being made in this code... What's the problem: sum or get_post_meta? If sum, this is off-topic here and has been answered dozen times in StackOverflow. . . . Also, I'm no expert in Ajax, but IMO, the way you're making the Ajax call seems pretty weird, consulting the Codex may be useful: codex.wordpress.org/AJAX – brasofilo Jul 28 '12 at 20:14
Problemy is both thing: 1 get_post_meta doesn't echo anything this is the reason why I can't sum... – maisdesign Jul 28 '12 at 20:25
please read the link brasofilo posted on proper AJAX use in WordPress, or read the answer I gave to this question for an example of AJAX in WordPress. Do not include wp-load directly, ever. – Milo Jul 28 '12 at 20:39
Ok but If i delete include wp-load.php I receive some errors regarding: update_post_meta() that I really don't know where is 'hidden' in WP code. – maisdesign Jul 28 '12 at 21:04
@maisdesign - If i delete include wp-load.php I receive some errors regarding: update_post_meta() yes, this is because you are not using AJAX action hooks. WordPress has a way of enabling AJAX which loads the WordPress environment, read the code in the link I posted. – Milo Jul 29 '12 at 13:59

closed as too localized by toscho Jul 28 '12 at 21:10

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

1 Answer

The problem was a lack of sleeping time :P I forgot to close an echo!

echo '<h3>Somma : </h3';
share|improve this answer
2  
Nonetheless, follow the suggestions given in the Comments to your Q. – brasofilo Jul 28 '12 at 22:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.