Jump to content

Form radio buttons and drop down boxes not writing to database


arpeggio

Recommended Posts

Hi everyone! New to the group and also PHP coding.

What I have so far is 2 radio buttons and when or the other is ticked, it then displays its hidden drop down box to choose values. The radio buttons work correctly and show the proper drop down boxes but do not write correct values to the database!

 

I am trying to have the chosen radio box  called "INQUIRY" write a value of "1" to "IncidentType" column.

Or when the radio box called "COMPLAINT" is chosen, write a value of "2" to the same column.

It seems to always write "1" to the DB column regardless which radio box is ticked.

 

Also when the drop down box is chosen (which are populated from another DB column), I am looking for it to write the "complaint.ComplaintID" or the "inquiry.InquiryID" to the column "CaseTypeSubCategory" as the ID integer, which it does not.

 

I'm really stuck on solving this and would appreciate any help someone would be able to offer. I think my insert.php page has the correct values but if anyone would like to see it I will be happy to post the code.

Thank you in advance for replies! Here is my code so far for the page:

 

 
<html>
<head>
<style>
    #ca {
        display: none;
    }
</style>
 
<script src="[url=https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js]https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js[/url]" type="text/javascript"></script>
<script>
$(document).ready(function() {
    $("input:radio").click(function() {
        $('.searchSelect').hide();
        $('#' + $(this).attr("value")).show();
    });
});
</script>
</head>
<body>
 
<form action="insert.php" method="post">
 
<?php
 
//Connect to our MySQL database using the PDO extension.
$pdo = new PDO('mysql:host=10.150.100.194:3308;dbname=publiccommenttracking', '[color=#0000FF]Iputmyusernameandpassword[/color]', '[color=#0000FF]inapublicpostontheinternet[/color]');
 
//Our select statement. This will retrieve the data that we want.
$sql = "SELECT complaint.ComplaintID, complaint.Description as Des, inquiry.InquiryID,Inquiry.Description as InquiryDescription
FROM complaint left join inquiry on complaint.ComplaintID = inquiry.InquiryID"; 
 
 
 
//Prepare the select statement.
$stmt = $pdo->prepare($sql);
 
//Execute the statement.
$stmt->execute();
 
//Retrieve the rows using fetchAll.
$users = $stmt->fetchAll();
 
?>
    <ul id="countrylist"> 
        <li> 
            <label for="US-country"> 
                <input name="IncidentType" type="radio" id="US-country" value="com" checked  /> 
                INQUIRY</label><input type="hidden" name="IncidentType" value="1">
       
      </li> 
        <li> 
            <label for="CA-country"> 
                <input name="IncidentType" id="CA-country" type="radio" value="ca"/> 
                COMPLAINT</label><input type="hidden" name="IncidentType" value="2">
        </li> 
    </ul> 
    <select name="CaseTypeSubCategory" id="com" class="searchSelect" title="Search in"> 
         <?php foreach($users as $user): ?> 
        <option value="<?= $user['inquiry.InquiryID']; ?>"><?= $user['InquiryDescription']; ?></option>
   <?php endforeach; ?> 
    </select>
    
     
    <select name="CaseTypeSubCategory" id="ca" class="searchSelect" title="Search in"> 
        <?php foreach($users as $user): ?> 
        <option value="<?= $user['complaint.ComplaintID']; ?>"><?= $user['Des']; ?></option>
   <?php endforeach; ?> 
    </select>
    
      <p><input type="submit" value="Submit"><br>
</form>
    
</body>
</html>
Link to comment
Share on other sites

Sorry forgot to post my insert.php page:

 

<html>
<body>
 
 
<?php
$con = mysql_connect("xxxx","xxxx","xxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("publiccommenttracking", $con);
 
$sql="INSERT INTO publiccommenttracking.reportpartydetail (IncidentType,  CaseTypeSubCategory )
 
VALUES
('$_POST[incidentType]','$_POST[CaseTypeSubCategory]'";
?>
<?php
$query = "SELECT InquiryID,Description FROM inquiry ORDER BY InquiryID ";
 
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
 
 
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";
 
mysql_close($con)
?>
</body>
</html>
Link to comment
Share on other sites

At least the fields seem named appropriately.

Only if you think it's a good idea to have two hidden field with the same name (and the values 1 and 2) as the radio buttons. Regardless of which button is selected, both these will be posted and the value of one of them will overwrite the other.

Link to comment
Share on other sites

Barand I do see that is what's happening!

Being new to PHP, do I take out the <input type="hidden" name="IncidentType" value="2"> completely or is there a way to make the radio's post either a "1" for one radio and a "2" for the other radio to the database depending on which one is chosen?

 

Thanks again!

Link to comment
Share on other sites

The hidden fields are wrong and shouldn't be there. As I said in my first reply, give the radio buttons values 1 and 2. Whichever is selected is the one that will be posted.

 

If neither is selected then you won't get any value, so it may be useful to set one of them as "checked" as a default.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.