soph2602 Posted July 29, 2015 Share Posted July 29, 2015 Hi everyone, am new with php and need some expert advice here. Am trying to save and display the textbox color upon selection of an option. The steps are as follows: 1. User select Selfrating option ie Good or Satisfactory 2. Upon option selection, textbox T11 will change color based on select option 3. After submit button click, textbox T11 suppose to save in mysql and display color in the same form Currently the problem is with step 3 as the textbox doesn't save in mysql and doesn't display the color change. Please advise. Thanks. <div id="tabs-1"> <?php session_start(); $con = mysql_connect("localhost","user",""); if (!$con){ die("Can not connect: " . mysql_error()); } mysql_select_db("p",$con); $Picid = $_GET['Picid']; $nwQty = "SELECT * FROM progress WHERE Picid = '$Picid'"; $solution = mysql_query($nwQty); if(isset($_POST['submit'])){ if (mysql_num_rows($solution) == 0){ $sql = "INSERT INTO progress(T11,Selfrating1,Picid) VALUES ('" . $_POST["T11"] . "','" . $_POST["Selfrating1"] . "','" . $Picid . "')"; $result = mysql_query($sql); } else { $sql =("UPDATE progress SET T11='" . $_POST["T11"] . "', Selfrating1='" . $_POST["Selfrating1"] . "' WHERE Picid='" . $Picid . "'"); $result = mysql_query($sql); echo('Record Updated'); } $result = mysql_query("SELECT * FROM progress WHERE Picid='" . $Picid . "' "); $row= mysql_fetch_array($result); } ?> <script> function ocalculateText(el) { var form = el.form; var idx = form.Selfrating1.selectedIndex; if (idx <= 0) { form.reset(); return; } if (form.Selfrating1.value == "Good") { form.T11.value = "#008000"; } else if (form.Selfrating1.value == "Satisfactory") { form.T11.value = "#9ACD32"; } } </script> <p>a.i.Self Rating(1st Rating): <select name="Selfrating1" id="Selfrating1" onchange="ocalculateText(this)" value="<?php echo $row['Selfrating1']; ?>" > <option selected>Please select an option</option> <option value=Good <?php if($row['Selfrating1']=='Good') { echo "selected"; }?>>Good</option> <option value=Satisfactory <?php if($row['Selfrating1']=='Satisfactory') { echo "selected"; }?>>Satisfactory</option> </select> <input type="color" name="T11" id="T11" onchange="ocalculateText(this)" value="<?php echo $row['T11'];?>"></p> <input type="hidden" name="Picid" id="Picid" value="<?php echo $row['Picid']; ?>" > <td colspan="2"><input type="submit" name="submit" value="Save" class="btnSubmit"></td> </div> Quote Link to comment https://forums.phpfreaks.com/topic/297530-cannot-save-and-display-textbox-color-upon-select-change-in-php-and-html/ Share on other sites More sharing options...
iarp Posted July 29, 2015 Share Posted July 29, 2015 I would recommend you look into either mysqli or PDO before continuing much farther with the code. Here is a good example of starting with PDO and how to execute queries correctly and in a safe manner. As it currently is, the queries are completely unprotected and someone could easily delete your entire database. Quote Link to comment https://forums.phpfreaks.com/topic/297530-cannot-save-and-display-textbox-color-upon-select-change-in-php-and-html/#findComment-1517689 Share on other sites More sharing options...
QuickOldCar Posted July 29, 2015 Share Posted July 29, 2015 To try and do what you want need to use js/ajax or jquery. Php can not communicate with the javascript because is processed client side in the browser after the php script is completed. So this needs to send the data and be redirected back for php to see it. Quote Link to comment https://forums.phpfreaks.com/topic/297530-cannot-save-and-display-textbox-color-upon-select-change-in-php-and-html/#findComment-1517722 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.