PHP calculator results in a new page? PHP first attempt - php

I'm trying to get the website to send the calculation results to another page. The code below is working but I have no idea how to get the rows with the results to be shown in a new page.
I know that i have to change the action below to /mynewpage
But I just want the results not the whole table.
I have no idea what to do to the code to make it show the results only in a new page. IF everything statys in the same page the calculator works well.
It's my first attempt with PHP, I clearly have no idea of what I'm doing. Many thanks in advance.
<?php
if (isset($_POST['valuea'])) $valuea = $_POST['valuea'];
if (isset($_POST['valueb'])) $valueb = $_POST['valueb'];
if (isset($_POST['valuec'])) $valuec = $_POST['valuec'];
if (isset($_POST['valued'])) $valued = $_POST['valued'];
if (isset($_POST['valuee'])) $valuee = $_POST['valuee'];
$balance = $valuec * $valuee;
$newphone = $valuea;
$total = $balance + $valuea;
$total2 = $balance + $valueb;
echo <<<_END
<form method='post' action='/'>
<table border='0' width='500px' cellpadding='3' cellspacing='1' class="table">
<tr class="calcheading"><td colspan="2"><strong>CALCULATOR</strong></td></tr>
<tr class="calcrow"><td>Phone Value:</td><td align="center"><input type='text' name='valuea' value="$valuea"/></td></tr>
<tr class="calcrow"><td>Phone upfront cost:</td><td align="center"><input type='text' name='valueb' value="$valueb"/></td></tr>
<tr class="calcrow"><td>Monthly contract cost:</td><td align="center"><input type='text' name='valuec' value="$valuec"/></td></tr>
<tr class="calcrow"><td>Contract duration:</td><td align="center"><input type='text' name='valued' value="$valued"/></td></tr>
<tr class="calcrow"><td>No. months left in the contract:</td><td align="center"><input type='text' name='valuee' value="$valuee"/></td></tr>
<tr class="submit"><td colspan="2"><input type='submit' value='Calculate'/></td></tr>
_END;
?>
<tr class="calcheading"><td colspan="2"><strong>OPTION 1 - PAY REMAINING OF THE CONTRACT AND BUY SAME PHONE UNLOCKED</strong></td></tr>
<tr class="calcrow">
<td><i>Payment left to network:</td>
<td align="center"><input type="text" value="<?php echo round($balance)?>"></td></i>
</tr>
<tr class="calcrow">
<td><i>New unlocked phone:</td>
<td align="center"><input type="text" value="<?php echo round($newphone)?>"></td></i>
</tr>
<tr class="calcrow">
<td><i>TOTAL:</td>
<td align="center"><input type="text" value="<?php echo round($total)?>"></td></i>
</tr>
<br>
<tr class="calcheading"><td colspan="2"><strong>OPTION 2 - PAY BALANCE LEFT AND GET SAME PHONE ON A NEW CONTRACT*</strong></td></tr>
<tr class="calcrow">
<td><i>Payment left to network:</td>
<td align="center"><input type="text" value="<?php echo round($balance)?>"></td></i>
</tr>
<tr class="calcrow">
<td><i>New contract phone initial cost:</td>
<td align="center"><input type="text" value="<?php echo round($valueb)?>"></td></i>
</tr>
<tr class="calcrow">
<td><i>TOTAL:</td>
<td align="center"><input type="text" value="<?php echo round($total2)?>"></td></i>
</tr></table>
</form>

You can either send the values in a form and receive them on the other page using
$value1 = $_GET['value1'];
$value2 = $_GET['value2']; // etc
The other method would be saving them in a session variable, at the top of any pages where you wish to use session variables, call session_start(), then save them
$_SESSION['value1'] = $value1;
Then in another page, you can call them by simply
echo $_SESSION['value1'];

I'm not sure what you mean by new page? If you have a script like process.php that has that code then you can add session_start(); as your first line after the php start tag. By using $_Session['result']=$calc_result; on the 'process.php' you will store the value in your session. In the 'new page' script you call session_start(); again and you can get the stored value by saying $_Session['result'].

There are many ways to print your answers on a new page but lets keep it simple: A good way to do it would be for you to separate out the HTML form that posts the values and the php calculation logic on two different pages. So for example, your HTML form is in one file values.php (does not have any php code, you can name it with a .html prefix as well) and the php code is in another file calc.php. Now, to your form, specify an action such as
<form method='post' action='calc.php'>
This will post all the values to calc.php where your calculation code is and you can display the results however you please (not limited to a form again) but in a table or so on. Once you learn ajax, you'll never want to come back to doing this.
Here is a working barebones example: http://runnable.com/VEKvtTTrkXFwjAwL/calculator555-for-php

Related

PHP how to Submit many records after search

I tried to look in this site but none of the question which relate exactly like mine. I need help on this problem since am very new in PHP.
Iam developing students result application which used to collect scores of the students. My problem is, I fail to submit all information I get like (idnumber, names and subject), after searching records of students of a certain class, here is my scripts.
<?php
$number = $courseObj->sele_ct_class($class, $year);
?>
<table class="table table-bordered" style="border-radius: 100px" border="0">
<tr style="font-size: medium; background-color: lavenderblush">
<td>#</td>
<td>Admission Number</td>
<td>Student name</td>
<td>Max.Mark</td>
<td>Mark</td>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$notaVilableId = $courseObj->seleId($class, $year); // Select_all From curClass according to aguments (return all idnumber)
foreach ($notaVilableId AS $haika) {
$classIdnumber = $haika->idnumber;
$jina = $haika->jina;
?>
<tr>
<td style="width: 5%"><?php echo $i++; ?></td>
<td><?php echo $classIdnumber ?></td> <!-- this display idnumber -->
<td><?php echo $jina ?></td> <!-- this display name -->
<td>100</td>
<td class="col-sm-2"><input type="number" id="subject" name="subject" class="form-control"></td> <!-- this fiels used to input score marks -->
</tr>
<?php } ?> <!-- end foreach -->
<tr>
<td colspan="20px">
<button class="btn btn-success" type="submit" name="fee_submit" id="fee_submit">Save
</button> <!-- submission button -->
</tr>
Problem is here, I get only idnumber of one student and the rest not appear, can any one show me how I could get all information like (idnumber, name and subject score which entered for each student). I mean that I want all information displayed to be submited with there scrored marks. Thank
<?php
if (isset($_POST['fee_submit'])) {
$classIdnumber;
$jina;
$score = sanitize($_POST['subject']);
$courseObj->insertseTExams($classIdnumber, $jina, $score); // function used to insert data
}
change this
<input type="number" id="subject" name="subject" class="form-control">
to note name="subject[]" change
<input type="number" id="subject" name="subject[]" class="form-control">
getting the data in php
i=0;
$number=count($_POST["subject"];
while ($i < $number ){
$score = sanitize($_POST['subject'][$i]);
//instert your data or do whatever else
$i++;
}
it seems you have assigned $classIdnumber,$jina to read its values from the $_POST super global variable.. And also, any data that is to be submitted to a server page for processing should be in-between form tags,this way you can access this values through the $_POST super global

php check text input length and change another input based on length

I have an php form utilizing several inputs that is driving a kiosk page. If a text input is blank I want this update a separate input with the word "hidden" If there is text I would like the word "visible" to show. Currently my code works if you click submit twice but will not work on the first submit. Here is my current code:
The if function that is current working on second submit:
if (strlen($something)>0) {
$_POST['someone'] = "visible";
} else {
$_POST['someone'] = "hidden";
}
input form:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table>
<tr>
<td>something : </td>
<td><input type="text" id="something" name="something" value="<?php echo htmlspecialchars($something); ?>"/></td>
<td></td>
</tr>
<tr>
<td></td>
<td><?php echo $_SERVER['PHP_SELF']; ?></td>
<td></td>
</tr>
<tr>
<td>someone:</td>
<td><input type="text" id="someone" name="someone" value="<?php echo htmlspecialchars($someone); ?>"/></td>
<td></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name='submit' value="Submit"/></td>
<td></td>
</tr>
</table>
</form>
Here is the update code:
$usql = "UPDATE test SET something= '".$_POST['something']."', someone= '". $someone ."' WHERE ID='a';";
Currently the "someone" input has a display of none so it cannot be seen by the user. This is not necessary but if someone could tell me how to bypass adding an input altogether and tweak the update statement itself to update something that would be great as well! Thanks!
Any help would be appreciated!
Using session variables can save the page state, even after reload.
First page save:
session_start();
$_SESSION['someone'] = $_POST['someone'];
Then:
if(isset($_SESSION['someone']))
{
if (strlen($_SESSION['someone'])>0) {
$_POST['someone'] = "visible";
} else {
$_POST['someone'] = "hidden";
}
}

How to save 'contenteditable' on database

<tr contenteditable>
<td><center><?php echo $nama = $isi['nama']; ?></center> </td>
<td><center><?php echo $jk = $isi['jk']; ?></center> </td>
I have this code,
And I dunno how to save it on my database.
if you guys don't mind, you can check the picture here : http://imagizer.imageshack.us/a/img673/4384/Ww9PKg.jpg
What I suggest is instead of making the table content editable, and allow users to change it, put the data that will be updated on input fields, and post to your PHP script to save:
<form action="/post.php" method="post">
<tr>
<td><input type="text" name="nama-0" value="<?php echo $nama = $isi['nama']; ?>"></td>
<td><input type="text" name="jk-0" value="<?php echo $jk = $isi['jk']; ?>"></td>
</tr>
</form>
But if you still need to use the table without the input elements (I've no idea why that'd be required) then use javascript to extract the values from table, and then submit to your PHP script.

Assistance with a PHP calculator

Okay, so I'm trying to make the calculator so $person1 and $person2 get added together, and then multiplied by 4. However, when I use this method, I test it by putting 1000 in one field, 1000 in the other and the result is "5000" when it should be "8000", yet I cannot seem to figure out why.
I tried adding "* 4;" to the $answer rather than having the $multivar variable, yet still the same issue.
<?php
if (isset($_POST['person1'])) $person1 = $_POST['person1'];
if (isset($_POST['person2'])) $person2 = $_POST['person2'];
$multivar = 4;
$answer = $person1 + $person2 * $multivar;
echo <<<_END
<form method='post' action='index.php'>
<table border='0' width='500px' cellpadding='3' cellspacing='1' class="table">
<tr class="calcheading"><td colspan="2"><strong>How much can you borrow?</strong></td></tr>
<tr class="calcrow"><td>Person 1 income:</td><td align="center"><input type='text' name='person1' value="$person1"/></td></tr>
<tr class="calcrow2"><td>Person 2 income</td><td align="center"><input type='text' name='person2' value="$person2"/></td></tr>
<tr class="submit"><td colspan="2"><input type='submit' value='Calculate'/></td></tr>
_END;
?>
<tr class="calcrow">
<td><i>You can borrow up to:</td>
<td align="center"><input type="text" value="<?php echo round($answer)?>"></td></i>
</tr>
</table>
</form>
It should be:
$answer = ($person1 + $person2) * $multivar;
Multiplication is done before addition, so you have to use parentheses if you want the addition to happen first.
I agree with Mischa, but a little bit more detail is that order of operations is coming into play here - "PEMDAS". Formulas in parentheses get calculated first, then exponents, then multiplication/division across the line, then addition/subtraction across the line. So what you had written earlier was actually calculated like: 1000+1000*4 which, to the compiler looks like 1000+4000 since the multiplication is performed first. Actual code should be $answer=($person1+$person2)*$multiplier.

HTML form to update Mysql with PHP (and HTML)

I've been trying to develop a real estate page where people can add listings. I am new to the world of php mysql. I have been over this problem for over a day and can't figure out where the problem is.
I have a form where people can add data. That's good and working. Now I am starting to have a place where people can add / delete / update their info. I am trying to build this step by step.
This is where a user could pull the information. My problem is with the piece of the code:
edit_form.php?idBAR=$row[id].
Full code below.
<table>
<tr>
<td align="center">EDIT DATA</td>
</tr>
<tr>
<td>
<table border="1">
<?php
include"configS_OH.php";//database connection
$order = "SELECT * FROM braaasil_brokerstour.property";
$result = mysql_query($order);
while ($row=mysql_fetch_array($result)){
echo ("<tr><td>$row[id]</td>");
echo ("<td>$row[address]</td>");
echo ("<td>$row[day]</td>");
echo ("<td>$row[hours]</td>");
echo ("<td>Edit</td></tr>");
}
?>
</table>
</td>
</tr>
</table>
Then this tutorial try to pass id through the address bar (I don't know much about php to actually say much)
It tries to upload the data into a new form where a person could edit info.
But I can't load the data into the new form. If I use where id=7, I get the info into the form. But this method of passing the info in the address bar like ?idBAR=8... and then try to catch it in the other code (where id=$idBAR), is not working.
Here is the code:
<table border=1>
<tr>
<td align=center>Form Edit Employees Data</td>
</tr>
<tr>
<td>
<table>
<?php
include "configS_OH.php";//database connection
print $database;
$order = "SELECT * FROM braaasil_brokerstour.property
WHERE id='$idBAR'";
print $idBAR;
$result = mysql_query($order) or die( mysql_error() );
$row = mysql_fetch_array($result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="idBAR" value="<?php echo "$row[id]"?>">
<tr>
<td>Address</td>
<td>
<input type="text" name="address"
size="20" value="<?php echo "$row[address]"?>">
</td>
</tr>
<tr>
<td>Date</td>
<td>
<input type="text" name="day" size="40"
value="<?php echo "$row[day]"?>">
</td>
</tr>
<tr>
<td>Time</td>
<td>
<input type="text" name="time" size="40"
value="<?php echo "$row[hours]"?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
I tried an tried and tried..
Thank you for your time in advance.
WHERE id='$idBAR'
You haven't assigned $idBAR any value. You need to read it from the $_GET array first:
$idBAR = $_GET['idBAR'];
You should, of course, check that this value exists first, and is acceptable.
I don't see anywhere you have actually used the GET data, just the reference name which is used in GET.
If you first query is working and is getting the $row['id'] value ok - you can verify this when you go to edit_form.php, in your browser URL bar at the top, does it say this:
edit_form.php?idBAR=7
(or whatever number should be there)
If so, then you just need to use the PHP GET. Your data is stored in $_GET[], and in this case, the reference name is idBAR. So your id from your previous page query is sent through the link into your URL, and on your edit_form.php page, you'd use that data as:
$_GET['idBAR']
You can use that, but personally I assign the data to a variable, such as:
$strGetId = $_GET['idBAR'];
Then you can use $strGetId throughout your code.
Also, check things like isset(), empty() etc, just so you know you are working with A) something is actually there, and B) it's not empty etc
if you are putting a variable directly in a string without concatenating, it can't be an array variable; you must concatenate those you also need to surr. so this
echo ("<td>Edit</td></tr>");
should be this
echo ("<td>Edit</td></tr>");
also, it looks like your form is sending data with POST. When you pass form data in the url string after the question mark, that is passing with get.
so...in your form where you want to use that variable, you set it up like this
$idBAR=$_GET['idBAR']; //to get the variable if it was part of the URL
$idBAR=$_POST['idBAR']; //if it was sent with post, as is the case with your form
also, request contains both get and post, so
$idBAR=$_REQUEST['idBAR'];
will work in either case.
The problem is the $row[id] is seen as text just like everything else. You want the value of $row[id]. Instead of
echo ("<td>Edit</td></tr>");
try
echo ("<td>Edit</td></tr>");

Categories