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 è 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 €</label>
</p>
<p>
<input name="10e" type="text" id="10e">
<label for="10e">10 €</label>
</p>
<p>
<input name="20e" type="text" id="10e">
<label for="10e">20 €</label>
</p>
<p>
<input name="50e" type="text" id="10e">
<label for="10e">50 €</label>
</p>
<p>
<input name="100e" type="text" id="10e">
<label for="10e">100 €</label>
</p>
<p>
<input name="200e" type="text" id="10e">
<label for="10e">200 €</label>
</p>
<p>
<input name="500e" type="text" id="10e">
<label for="10e">500 €</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?

sumorget_post_meta? Ifsum, 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