Jump to content

Keep value in a form on refresh.


Rodis

Recommended Posts

Hello,

 

Hope somebody can help me here, last time you guys did and still very thankfull.

 

Now i am making myown admin script for my page and all is going fine except i have a problem  with my form. I am trying to keep my values when i select a different category in a field. when i change my category the current page gets refreshed so i can get diferent result in the subcategory selection. Only when i do that i loose all the contents filled out in the form and have to start all over. i will show some code here

 

form:

<SCRIPT language=JavaScript>
<!--
function changecat(newcat) {
    exit=false;
    site = "index.php?option=link_toevoegen&cat="+(newcat);
    if (newcat!=0) {
	top.location.href=site;
    } else {
	top.location.href="index.php?option=link_toevoegen";
    }
}
-->	
</SCRIPT>
<?php

// Dit word een taal bestandje
// define woorden bij link toevoegen.
define('CATEGORY', 'Categorie*');
define('SUBCATEGORY','Subcategorie');
define('LANGUAGE_SITE', 'Taal');
define('NAME_LINK','Naam*');
define('LINK_URL','Link*');
define('DESCRIPTION','Omschrijving');
define('IMAGE','Plaatje');
define('TIP','Tip');
define('KEYWORDS','Sleutelwoorden*');
define('DAY','Dag');
define('MONTH','Maand');
define('YEAR','Jaar');
define('EMAIL','Email');
define('NOTES','Opmerkingen');
define('BUTTON_SUBMIT','Toevoegen');
define('DATANEWLINK','Gegevens nieuwe link:');
define('STARFIELD','*Velden met een sterretje zijn verplicht');
define('EXPIREDATE','Verloop datum:');
define('MAXDATE','Max tot 1 januari 2038');
define('DATA_ADVERTISER','Gegevens adverteerder:');

?>
<?php
function categorie(){
if (isset($_GET['cat'])){
	$cat = $_GET['cat'];
	}else {
		$cat = "";
	}
$db = new db_connect();
$db->select = "SELECT c_naam,c_id FROM sb_link_categorie ORDER BY c_naam asc"; 
$db->selectQuery(); 
	while($row = $db->getRows()) {
		if ($cat == $row['c_naam']) {$selected = "selected";}else {$selected = "";}  
		$result = "<option $selected id=".$row['c_naam']." value=".$row['c_id'].">".$row['c_naam']."</option>"; 
		echo $result;
		}
}

function subcategorie(){
if (isset($_GET['cat'])){
	$categorie = $_GET['cat'];
	}else {
		$categorie = "";
	}
$db = new db_connect();
$db->select = "SELECT (s_naam) FROM sb_link_subcat WHERE c_naam = '$categorie' ORDER BY s_naam ASC";
$db->selectQuery();
	while ($row =$db->getRows()){
	$result2 = "<option value=".$row['s_naam'].">".$row['s_naam']."</option>";
	echo $result2;
	}
}

//vanaf hier gaan we zorgen dat bij cat wijzigen de inhoud blijft
if (isset($_POST['naam'])){$naam = $_POST['naam'];}else{$naam = "";}
?>
<table>
  <tr>
<td>
<p><? echo STARFIELD ?></p>
</td>
  </tr>
</table>
<form name="newlink" method="post" action="index.php?option=db_schrijven&action=toevoegen">
<table width="100%"  cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="3"><div align="center"><strong><? echo DATANEWLINK ?></strong></div></td>
  </tr>
  <tr>
    <td><? echo CATEGORY ?></td>
    <td><strong>:</strong></td>
    <td><select name="categorie" onChange="changecat(this.options[this.selectedIndex].id)">
      <option></option>
      <? categorie(); ?>
    </select></td>
  </tr>
  <tr>
    <td><? echo SUBCATEGORY ?></td>
    <td><strong>:</strong></td>
    <td><select name="subcategorie" onChange="MM_jumpMenu('parent',this,0)">
      <option></option>
      <? subcategorie(); ?>
    </select></td>
  </tr>
  <tr>
    <td><? echo LANGUAGE_SITE ?></td>
    <td><strong>:</strong></td>
    <td><select name="taal" onChange="MM_jumpMenu('parent',this,0)">
      <option selected>unnamed1</option>
    </select></td>
  </tr>
  <tr>
    <td><? echo NAME_LINK ?></td>
    <td><strong>:</strong></td>
    <td><input type="text" name="naam" value="<? echo $naam; ?>" size="50"></td>
  </tr>
  <tr>
    <td><? echo LINK_URL ?></td>
    <td><strong>:</strong></td>
    <td><input type="text" name="link" size="100"></td>
  </tr>
  <tr>
    <td valign="top"><? echo DESCRIPTION ?></td>
    <td valign="top"><strong>:</strong></td>
    <td>
      <textarea name="textarea" cols="80" rows="15"></textarea>	</td>
  </tr>
  <tr>
    <td><? echo IMAGE ?></td>
    <td><strong>:</strong></td>
    <td><input type="text" name="textfield3" size="50"></td>
  </tr>
  <tr>
    <td><? echo TIP ?></td>
    <td><strong>:</strong></td>
    <td><select name="menu1" onChange="MM_jumpMenu('parent',this,0)">
      <option selected>unnamed1</option>
    </select></td>
  </tr>
  <tr>
    <td><? echo KEYWORDS ?></td>
    <td><strong>:</strong></td>
    <td><input type="text" name="keywords" size="105"></td>
  </tr>
  <tr>
    <td colspan="3"><div align="center"><strong><? echo EXPIREDATE ?></strong></div></td>
  </tr>
  <tr>
    <td><span class="style1"><? echo MAXDATE ?></span></td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td><? echo DAY ?></td>
    <td><strong>:</strong></td>
    <td><input type="text" name="textfield5" size="50"></td>
  </tr>
  <tr>
    <td><? echo MONTH ?></td>
    <td><strong>:</strong></td>
    <td><input type="text" name="textfield6" size="50"></td>
  </tr>
  <tr>
    <td><? echo YEAR ?></td>
    <td><strong>:</strong></td>
    <td><input type="text" name="textfield7" size="50"></td>
  </tr>
  <tr>
    <td colspan="3"><div align="center"><strong><? echo DATA_ADVERTISER ?></strong></div></td>
    </tr>
  <tr>
    <td><? echo EMAIL ?></td>
    <td><strong>:</strong></td>
    <td><input type="text" name="textfield8" size="50"></td>
  </tr>
  <tr>
    <td valign="top"><? echo NOTES ?></td>
    <td valign="top"><strong>:</strong></td>
    <td><textarea name="textarea2" cols="80" rows="5"></textarea></td>
  </tr>
  <tr>
    <td colspan="3"><div align="center">
      <input name="Submit" type="submit" value="<? echo BUTTON_SUBMIT ?>">
    </div></td>
    </tr>
</table>
</form>

 

Is it possible to get the values back from a refresh?

 

Thanks in advance

Link to comment
Share on other sites

well.. on REFRESH as in f5 it would most likely be impossible to save the old values of the form.. but if you mean when you "submit" just loop through the db values and echo in the results.. if there is no result it will still be a blank text box.. if there is a result, it will fill it with the value

 

<input type="text" name="name" value="<?php echo $name; ?>" />

Link to comment
Share on other sites

I have a question about using sessions.

Is the session data stored on the server or on the user side? Since reading about it i have seen a pageview script using session. for example

<?php
session_start();  
if(isset($_SESSION['views']))
    $_SESSION['views'] = $_SESSION['views']+ 1;
else
    $_SESSION['views'] = 1;

echo "views = ". $_SESSION['views']; 
?>

 

And after using session_start() do i need to close the session at the end of the page?

 

Also does it have effect on the load of pages, since i have to start the session before any html is used i have to put it on top of the index which will result it starts at every page i load cause of the way i build my site using a template system that

i as a complete idiot not knowing how to but got it working by thinking of ways it could be done
so i don't know if that was a correct way
Link to comment
Share on other sites

well.. on REFRESH as in f5 it would most likely be impossible to save the old values of the form.. but if you mean when you "submit" just loop through the db values and echo in the results.. if there is no result it will still be a blank text box.. if there is a result, it will fill it with the value

 

<input type="text" name="name" value="<?php echo $name; ?>" />

 

Wel it's an onchange code that refreshes the pages to get the a different subcategory each time the category is changed. so that wouldn't work.

 

Thanks for the wuick reply's btw

Link to comment
Share on other sites

but if you mean when you "submit" just loop through the db values and echo in the results.. if there is no result it will still be a blank text box.. if there is a result, it will fill it with the value

 

<input type="text" name="name" value="<?php echo $name; ?>" />

 

as with my first post ^^ thats IF there isn't a refresh...

 

in the onchange event don't do top.location or whatever.. do theFormWithFields.submit()

Link to comment
Share on other sites

MMM can't seem to find anything on how theFormWithFields.submit() works.

 

and

 

Why would that not work?

 

Just put:

 

$name = $_POST['name'];

echo '<input type="text" name="name" value="'.$name.'" />';

 

That should work for each time the page is refreshed.

Only seems to work when submit is pressed and i need the values before a submit since the refresh script i put in changes the url to the category selected so only the subcategory's belonging to the selected category are showing. used $_GET['cat'] for category so the query changes to where cat= $_GET['cat'] also used the $_POST['naam'] and then echo that but since nothing is submitted it doesn't get the value if you know what i mean.

 

i have seen pages where that worked but maybe they where using different kind of scripting like AJAX i don't know they did it. maybe i should try and learn ajax since i read that u can get results from database without refreshing the whole page or am i wrong. I am a bit in the dark know

 

Really hope you guys can help with this. thanks anyway.

 

Link to comment
Share on other sites

yeah, I know my idea wouldn't work.

 

AJAX seems a bit excessive.

 

Why not just display different div elements when a user selects something rather than refreshing the page?

 

This will allow you to keep values in fields, and it'll be faster.

 

Something like:

 

onchange="document.getElementById('next_field').style.display='block'"

Link to comment
Share on other sites

yeah, I know my idea wouldn't work.

 

AJAX seems a bit excessive.

 

Why not just display different div elements when a user selects something rather than refreshing the page?

 

This will allow you to keep values in fields, and it'll be faster.

 

Something like:

 

onchange="document.getElementById('next_field').style.display='block'"

 

Have been looking into that but i don't know how to get the data from subcategory out of the database matching the category since this code only shows the info displayed in a hidden block so then i would have to manually add each subcategory's matching the category's and manually change them each time i add/delete category's and/or subcategory's Or wasn't that what you meant with this code?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.