0

Greetings all,
I have a noob php question i need answering if possible..
I simply want to save what ever is typed into the form field for when the page is reloaded to show again.
I know it needs a new session started but im confused alot about what needs changing.
Here is my php of code:

<?php

include("config.php");
if(isset($_POST['submit'])) {
$word=$_POST['q'];
if($word=="") { $error="<font color='red'>Please enter a keyword</font>"; }
if($error=="") {
$sql=mysql_query("SELECT * FROM slogans ORDER BY RAND() LIMIT 1");
$r=mysql_fetch_array($sql);
echo '<font size="6">'; 
$slogan=$r['slogan'];
$slogan=str_replace("[keyword]",$word,$slogan);

echo $slogan;
echo '</font>';
}
}
echo $error; ?>

And my Form:

<form method="post">

<input type="text" name="q" VALUE="Slogan Maker" size="35" maxlength="255"  style="height:38px;font-size:18px;" onFocus="this.value=''"/>
<input type="submit" name="submit" value="Make Slogan!"> 

</form> 

Thanks alot! plz help

2
Contributors
5
Replies
10
Views
5 Years
Discussion Span
Last Post by diafol
0

Ok, you need to use a session. Place session_start() at the top of the file and use:

<?php
session_start();
if($_POST){
     $_SESSION['q'] = $_POST['q'];
     $q = $_SESSION['q'];
}     
?>

<input type="text" name="q" VALUE="Slogan Maker" size="35" maxlength="255"  style="height:38px;font-size:18px;" value="<?php echo $q;?>" />
<input type="submit" name="submit" value="Make Slogan!">      

Do you need to take off the onfocus attibute?
You could unset the $_SESSION['q'] after the $q= ... line if you don't want the text to be propogated beyond the post action, e.g. page refresh.

0

Hey thanksa lot for the reply! I have tried the ubove code and it seems it still doesnt save the form data when the page is reloaded. Does the PHP form u just sent go into the same area as my php form? or in diffrent php brackets all together.. Sorry i am very noob at php lol

0

Should go to the same page - sorry forgot the form tags! Doh!

<form method="post">
<input type="text" name="q" VALUE="Slogan Maker" size="35" maxlength="255"  style="height:38px;font-size:18px;" onFocus="this.value=''"/>
<input type="submit" name="submit" value="Make Slogan!"> 
</form>
0

Thanks alot Diafol for the reply, unfortunetly it has still not worked..

value="<?php echo $q;?>

From ur original PHP was missing in the new one> does this effect it?
Thanks again

0

Oh gawd, doh! Here you go, hope I've got it right this time:

<?php
session_start();
if($_POST){
     $_SESSION['q'] = $_POST['q'];
     $q = $_SESSION['q'];
}     
?>
<form method="post">
<input type="text" name="q" VALUE="Slogan Maker" size="35" maxlength="255"  style="height:38px;font-size:18px;" value="<?php echo $q;?> />
<input type="submit" name="submit" value="Make Slogan!"> 
</form>

Just a note, if you send the form to itself (same page) a refresh following a form submit will resend the data. As a rule forms are sent to different pages/files for processing and then redirected back to the form if req'd. The session solution works equally well for this, although you need to spread the code over two pages/files.

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.