Jump to content
Sign in to follow this  
FeverZebra

Realtime form

Recommended Posts

Hello there. I've been trying to find useful information with google but I'm not sure what I should look for hence I've failed... Is it possible to make a form which gives results right away, without a "submit" button? Say for instance:34*X=Y X is the value you put into the "form inputbox".Y is the result. If I type 10, then klick another box then I want Y to become 340 without having to push a submit/calculate button. Is this possible? At the moment I've a form and a script on the same page. However, I've to push submit to se the results. When I submit the form is reseted. If I can't make a "realtime script" then can I have the form NOT reseting when I submit? Thanks a bunch, and sorry if I've posted in the wrong category!/W

Share this post


Link to post
Share on other sites

You can run a function with the onkeyup event, but that would be Javascript and not PHP.

Share this post


Link to post
Share on other sites

Yes, doing a 'real-time' form is possible. You have to use JavaScript. Doing something like that would require some basic JavaScript knowledge. As for your form reseting, that's because you need a server-side script to fill the form back in. You're in the PHP section so I'm gonna assume you have PHP. To keep the input boxes filled in, you need to get the value that was passed using PHP. Take a look at the below example, it's very plain and straight-foward. Forms:

<form action="" method="post">  <input type="text" name="txtName" value="<?php echo $txtName; ?>" />  <input type="submit" name="btnName" value="Click Me" /></form>

PHP (on the same page, but at the top):

<?php  $txtName = $_POST['txtName'];?>

Share this post


Link to post
Share on other sites

Trying this with "value". However, it do not work. This is what I've written:

<form action="document.php" method="get">  <input type="text" name="txtName" value="<?php echo $name; ?>" />  <input type="submit" name="btnName" value="Click Me" /></form>

With this PHP code:

<?php  $name = $_GET['txtName'];?>

document.php is the same document the code is writen in. Is it because I use the method "get"? I want "GET" because then you can make more convinient links and bookmark stuff. I'm not that experianced with Java but I think i could figure stuff out with some help by google and asking around or simply try. That's also why I was unsure where to put this post^^ Btw, why is the row with "<input type="text" name="txtName" value="<?php echo $name; ?>" />" not colored?

Share this post


Link to post
Share on other sites

If your using the same page, you can simply remove document.php from the form. It will self reference without any help. The line is different because I put php code into the HTML which screws with it here on the forums. But don't worry, it's perfectly fine the way it is. GET is fine too. I'm simply used to using POST is all. I'm not sure why it's not working. That code there works fine, even how you have it set up. Can you post everything that you have so far?

Edited by Err

Share this post


Link to post
Share on other sites

All? Right... I've only tested with the first line though. Looking through my code I can't see any differances :/

<form action="" method="get"><p> <h1> Välj ras: </h1></p><p><input type="radio" name="ras" value="alv" /> Alv<br /><input type="radio" name="ras" value="bhorgol" /> Bhorgol<br /><input type="radio" name="ras" value="dvarg" /> Dvärg<br /><input type="radio" name="ras" value="manniska" /> Människa<br /><input type="radio" name="ras" value="troll" /> Troll<br /><input type="radio" name="ras" value="ork" /> Ork</p><p> <h1> Fysik: </h1><table width="200" height="auto" border="0" cellspacing="0">  <tr style="background: #F0F0F0;"><td style="padding-top:5px">Hälsa:</td><td align="right"><input type="text" name="halsa" value="<?php echo $ha; ?>" size="5" maxlength="15" /></td></tr>  <tr><td style="padding-top:5px">Uthållighet:</td><td align="right"><input type="text" name="uth" value="" size="5" maxlength="15" /></td></tr>  <tr style="background: #F0F0F0;"><td style="padding-top:5px">Skadebonus:</td><td align="right"><input type="text" name="sb" value="" size="5" maxlength="15" /></td></tr></table></p><p> <h1> Intelligens: </h1><table width="200" height="auto" border="0" cellspacing="0">  <tr style="background: #F0F0F0;"><td style="padding-top:5px">Inlärningsförmåga:</td><td align="right"><input type="text" name="inlar" value="" size="5" maxlength="15" /></td></tr>  <tr><td style="padding-top:5px">Ledarskap:</td><td align="right"><input type="text" name="ls" value="" size="5" maxlength="15" /></td></tr>  <tr style="background: #F0F0F0;"><td style="padding-top:5px">Skarpsinne:</td><td align="right"><input type="text" name="ska" value="" size="5" maxlength="15" /></td></tr></table></p><p> <h1> Smidighet: </h1><table width="200" height="auto" border="0" cellspacing="0">  <tr style="background: #F0F0F0;"><td style="padding-top:5px">Initiativ:</td><td align="right"><input type="text" name="ini" value="" size="5" maxlength="15" /></td></tr>  <tr><td style="padding-top:5px">Obeväpnad strid:</td><td align="right"><input type="text" name="oe" value="" size="5" maxlength="15" /></td></tr>  <tr style="background: #F0F0F0;"><td style="padding-top:5px">Undvika Anfall:</td><td align="right"><input type="text" name="ua" value="" size="5" maxlength="15" /></td></tr>  <tr><td style="padding-top:5px">Huggvapen:</td><td align="right"><input type="text" name="hugg" value="" size="5" maxlength="15" /></td></tr>  <tr style="background: #F0F0F0;"><td style="padding-top:5px">Krossvapen:</td><td align="right"><input type="text" name="kross" value="" size="5" maxlength="15" /></td></tr>  <tr><td style="padding-top:5px">Kättingvapen:</td><td align="right"><input type="text" name="katting" value="" size="5" maxlength="15" /></td></tr>  <tr style="background: #F0F0F0;"><td style="padding-top:5px">Sköld:</td><td align="right"><input type="text" name="skold" value="" size="5" maxlength="15" /></td></tr>  <tr><td style="padding-top:5px">Stickvapen:</td><td align="right"><input type="text" name="stick" value="" size="5" maxlength="15" /></td></tr></table></p><p><br /> <input type='submit' name='Submit' value='Submit' /></p></form>

and the PHP script:

<?php$ras = $_GET['ras'];$ha = $_GET['halsa'];$uth = $_GET['uth'];$sb = $_GET['sb'];$ska = $_GET['ska'];$ls = $_GET['ls'];$inl = $_GET['inlar'];$ini = $_GET['ini'];$oe = $_GET['oe'];$ua = $_GET['ua'];$hug = $_GET['hugg'];$kro = $_GET['kross'];$kat = $_GET['katting'];$sti = $_GET['stick'];$sko = $_GET['skold']; if($ras == "alv"){  $haA = $ha * 0.9;  $uthA = $uth * 1.2;  $sbA = $sb * 0.9;  $skaA = $ska * 1.3;  $lsA = $ls;  $inlA = $inl * 1.5;  $iniA = $ini * 1.4;  $oeA = $oe * 1.1;  $uaA = $ua * 1.5;  $hugA = $hug * 1.2;  $kroA = $kro;  $katA = $kat;  $stiA = $sti * 1.3;  $skoA = $sko * 1.2;   echo("<h1>Ras: Alv</h1>\n");  echo("<h2>Fysik:</h2>\n");  echo("<table width='300' height='auto' border='0' cellspacing='0'>   <tr><td>Egenskap</td><td align='right'>Poäng</td><td align='right'><b>Ung</b></td><td align='right'>Vuxen</td><td align='right'><b>Medel</b></td></tr>   <tr style='background: #F0F0F0;'><td>Hälsa:</td><td align='right'>$ha</td><td align='right'><b>" . floor($haA) . "</b></td><td align='right'>" . floor($haA*1.102) . "</td><td align='right'><b>" . $_GET['halsa']*0 . "</b></td></tr>   <tr><td>Uthållighet:</td><td align='right'>$uth</td><td align='right'><b>" . floor($uthA) . "</b></td><td align='right'>" . floor($uthA*1.106) . "</td><td align='right'><b>" . $_GET['halsa']*0 . "</b></td></tr>   <tr style='background: #F0F0F0;'><td>Skadebonus</td><td align='right'>$sb</td><td align='right'><b>" . floor($sbA) . "</b></td><td align='right'>" . floor($sbA*1.1) . "</td><td align='right'><b>" . $_GET['halsa']*0 . "</b></td></tr></table>");  echo("<h2>Intelligens:</h2>\n");  echo("<table width='300' height='auto' border='0' cellspacing='0'>   <tr><td>Egenskap</td><td align='right'>Poäng</td><td align='right'><b>Ung</b></td><td align='right'>Vuxen</td><td align='right'><b>Medel</b></td></tr>   <tr style='background: #F0F0F0;'><td>Inlärning:</td><td align='right'>$inl</td><td align='right'><b>" . floor($inlA) . "</b></td><td align='right'>" . floor($inlA*1.052) . "</td><td align='right'><b>" . $_GET['halsa']*0 . "</b></td></tr>   <tr><td>Ledarskap:</td><td align='right'>$ls</td><td align='right'><b>" . floor($lsA) . "</b></td><td align='right'>" . floor($lsA*1.07) . "</td><td align='right'><b>" . $_GET['halsa']*0 . "</b></td></tr>   <tr style='background: #F0F0F0;'><td>Skarpsinne:</td><td align='right'>$ska</td><td align='right'><b>" . floor($skaA) . "</b></td><td align='right'>" . floor($skaA*1.052) . "</td><td align='right'><b>" . $_GET['halsa']*0 . "</b></td></tr></table>");  echo("<h2>Smidighet:</h2>\n");  echo("<table width='300' height='auto' border='0' cellspacing='0'>   <tr><td>Egenskap</td><td align='right'>Poäng</td><td align='right'><b>Ung</b></td><td align='right'>Vuxen</td><td align='right'><b>Medel</b></td></tr>   <tr style='background: #F0F0F0;'><td>Initiativ:</td><td align='right'>$ini</td><td align='right'><b>" . floor($iniA) . "</b></td><td align='right'>" . floor($iniA*0.958) . "</td><td align='right'><b>" . $_GET['halsa']*0 . "</b></td></tr>   <tr><td>Obeväpnad strid:</td><td align='right'>$oe</td><td align='right'><b>" . floor($oeA) . "</b></td><td align='right'>EI</td><td align='right'><b>" . $_GET['halsa']*0 . "</b></td></tr>   <tr style='background: #F0F0F0;'><td>Undvika anfall:</td><td align='right'>$ua</td><td align='right'><b>" . floor($uaA) . "</b></td><td align='right'>" . floor($uaA*0.947) . "</td><td align='right'><b>" . $_GET['halsa']*0 . "</b></td></tr>   <tr><td>Huggvapen:</td><td align='right'>$hug</td><td align='right'><b>" . floor($hugA) . "</b></td><td align='right'>" . floor($hugA*1.05) . "</td><td align='right'><b>" . $_GET['halsa']*0 . "</b></td></tr>   <tr style='background: #F0F0F0;'><td>Krossvapen:</td><td align='right'>$kro</td><td align='right'><b>" . floor($kroA) . "</b></td><td align='right'>" . floor($kroA) . "</td><td align='right'><b>" . $_GET['halsa']*0 . "</b></td></tr>   <tr><td>Kätting:</td><td align='right'>$kat</td><td align='right'><b>" . floor($katA) . "</b></td><td align='right'>EI</td><td align='right'><b>" . $_GET['halsa']*0 . "</b></td></tr>   <tr style='background: #F0F0F0;'><td>Sköld:</td><td align='right'>$sko</td><td align='right'><b>" . floor($skoA) . "</b></td><td align='right'>EI</td><td align='right'><b>" . $_GET['halsa']*0 . "</b></td></tr>   <tr><td>Stickvapen:</td><td align='right'>$sti</td><td align='right'><b>" . floor($stiA) . "</b></td><td align='right'>" . floor($stiA) . "</td><td align='right'><b>" . $_GET['halsa']*0 . "</b></td></tr></table>");  }elseif($ras == "ork"){  echo("Hälsa: " . $_GET['halsa'] . "<br />\n");  }else {  echo ("<h1>Du har inte valt ras. Tryck bakåt så kommer dina värden att åter finnas och du behöver bara välja ras </h1><br />\n");  }echo("<h2>Övrigt:</h2>\nTotalt antal poäng: " . floor($ha+$uth+$sb+$ls+$ska+$inl+$ini+$oe+$ua+$hug+$kat+$sti+$kro+$sko) . "<br />");if ($ras == "alv"){  echo("Totalt antal poäng inklusive rasbonusar (som ung): ". floor($haA+$uthA+$sbA+$lsA+$skaA+$inlA+$iniA+$oeA+$uaA+$hugA+$katA+$stiA+$kroA+$skoA) . "");}else{  echo("Totalt antal poäng inklusive rasbonusar: fel");}?>

My rhough would be to use (as you can see) the value input under "halsa", put it into "$ha" and use your small string as value, "<?php echo $ha; ?>". Must be missing something I guess...

Share this post


Link to post
Share on other sites

all that code should be on one page, as has been stated. Is that the case in your post? The point is that the form submits to itself (a PHP page to begin with) that will receive the POST array and can thus be used to fill itself in after it's been submitted.

Edited by thescientist

Share this post


Link to post
Share on other sites

Both those codes are on the same page, and the page is ".php". Yes, I understand the way it should work.You type something -> you press submit -> a variable $var gets the value of "something" -> php writes $var where you wrote "something" -> you'll see the result + you'll still have the "something" inside the form field. Right? To me it seems as if the <?php echo $var; ?> in my form as value does not work. Notpad++ don't show it as "grey", it's shown as code and the submitfield have that string"<?php echo..." as a starting text. And btw "GET" should not be a problem, should it?

Edited by FeverZebra

Share this post


Link to post
Share on other sites
You type something -> you press submit -> a variable $var gets the value of "something" -> php writes $var where you wrote "something" -> you'll see the result + you'll still have the "something" inside the form field. Right?
To be specific, you press submit, and depending on the method (get/post) PHP takes the form elements and their values based on the name attributes you've designated them and populates the corresponding array ($_GET/$_POST) with correct members and values. It's up to you at that point. Most people opt to assign these key/values to more meaningful variables that can then be used throughout their script. In this case you are trying to populate a form with it's submitted values. A simple way to check if the form is at least submitting correctly is to put something like this at the top of your scripts for debugging purposes.
var_dump($_GET);

and at least make sure the array has the values you are expecting. Now naturally you will only see anything meaningful until after the form is submitted. But, when you submit, you should see what you submitted in the output. From there, trace the steps of your script and see where things might be going wrong, but outputting the one test variable to make sure it is what you expect at each step in the flow. You should be doing this for the if/else's so you know for certain what path your code is taking.

Share this post


Link to post
Share on other sites
array(16) { ["ras"]=> string(3) "alv" ["halsa"]=> string(2) "10" ["uth"]=> string(2) "10" ["sb"]=> string(2) "10" ["inlar"]=> string(2) "10" ["ls"]=> string(2) "10" ["ska"]=> string(2) "10" ["ini"]=> string(2) "10" ["oe"]=> string(2) "11" ["ua"]=> string(2) "10" ["hugg"]=> string(2) "10" ["kross"]=> string(2) "10" ["katting"]=> string(2) "10" ["skold"]=> string(2) "10" ["stick"]=> string(2) "10" ["Submit"]=> string(6) "Submit" }

Is what I get if I add "var_dump ($_GET);" in the begining, I assume this just types what every var have as value? Because it sure looks like it and it looks totally correct. I think the problem lies in the form. You have the php/form thing here normedico.com/are_calc.php as you can see the form have "<?php echo $ha; ?>" as starting value in one inputfield. However it disapears when you push submit and there will be no "reinput" of the value you type :( It is just frustrating... I'm sorry if I didn't understand your previous post fully but I'm fairely new to the thermonology and English isn't my native tongue :/

Edited by FeverZebra

Share this post


Link to post
Share on other sites
Guest LH91325

At least one part of the problem understanding your code is that you've separated it out into two sections. For example, if your HTML precedes your PHP then your $ha variable will not yet be populated. The PHP processor follows through the source echoing the HTML code until it sees <?php then shifts to executing PHP... So if all that PHP code follows your HTML code then by the time PHP calculates $ha values the HTML will have already been sent. I'm not saying that's what your problem is but it would help to just post the whole page at one time. This looks a lot like some of my code except I use POST, not GET. It never even occurred to me to use GET. Probably not relevant to your problem though.

Share this post


Link to post
Share on other sites

Right... I don't get it to work even if I put the HTML form inside php with everyting else. It works but I can't make the form keep whatever I wrote when I push submit, it's as if I reseted it. I struggle with understanding here:1. The page loads, code is read but no values are there yet so the only thing I see is the form.2. I add numbers in my form and push "submit". The page "reload" and now I can see an empty form and the results. If I add something like php inside the form which is inside a php to begin with it will not work. To me it seems like sence there's no value in the form to begin with it will not write a value. However, adding a value can only be done through the form... Like a circle with no begining or end. While on it, is it possible to prevent submiting (without reseting the fields) if something doesn't have a value? Must I make a script for that?

Share this post


Link to post
Share on other sites

you can put value in input tag's value attributes. so that when form will be sumited it will evaluate the input boxes with the valuesecho "<input type='text' value='{$_GET['field']}' name='field' />"; you have to use JS to prevent form submission when validation fails.

Share this post


Link to post
Share on other sites

Okay, gotta make me a JS then^^ But I've made it work now thanks to your input birbal. Can I do something similar with radio buttons? I've 6 alternatives, you must check one to proceede. Problem being they all have the same name...

Edited by FeverZebra

Share this post


Link to post
Share on other sites
I've 6 alternatives, you must check one to proceede. Problem being they all have the same name...
loop through them and check that at least one has been selected.

Share this post


Link to post
Share on other sites
Give them different names.
that defeats the purpose of radio buttons. Only one in a group can be checked/selected. If they have different names, you don't get that behavior.

Share this post


Link to post
Share on other sites
loop through them and check that at least one has been selected.
Yeah, I understand that. My question were if it is possible to return the selected one when the page "reloads" as I do with the "echo "<input type='text' value='{$_GET['field']}' name='field' />";". Meaning: when I push submit with alot of values and 1/6 radio buttons selected it will return the results but still save what you typed inside the form, including which radio button you selected.
Give them different names.
Will defeat the purpose of having radio buttons. Radio buttons are OR not AND. If I want alternative 1 I won't be able to select alternative 2 aswell. If I wanted that type of function I'd use checkboxes instead :) Edited by FeverZebra

Share this post


Link to post
Share on other sites

when you loop through the Radio buttons you will check in condition where "selected" attribute is set to "selected". if it matches you can assume that is the radio button you are selected.

Share this post


Link to post
Share on other sites

are not you trying to validate the radio buttons before form submit? or are you asking to preserve radio selection after form submit?

Share this post


Link to post
Share on other sites

something like.. option 1:<input type='radio' name='somename' <?php ($_POST['somename']=='option1')?'selected=selected' : ''; ?> />option 2:<input type='radio' name='somename' <?php ($_POST['somename']=='option2')?'selected=selected' : ''; ?> />.....so on option1 and option2 is the value of the radio button

Share this post


Link to post
Share on other sites

Never got that <?php TEXTTEXTEX ?> inside a form to work... This is where my "issue" are:

<?phpecho("<table width='700' border='0' cellspacing='0'><tr><td>");echo("<form action='' method='get'><p> <h2> Välj ras: </h2></p><p><table><tr><td><input type='radio' name='ras' value='alv' /> Alv<br /><input type='radio' name='ras' value='bhorgol' /> Bhorgol<br /><input type='radio' name='ras' value='dvarg' /> Dvärg<br /></td><td><input type='radio' name='ras' value='manniska' /> Människa<br /><input type='radio' name='ras' value='troll' /> Troll<br /><input type='radio' name='ras' value='ork' /> Ork</td></tr></table></p>);?>

It just returns error if I add what you surguested. My whole code including the form is inside a PHP and everything works part from returning the selected value.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...