basic php form help - php

I'm trying to make a calculator that will take inputs from users and estimate for them how much money they'll save if they use various different VoIP services.
I've set it up like this:
<form method="get" action="voip_calculator.php">
How much is your monthly phone bill?
<input name="monthlybill" type="text" value="$" size="8">
<p><input type="submit" name="Submit" value="Submit">
</p>
</form>
On voipcalculator.php, the page I point to, I want to call "monthlybill" but I can't figure out how to do it. I also can't figure out how to make it do the subtraction on the numbers in the rows.
This may be very simple to you but it's very frustrating to me and I am humbly asking for a bit of help. Thank you!
Here is the relevant stuff from voip_calculator, you can also click on the url and submit a number to see it in (in)action. I tried various times to call it with no success:
<table width="100%;" border="0" cellspacing="0" cellpadding="0"class="credit_table2" >
<tr class="credit_table2_brd">
<td class="credit_table2_brd_lbl" width="100px;">Services:</td>
<td class="credit_table2_brd_lbl" width="120px;">Our Ratings:</td>
<td class="credit_table2_brd_lbl" width="155px;">Your Annual Savings:</td>
</tr>
Your monthly bill was <?php echo 'monthlybill' ?>
<?php echo "$monthlybill"; ?>
<?php echo "monthlybill"; ?>
<?php echo '$monthlybill'; ?>
<?php echo 'monthlybill'; ?>
<?php
$monthybill="monthlybill";
$re=1;
$offer ='offer'.$re.'name';
$offername= ${$offer};
while($offername!="") {
$offerlo ='offer'.$re.'logo';
$offerlogo=${$offerlo};
$offerli ='offer'.$re.'link';
$offerlink=${$offerli};
$offeran ='offer'.$re.'anchor';
$offeranchor=${$offeran};
$offerst ='offer'.$re.'star1';
$offerstar=${$offerst};
$offerbot='offer'.$re.'bottomline';
$offerbottomline=${$offerbot};
$offerca ='offer'.$re.'calcsavings';
$offercalcsavings=${$offerca};
echo '<tr >
<td >
<a href="'.$offerlink.'" target="blank">
<img src="http://www.nextadvisor.com'.$offerlogo.'" alt="'.$offername.'" />
</a>
</td>
<td >
<span class="rating_text">Rating:</span>
<span class="star_rating1">
<img src="IMAGE'.$offerstar.'" alt="" />
</span>
<br />
<div style="margin-top:5px; color:#0000FF;">
Go to Site
<span style="margin:0px 7px 0px 7px;">|</span>
Review
</div>
</td>
<td >'.$offercalcsavings.'</td>
</tr>';
$re=$re+1;
$offer ='offer'.$re.'name';
$offername= ${$offer};
}
?>
offercal(1,2,3,4,5,6,7)savings calls to a file called values.php where they are defined like this:
$offer1calcsavings="24.99";
$offer2calcsavings="20.00";
$offer3calcsavings="21.95";
$offer4calcsavings="23.95";
$offer5calcsavings="19.95";
$offer6calcsavings="23.97";
$offer7calcsavings="24.99";

One thing i do is this
echo "<pre>";
print_r($_GET);
echo "</pre>";
Put this somewhere in your receiving end and you'll get an understanding of whats happening.

Not enough detail provided for an answer but let's simplify and assume you have the 'savings' numbers in an array, say, companySavings . So, you need to subtract each of these from the value the user specifies right? You don't need to call something (you could if you want...)
when the user clicks 'Submit' and the page is loaded again pull the monthlybill into a var e.g.
$monthlyBill = $_GET['monthlybill']; //you should do some checking to prevent attacks but that's another matter
Then, when you are building the list of savings it would look something like this
<?php
//...code for the rest of the page and starting your table
foreach($companySavings as $savings){//insert each row into the table
echo("<tr><td>".(comapnyName/Image whatever..)."</td><td>$".$monthlyBill-$savings."</td></tr>);
}
//... end the table and rest of code
?>

You need to get the value from the QueryString and put it into a PHP variable.
Like this:
$monthlyBill = $_GET['monthlybill'];
Now the variable $monthlyBill contains the value from the QueryString.
To display it:
echo "Your monthly bill is: $monthlyBill";

Related

Multiple entries added to a single variable?

I am trying to delete separate messages from a users inbox on my website. The user can go to the inbox, turn on the Delete Messages function which will show an extra HTML checkbox. This checkbox will then update the database to delete each message.
The messages and delete function are shown using the code below:
<p>
<a href="<?php echo 'http://basecentre.co.uk/', $message["username"]; ?>">
<img alt="" align="left" hspace="20" height="50" width="50"
src="<?php
echo 'http://basecentre.co.uk/userimages/',
$message["dp"];
?>">
</a>
<font color='<?php if ($message['unread']) echo '#FF0000'; ?>'>
<strong> <?php echo $message['first_name']; ?>
<?php echo $message['last_name']; ?> (
<?php echo date('d/m/Y H:i:s', $message['date']); ?>):
</strong>
</font>
<br />
<input type="checkbox" name="messageid" value="<?php echo $message['id']; ?>">
<?php echo $message['text']; ?>
</p>
The code to process the information is still being worked on, however I have hit a snag. The variable shows only ONE message id when it should actually show a list of all message id's that have been marked. Here is the processing code:
<?
/// DELETE MESSAGES
if (isset($_POST['delmessages'])) {
$delmessage_id = $_POST['messageid'];
echo $delmessage_id;
exit();
}
?>
How can I configure the variable to show more than one message id at a time ? Any ideas would be helpful. Thanks!
instead of
... name="foo"...
in your inputs, you need to use
... name="foo[] "
and all values will be in array $_POST[" foo"]

Display when the last visitor upvoted or downvoted next to the vote buttons

I have a PHP based CMS website. People have the ability to up or down vote a piece of content. I previously inquired as to how to add a time stamp to the vote buttons so that a person can see how recently anyone else has voted on the content.
I currently have the following table:
<table class="rating-table" cellspacing="0" cellpadding="0" border="0">
<tr>
<td><?php echo Kohana::lang('ui_main.credibility');?>:</td>
<td><a onclick="myFunction()" href="javascript:rating('<?php echo $incident_id; ?>','add','original','oloader_<?php echo $incident_id; ?>')"><img id="oup_<?php echo $incident_id; ?>" src="<?php echo url::file_loc('img'); ?>media/img/up.png" alt="UP" title="UP" border="0" /></a></td>
<td><a onclick="myFunction()" href="javascript:rating('<?php echo $incident_id; ?>','subtract','original')"><img id="odown_<?php echo $incident_id; ?>" src="<?php echo url::file_loc('img'); ?>media/img/down.png" alt="DOWN" title="DOWN" border="0" /></a></td>
<td><?php echo $incident_rating; ?></td>
<td><a href="" id="oloader_<?php echo $incident_id; ?>" class="rating_loading" ></a></td>
<td>Last voted at:
<span id="vote_date">
<script>
function myFunction()
{
var d = new Date();
var x = document.getElementById("vote_date");
x.innerHTML=Date();
}
</script>
</span>
</td>
</tr>
</table>
The second and third <td> elements are the up and down vote buttons. When someone clicks them the time and date is displayed in the last <td> element. Great!
Except (beginner here) I need this to not be session specific. If someone else visits the page the next day, they should see the last time that someone interacted with the vote buttons.
I do have limited coding skills but I suspect that I must somehow store the output of myFunction in a table and then echo it when someone else visits the page - is that right? Is there a simpler way?
Here is an example of what I'm trying to do on the live site. The time that the last person voted should be displayed next to the vote buttons (code block above is only on localhost): http://tinyurl.com/k5w4olu
you must store the votes in a database.
when the page loads, get the vote value for the particular page that is loading, and programmatically show an up or a downvote based on what the database says.
You are on the right track you need to add the date into a database or memory. the best way would be to AJAX the date variable to a mySQL db that you have setup and connected via PHP.
To understand more about mySQL go here -> http://www.w3schools.com/php/php_mysql_intro.asp
You will need to use AJAX easiest way is the jQuery Library -> http://www.w3schools.com/jquery/jquery_ref_ajax.asp

passing a php array item into a javascript popup window to be edited and updated in mysql db

I am creating a diabetes management system for a university project. One of the features of this system is for the patient to be able to send latest glucose readings and for the nurse to be able to login and comment on those readings.
I have been able to code the patient feature, however I wish to add a comment button in the comments column, which when clicked, brings up a popup window or a textbox for the nurse to be able to comment on that specific record. If a comment has not already been entered, an empty box should come up, however if there has been a previously entered comment, it should be displayed in the box to be updated and sent back to the mysql database. I would like to ask if someone could give me a way to include this comment box and code for the existing value to be displayed in the box and if there is no existing comment, then a new comment can be entered and stored in the database.
Below is my php code.
<?php//run query
$result = mysql_query($GetReadings);
?>
<table>
<tr>
<th>Date</th>
<th>Time</th>
<th>Glucose Level</th>
<th>SBP</th>
<th>DBP</th>
<th>Comments</th>
</tr>
<?php
//display results
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<tr>
<td><?php echo $row["Date"]; ?> </td>
<td><?php echo $row["Time"]; ?> </td>
<td><?php echo $row["GlucoseLevel"]; ?> </td>
<td><?php echo $row["SBP"]; ?> </td>
<td><?php echo $row["DBP"]; ?> </td>
<td><?php echo $row["Comments"];
<?php
//if statement to add comment link if user is a nurse
if ($_SESSION['User_level'] == 2)
{
//code for comments
}
?> </td>
</tr>
<?php
//end of while loop
}
?>
Hope I haven't missed out any crucial information.
Use the javascript function :
window.open(URL, windowName[, windowFeatures])
Where, URL - desired URL to display a page containing Textbox, use any window name you want.
Echo <a> or button with a onclick event eg:
Add Comment
Edit
The most basic way to achieve is, echo a <div></div> containing the past comments, the text box for new comments and Send/Cancel buttons. The trick is to set the display:none style property of that div. You the following code a guideline :
Echo the following code if the User has right user level.
Show Comments
<div id="comment-<?php echo $row['id']?>" style="display:none">
//display previous comments
<form method="post" action="addComment.php?id=<?php echo $row['id']?>">
<textarea name="comment"></textarea>
<input type="submit" value="Add Comment" /><input type="button" onclick="hideComment('<?php echo $row['id']?>')">
</form>
</div>
<script type="text/javascript">
function hideComment(id) {
document.getElementById('comment-' + id).style.display = 'none';
}
function showComment(id) {
document.getElementById('comment-' + id).style.display = 'block';
}
</script>

Submitting values of "jQuery Editable Invoice" into a MySQL DB using PHP

I am using this fantastic example of a jQuery editable invoice as a template for creating dynamic invoices for my users.
It's working quite well and I am successfully generating the items but I now need to save the values entered into the various text fields and enter them into the MySQL database.
I am confident in doing the MySQL entering with PHP but what makes this trickier is that the amount of 'invoice items' is completely dynamic and I am unsure how I can get PHP to 'check' through the pages text fields and find new ones, group them and then add them to my DB.
Here is an example of my code that I am using to generate the items:
<?php if($invoice_items->result_array()) { ?>
<?php foreach($invoice_items->result_array() as $invoice_Row): ?>
<tr class="item-row">
<td class="item-name">
<div class="delete-wpr">
<textarea><?php echo $invoice_Row['item_name']; ?> Facility Booking</textarea>
<a class="delete" href="javascript:;" title="Remove row">X</a>
</div>
</td>
<td class="description">
<textarea><?php echo $invoice_Row['description']; ?></textarea>
</td>
<td><textarea class="cost">$<?php echo $invoice_Row['hourly_cost']; ?>.00</textarea></td>
<td><textarea class="qty"><?php echo $total_time_hours; ?></textarea></td>
<td><span class="price">$<?php $unit_total = $invoice_Row['hourly_cost']* $total_time_hours; echo $unit_total;?>.00</span></td>
</tr>
<?php endforeach; ?>
<?php } ?>
I am thinking that I need to perhaps generate unique ID's for each invoice items text field, ie item-1-desc, item-1-cost etc, but that involves writing javascript which I know almost nothing about. Also I would still have to get PHP to loop through the ID's somehow until it reached the end...
If anyone has attempted something similar before or you can see a solution to my problem I would greatly appreciate your help.
Thanks,
Tim
Use the php form array syntax name="item-desc[<?php echo $id?>]"
You can then iterate them on the backend with foreach to store the data. You have the id's as keys to the arrays so it should be fairly trivial to update the db.

Basic php form help (2)

This is an extension of a question I had yesterday.
I am trying to make a little php calculator that will show how much people can save on their phone bills if they switch to VoIP, and how much they can save with each service.
I have a form that will spit out the right amount for a monthly bill here:
http://www.nextadvisor.com/voip_services/voip_calculator.php?monthlybill=50&Submit=Submit
But now I need to integrate that with some other data and put in a table. The prices for each of the different services are in another file called "values.php". The values are:
$offer1calcsavings="24.99";
$offer2calcsavings="20.00";
$offer3calcsavings="21.95";
$offer4calcsavings="23.95";
$offer5calcsavings="19.95";
$offer6calcsavings="23.97";
$offer7calcsavings="24.99";
I want each of the seven rows of the table to have one of the offercalcsavings values subtracted from the monthlybill value.
The php code currently looks like this:
<?php $monthlybill = $_GET['monthlybill']; ?>
Your monthly bill was <?php echo "$monthlybill"; ?>
<?php
$monthybill="monthlybill";
$re=1;
$offer ='offer'.$re.'name';
$offername= ${$offer};
while($offername!=""){
$offerlo ='offer'.$re.'logo';
$offerlogo=${$offerlo};
$offerli ='offer'.$re.'link';
$offerlink=${$offerli};
$offeran ='offer'.$re.'anchor';
$offeranchor=${$offeran};
$offerst ='offer'.$re.'star1';
$offerstar=${$offerst};
$offerbot='offer'.$re.'bottomline';
$offerbottomline=${$offerbot};
$offerca ='offer'.$re.'calcsavings';
$offercalcsavings=${$offerca};
echo '<tr >
<td >
<a href="'.$offerlink.'" target="blank">
<img src="http://www.nextadvisor.com'.$offerlogo.'" alt="'.$offername.'" />
</a>
</td>
<td ><span class="rating_text">Rating:</span>
<span class="star_rating1">
<img src="http://www.nextadvisor.com'.$offerstar.'" alt="" />
</span><br />
<div style="margin-top:5px; color:#0000FF;">
Go to Site
<span style="margin:0px 7px 0px 7px;">|</span>
Review
</div> </td>
<td >'.$offercalcsavings.'</td>
</tr>';
$re=$re+1;
$offer ='offer'.$re.'name';
$offername= ${$offer};
}
?>
I want each of the seven rows of the
table to have one of the
offercalcsavings values subtracted
from the monthlybill value.
You need to do the math somewhere. Presumably you would do this before the echo statement where you output your row.
$offerwithsavings = $monthlybill - $offercalcsavings
Then just be sure to include it in your table somewhere.
<td >'.$offerwithsavings.'</td>
The real prescription for this code is an array and foreach() loop, but I thought I'd keep my answer simple for now. Arrays and foreach() loops are very powerful and relatively quick and easy to learn. You would do well to give them some in depth study.
You can include the values.php file like this:
include 'path/to/values.php';
If you put the values in values.php in an array you can easily loop through them using a foreach loop:
in values.php;
$values['1calsavings'] = 24.99;
$values['2calsavings'] = 20.00;
etc;
Then in the other file:
include 'path/to/values.php';
foreach($values as $name => $amount){
echo '<some_markup>';
echo "$name $amount";
echo '</some_markup>';
}
Egads.
Use arrays!
So:
$offercalcsavings[0] = "24.99";
$offercalcsavings[1] = "20.00";
etc. etc.
Then, you're looping the output really:
for($i = 0; $i < CONSTANT_THAT_EQUALS_7; $i++) {
echo "<html bits>";
echo $offercalcsavings[$i];
echo $offerlink[$i];
etc. etc.
}

Categories