OtagoHarbour Posted April 20, 2011 Share Posted April 20, 2011 I create a table in a database thus. $result = mysql_query("CREATE TABLE IF NOT EXISTS Subscribers ( personID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(personID), FirstName varchar(25), LastName varchar(25), UserName varchar(25), EmailAddress varchar(25), PassWord varchar(25), Gender varchar(16), MailingList char(1) )");I then set up a form with html thus.<form action="insert.php" method="post">First name: <input type="text" name="firstname" />Last name: <input type="text" name="lastname" />User name: <input type="text" name="username" />E-mail Address: <input type="text" name="EmailAddress" />Password: <input type="password" name="pwd" /><br /><input type="radio" name="MaleFemale" value="male" /> Male<br /><input type="radio" name="MaleFemale" value="female" /> Female<br /><input type="radio" name="MaleFemale" value="DeclineGender" /> Decline to give gender<br /><input type="checkbox" name="mailinglist" value="MailingList" /> Subscribe to mailing list.<br /><input type="submit" value="Submit" /><br /></form>insert.php include the following code to insert the record.$sql="INSERT INTO SubScribers (FirstName, LastName, UserName, EmailAddress, PassWord, Gender, MailingList)VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[username]','$_POST[EmailAddress]', '$_POST[pwd]','$_POST[MaleFemale]', '$_POST[mailinglist]')";This works if the checkbox is checked. However, if the checkbox is unchecked, the following message ensues.Notice: Undefined index: mailinglistHow should I fix this problem?Thanks,Peter. Link to comment Share on other sites More sharing options...
birbal Posted April 20, 2011 Share Posted April 20, 2011 you can use isset() to set a variable($_POST['mailinglist']) which does not exist Link to comment Share on other sites More sharing options...
pratkal Posted April 20, 2011 Share Posted April 20, 2011 1 possible solution to the issue is that if user select checkbox then the vale will be 1 otherwise value will be 0 ( as default ) Link to comment Share on other sites More sharing options...
OtagoHarbour Posted April 20, 2011 Author Share Posted April 20, 2011 you can use isset() to set a variable($_POST['mailinglist']) which does not existI had been playing around with isset() but no luck so far. I suspect my syntax is wrong but I cannot figure out what it should be. I tried $WantMailingList=0; if (isset('$_POST[mailinglist]')) $WantMailingList=1;and got the following error message.Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRINGThanks,Peter. Link to comment Share on other sites More sharing options...
OtagoHarbour Posted April 20, 2011 Author Share Posted April 20, 2011 1 possible solution to the issue is that if user select checkbox then the vale will be 1 otherwise value will be 0 ( as default )Would you know the right syntax for that? It's essentially what I've been trying to do with isset() but keep getting syntax errors.Thanks,Peter. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 20, 2011 Share Posted April 20, 2011 The quotes are in the wrong place.if (isset($_POST['mailinglist'])) $WantMailingList=1; Link to comment Share on other sites More sharing options...
OtagoHarbour Posted April 20, 2011 Author Share Posted April 20, 2011 The quotes are in the wrong place.if (isset($_POST['mailinglist'])) $WantMailingList=1;Yes!!! That works! Thanks very much!Peter. Link to comment Share on other sites More sharing options...
eduard Posted April 21, 2011 Share Posted April 21, 2011 ?????? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.