Jump to content

[SOLVED] A few simple questions


Lefteris

Recommended Posts

Hello all,

 

It has been a long time since I last touched php and I notice that many things are not as I thought them to be.

 

Just so I can remember I set out to make a simple news script which will communicate with a mysql Database which will keep my site's news. This should be simple but here are the questions.

 

So, I have a .php file to use as news addition file. This outputs a form like that:

 

<FORM ACTION="<?php echo(recentURL(true) ); ?>" METHOD=POST>
<p>Type the authors name here:<BR>
<input size="25" NAME="author">
<p>Type news title here:<BR>
<input size="25" NAME="newsTitle">
<P>Type your news here:<BR>
<TEXTAREA NAME="newsText" ROWS=10 COLS=40 WRAP>
</TEXTAREA><BR>
<INPUT TYPE=SUBMIT NAME="submitNews" VALUE="SUBMIT">
</FORM>

 

Right below the form from what little php I remembered I opened an if statement like that :

 

 if (isset($_POST['submitNews']))
  {
     

 

Inside the statement is the sql db connection code. The problem is that from the very start this if returns true. As a result whenever I load the page, or refresh it I have additions to the database. Any idea what could be wrong? Or do you need more code?

 

 

Another question I would like to ask is about variables. Let's say I have a variable which is not set. But I set it right after I click a button, and that button refreshes the page. When the page refreshes that variable should be set, right? In the code I am trying out it refreshes and the variable is not set.

 

Thanks in advance for the answers people :)

Link to comment
Share on other sites

Oh so that if returns true if the form exists!

 

Now all is clear to me at least as far as the form problem is concerned. I already solved it taking into account the actual usage of that if. Thanks wayne.

About the other thing I said do you have any idea :

 

Another question I would like to ask is about variables. Let's say I have a variable which is not set. But I set it right after I click a button, and that button refreshes the page. When the page has refreshed that variable should be set, right? In the code I am trying out, it refreshes and the variable is not set.

Link to comment
Share on other sites

Another question I would like to ask is about variables. Let's say I have a variable which is not set. But I set it right after I click a button, and that button refreshes the page. When the page refreshes that variable should be set, right? In the code I am trying out it refreshes and the variable is not set.

 

I'm not entirely sure what you mean. Here's an example themed towards what I think you mean.

 

HTML

<input type="submit" name="submit" value="Click Here" ./>

 

$button_pressed = false; //by default, unless proven otherwise
if(isset($_POST['submit'])){
$button_pressed = true; //now you know that the button was pressed.
}

Link to comment
Share on other sites

Thanks for your reply, what I actually meant is not exactly that but we can start from there.

 

Looking at your example $button_pressed is true now. True for how long? If I refresh the page with a  meta http-equiv a little later down in the code , will it still be set? That's what I meant to ask.

 

From the things I tried it seems it will not be set. I have totally forgotten how php works, there must have been some kind of global variable type for that purpose.

Link to comment
Share on other sites

I think you should look into using sessions.

 

if(isset($_POST['submit'])){
$_SESSION['button_clicked'] = true;
}

 

That Session variable will be available to you on each page, providing you place the function session_start() at the top of your pages.

 

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.