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

Recommended Answers

All 5 Replies

Member Avatar for diafol

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.

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

Member Avatar for diafol

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>

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

Member Avatar for diafol

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.