Page 1 of 1

Help a Noob - radio button

Posted: Thu Aug 23, 2007 2:27 am
by Xager
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Ok, my first post, yay!

I havent been using PHP for long so I am still learning alot. I used a tutorial the other day to add a "News" section to my website. It is pretty basic, just has Incremented ID, Title, Author, Post and Date. I have that working fine but what i want to do now is add 3 radio buttons so that I can add a image into the table row the title is in. So, basically its going to be a row with the image aligning to the left and the title aligned to the middle, then another row below it with the post and then another row below that with the Author and Date. I have the layout allready but I need to get that image in there relating to one of the radio buttons.

this is the code i have for the addnews.php form:

Code: Select all

<?php
   if (isset($_POST['submitted'])) {
   include ('mysql_connect.php');
   if (empty($_POST['title'])) {
   echo '<p><font color="red">You need to enter a title.</font></p>';
   } else {
   $title = $_POST['title'];
   }
    
   if (empty($_POST['name'])) {
   echo '<p><font color="red">You need to enter a name.</font></p>';
   } else {
   $name = $_POST['name'];
   }
     
   if (empty($_POST['message'])) {
   echo '<p><font color="red">You need to enter a message.</font></p>';
   } else {
   $message = $_POST['message'];
   }
     
   if ($title && $name && $message) {
   $query = "INSERT INTO news_posts (title, author, newstype, post, date) VALUES ('$title', '$name', '$newstype', '$message', NOW())";
   $result = @mysql_query($query);
     
   if ($result) {
   echo '<p><font color="red">News was added!</font></p>';
   } else {
   echo '<font color="red"><p>News could not be added! Please try again.</p></font>';
   }
   } else {
   echo '<p><font color="red">Please fill in the appropriate information</font></p>';
   }
   }
   ?>
     
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<p><b>News Title :</b><br />
<input type="input" name="title" size="25" maxlength="60" value="<?php if(isset($_POST['title'])) echo $_POST['title']; ?>" /></p>
          
<p><b>Name :</b><br />
<input type="input" name="name" size="15" maxlength="35" value="<?php if(isset($_POST['name'])) echo $_POST['name']; ?>" /></p>

<p><b>News Type :</b><br />
<input type="radio" name="newstype[]" value="news" /> News<br />
<input type="radio" name="newstype[]" value="wow" /> World of Warcraft<br />
<input type="radio" name="newstype[]" value="war" /> Warhammer 40,000<br />
<input type="radio" name="newstype[]" value="random" /> Random</p>
     
<p><b>Message :</b><br />
     
<textarea rows="7" cols="55" name="message"><?php if(isset($_POST['message'])) echo $_POST['message']; ?></textarea></p>
     
<p><input type="submit" name="submit" value="Add News" /></p>
<input type="hidden" name="submitted" value="TRUE" /></p>
</form>
I am not sure on how to actually submit the data from the radio buttons, I think I can retrieve it though. So, anyone able to tell me whats wrong here?

Hope that made sense. Thanks for any responces.

Regards,
Xager


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Aug 23, 2007 3:32 am
by Defkon1

Code: Select all

<p><b>News Type :</b><br />
<input type="radio" name="newstype" value="news" /> News<br />
<input type="radio" name="newstype" value="wow" /> World of Warcraft<br />
<input type="radio" name="newstype" value="war" /> Warhammer 40,000<br />
<input type="radio" name="newstype" value="random" /> Random</p>
you don't need to use an array to pass the radio value... submitting the form you'll have a $_POST['newstype'] variable...

if you prefer to allow multiple selections use checkboxes instead of radio buttons...

Posted: Thu Aug 23, 2007 6:45 am
by Xager
EDIT!!!: Ignore below text, I understand what you mean now, I have fixed it and it is working perfectly! Thanks so much for your help!

Thanks for the reply. I was trying to use checkboxes in the begining which is why I was using array. Sorry, but I dont know that much PHP so I dont really understand your next point: submitting the form you'll have a $_POST['newstype'} variable...

I got the current setup from a tutorial, so it was luck that it actually worked. Could you explain what you mean a bit more?

And I dont want to allow multiple selections, I just want the one.

Thanks again.
Regards,
Xager

Posted: Thu Aug 23, 2007 10:08 am
by Defkon1
:wink:

regards...