HTML reset button not working properly - php

I have a basic sum example with some PHP and HTML, inside this PHP page the reset button only resetting first two input field, and it is not resetting the third answer field. I dont know what is the error with this cause i am new to PHP. someone please help me to fix this.
code
<html>
<head>
<title>Title goes here</title>
</head>
<body>
<form action="" method="post">
<label>Enter Num1:</label>
<input type="text" name="num1" id="num1" /><br>
<label>Enter Num2:</label>
<input type="text" name="num2" id="num2" /><br><br>
<input type="radio" name="rad" value="add"/>addition
<input type="radio" name="rad" value="sub"/>sub
<input type="submit" name="btn_submit" value="fire">
<?php
if(isset($_POST['btn_submit']))
{
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$rad_val = $_POST['rad'];
if($rad_val=="add"){
$total = $num1+$num2;
}
else if($rad_val=="sub"){
$total = $num1-$num2;
}
echo "<SCRIPT TYPE=\"text/javascript\">
document.getElementById(\"num1\").value= $num1;
document.getElementById(\"num2\").value= $num2;
</SCRIPT>";
echo "<label>Answer is:</label> <input type=\"text\" name=\"rad\" value = $total />";
echo"<input type=\"reset\" name=\"reset\" value=\"reset\" />";
}
else if(isset($_POST['reset'])){
echo "<script>window.location = 'your_page.php';</script>";
}
?>
</form>
</body>
</html>

The reset function loads the your_page.php. So the initial value on that page might be needed to be changed to 0. Could you give an example of your_page.php?
You can trigger a page refresh by changing the line
echo"<input type=\"reset\" name=\"reset\" value=\"reset\" />";
to
echo"<input type=\"reset\" name=\"reset\" value=\"reset\" onclick= \"window.location = 'your_page.php';\"/>";
OR
Even better. Just Reset the value in the Answer to 0, This will save on the page reload.
Add an ID to the result field :
<input type=\"text\" name=\"rad\" id=\"rad\" value = $total />";
And change te value on the reset click/
echo"<input type=\"reset\" name=\"reset\" value=\"reset\" onclick= \" document.getElementById(\"rad\").value= 0;\"/>";

With Jquery you can reset to any value but you net to prevent the default action:
Example:
$('#Reset').click(function(){
event.preventDefault();
$('#element').val('default');
$('#element2').val('default');
$('#element3').val('default');
});

Related

add values with one another with input field submit in php calculator

I want a calculator with my PHP code that adds values with one after one with submit button.like when I input a number then submit it and show it on the page then and input other, it should add previous number.and then enter another then submit.like these, numbers are adding with one another after submitting.
<?php
session_start();
?>
<?php
error_reporting(0);
?>
<html>
<title>adding input single values</title>
<body>
<form method="post">
<input type="text" name='number' method="post"/>
<input type="submit" />
</form>
<?php
if(!isset($_POST['number']))
{
}
else
{
$sum += $_POST['number'];
echo ++$sum;
}
?>
</body>
</html>
here is the output
You could use a hidden field instead of storing in the session.
<input type="text" name='number' method="post"/>
<?php
if(!isset($_POST['number'])) {
echo "<input type='hidden' name='prev_number' value=0 />";
} else {
$sum = $_POST['number'] + $_POST['prev_number'];
echo "<input type='hidden' name='prev_number' value=" . $sum . " />";
echo $sum;
}
?>
<input type="submit" />
</form>
<?php
if(!isset($_POST['number'])) {
// ...
}
else {
$_SESSION['number'] = isset($_SESSION['number']) ? $_SESSION['number'] : '';
$_SESSION['number'] += $_POST['number'];
echo $_SESSION['number'];
}

how to dynamically add / remove form while page refresh using php

How to dynamically add and remove input fields the form will stay while page refresh using PHP?. could it possible to use session? please any PHP script for this? thanks in advance!!
You could use something like this
<?php
$showEnterOther = "";
$showEnterOtherAsWell = "";
if(isset($_POST["show_form"])) {
$showEnterOther = "<input type='text' name='whatever' />";
$showEnterOtherAsWell = "<input type='text' name='whatever' />";
}
?>
<form action="#" method="post">
<input type="text" name="username" placeholder="Enter Username" value="<?php if(isset($_POST['username'])){echo $_POST['username'];} ?>"/>
<?php echo $showEnterOther; ?>
<?php echo $showEnterOtherAsWell; ?>
<input type="submit" name="show_form" value="Continue!" />
</form>

increase by one a variable every time you push a button (PHP)

I'd like that the variable $order is increase by 1 every time I push the submit button.
(The name of the page is study.php so every time I push the button the page is refreshed):
<?php
$order = $_GET['number'];
echo "<form action='study.php' method='GET'>
<input type='hidden' name='number' value='$order++' />
<input class='big_b' type='submit' value='next' />
</form>";
echo "$order";
?>
The first time 1 push the button $order is 1, the second 2, the third is 3 ... etc
Thanks for your help!
EDIT: SOLVED
<?php
session_start();
if(empty($_SESSION['count'])) $_SESSION['count'] = 0;
$order = $_SESSION['count']+1;
$_SESSION['count'] = $order;
echo "<form action='study.php' method='GET'>
<input class='big_b' type='submit' value='next' />
</form>";
echo "$order";
?>
How you currently have it, is that it will increment on each page refresh regardless whether button was clicked or not, do you need it only to increment if the button is pressed?
<?php
session_start();
// Reset to 1
if(isset($_POST['reset'])){unset($_SESSION['number']);}
// Set or increment session number only if button is clicked.
if(empty($_SESSION['number'])){
$_SESSION['number']=1;
}elseif(isset($_POST['next'])){
$_SESSION['number']++;
}
echo '
<form action="" method="POST">
<input class="big_b" type="submit" name="next" value="Next" />
<input type="submit" name="reset" value="Reset" />
</form>';
echo $_SESSION['number'];
?>
<?php
session_start();
if(empty($_SESSION['order'])){
$_SESSION['order'] = 1;
}
echo "<form action='study.php' method='GET'>
<input type='hidden' name='number' value='".$_SESSION['order']++."' />
<input class='big_b' type='submit' value='next' />
</form>";
echo $_SESSION['order'];
?>
You need to put $order++ outside of the quotation marks to make an operation (incrementing by 1). Here's the code:
<?php
$order = $_GET['number'];
echo "<form action='study.php' method='GET'>
<input type='hidden' name='number' value='".$order++."' />
<input class='big_b' type='submit' value='next' />
</form>";
echo "$order";
?>

PHP function to increment variable by 1 each time

I have started writing a PHP script for a game about creatures, there are 4 yes/no questions and what I am trying to do is write a function that will display 2 buttons that say yes and no and give then different names each time I run the function, for example yes1 and no1, then the next time the function is run the names of the buttons will be yes2 and no2.
I have attempted to do this already but it is not working correctly, below is the code I have done so far, any help would be much appreciated.
<?php
session_set_cookie_params(2592000);
session_start();
?>
<html>
<head>
<title>Creature Guessing Game</title>
</head>
<body>
<h1>Creature Guessing Game</h1>
<p> Welcome to the creature guessing game! </p>
<p>Click the button below to start or restart the game </p>
<form method="post" action="Creatures.php">
<input type="submit" name="start" value="Start Game" />
</form>
<?php
$questions = array('Does the creature live on land?', 'Does it have wings?', 'Does it live near water?', 'Can it fly?');
function repeat()
{
$number = 0;
$number++;
echo "<form method ='post' action='Creatures.php'>
<input type='submit' name='yes'.$number value='Yes' />
<input type='submit' name='no'.$number value='No' />
</form>";
}
//If form not submitted, display form.
if (!isset($_POST['start'])){
?>
<?php
} //If form is submitted, process input
else{
switch($_POST)
{
case yes1:
echo $questions[0];
repeat();
break;
case no1:
echo $questions[1];
repeat();
break;
case yes2:
echo $questions[2];
repeat();
break;
case no2:
$questions[3];
repeat();
}
}
?>
</body>
</html>
To do this you need to maintain state between requests.
function repeat() {
$number = $_SESSION['number'];
$number++;
echo "<form method ='post' action='Creatures.php'>
<input type='submit' name='answer' value='Yes' />
<input type='submit' name='answer' value='No' />
</form>";
$_SESSION['number'] = $number;
}
switch
if($_POST['answer']=='Yes') {
switch($_SESSION['number']) {
case 1:
break;
case 2:
break;
}
} else {
switch($_SESSION['number']) {
case 1:
break;
case 2:
break;
}
}
I am not in agreement with the accepted answer:
Here is an easier way to go about it without session:
function repeat() {
static $number = 0;
$number++;
echo "<form method ='post' action='Creatures.php'>
<input type='submit' name='answer' value='Yes' />
<input type='submit' name='answer' value='No' />
</form>";
}
You have to store the 'number' in session or cookie as it will loose the value every time page submitted (reloaded).

how to pre-load an existing form with a data from db?

i have a problem here,so here's my code
<div id="educmaininfo">
<?php
foreach($cv->getEducation($_GET['cvid']) as $r){
echo "<a href='#' id='editeducation'>Edit</a> | ";
echo "<input type='hidden' name='cvid' value='".$r['ResumeID']."' />";
echo "<input type='hidden' name='educid' value='".$r['EducationID']."'/>";
echo "<a href='#' id='deleteeducation'>Delete</a><br />";
echo "Date From = ".$r['DateFrom']."<br />";
echo "Date To = ".$r['DateTo']."<br />";
echo "Title =".$r['Title']."<br />";
echo "Summary =".$r['Summary']."<br />";
echo "Place Organization =".$r['PlaceOrganization']."<br />";
echo "Emphasis of Study =".$r['EmphasisOfStudy']."<br />";
echo "Study Details =".$r['StudyDetails']."<br />";
echo "Qualifications =".$r['Qualifications']."<br /><br />";
}
?>
</div>
as you can see that code above, I added 2 hidden stuff, the cvid and the educid.
my question is, how to load this form below
<div id="educajaxinfo" style="display:none">
<table>
<form id="educdetails" method="post" action="">
<input type="hidden" name="cvid" id="cvid" value="<?php echo $v['ResumeID']; ?>" />
<tr><td>Date From:</td><td><input type="text" name="datefromeduc" id="datefromeduc" value="" /></td></tr>
<tr><td>Date To:</td><td><input type="text" name="datetoeduc" id="datetoeduc" value="" /></td></tr>
<tr><td>Title:</td><td><input type="text" name="titleeduc" id="titleeduc" value="" /></td></tr>
<tr><td>Summary:</td><td><textarea name="summaryeduc" id="summaryeduc" rows="10" cols="50"></textarea></td></tr>
<tr><td>Place Organization:</td><td><input type="text" name="pog" id="pog" value="" /></td></tr>
<tr><td>Emphasis of study:</td><td><input type="text" name="eos" id="eos" value=""></td></tr>
<tr><td>Study Details:</td><td><textarea name="studyeduc" id="studyeduc" rows="10" cols="50" ></textarea></td></tr>
<tr><td>Qualifications:</td><td><textarea name="qualificationseduc" id="qualificationseduc" rows="10" cols="50"></textarea></td></tr>
<tr><td><input type="submit" name="update" value="<?php if($count < 3){ echo 'Add';}else{ echo 'Update';}?>" /></td></tr>
</form>
</table>
</div>
with the existing data from the db table, that matches the cvid and educid ?
the flow goes like this, if user clicks the edit link, it should redirect him to the
form with the values loaded in the input forms...if in pure php i can do this by just
doing something like
e.g edit.php?cvid=cvid&educid=educid
how to do it in ajax way?
Use jQuery to call a PHP script that returns JSON encoded data. Then use the “success” callback function to inject the data into your form.
Use the jquery function like following
a href='#' onClick=test
(echo $cvid ,echo $educid ) id='editeducation'>Edit
function test(cvid,educid)
{
jQuery.post('edit.php?cvid=cvid&educid=educid',function(response){
jQuery('#educajaxinfo').html(response);
});
}
include the appropriate jquery min file to use above function.
on edit.php get the two id's and prepare a whole html then it will be shown in #educajaxinfo div

Categories