Lefteris Posted May 17, 2009 Share Posted May 17, 2009 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 Quote Link to comment Share on other sites More sharing options...
waynew Posted May 17, 2009 Share Posted May 17, 2009 if (isset($_POST['submitNews'])) { Will be true if you $_POSTed a form to that page. If the $_POST variable submitNews is not found, the condition will be proved false. Quote Link to comment Share on other sites More sharing options...
Lefteris Posted May 17, 2009 Author Share Posted May 17, 2009 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. Quote Link to comment Share on other sites More sharing options...
waynew Posted May 17, 2009 Share Posted May 17, 2009 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. } Quote Link to comment Share on other sites More sharing options...
Lefteris Posted May 17, 2009 Author Share Posted May 17, 2009 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. Quote Link to comment Share on other sites More sharing options...
waynew Posted May 17, 2009 Share Posted May 17, 2009 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. Quote Link to comment Share on other sites More sharing options...
Lefteris Posted May 17, 2009 Author Share Posted May 17, 2009 A yeah session start! Now I remember. And you gotta put it before everything in the page, even the <head> tag, I think. Thanks for the help wayne. I will get on to it right away Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.