johram pong johram pong - 10 days ago 4
Javascript Question

Need to update a query from dropdown section

Mates,
I have this dropdown menu and I need to update a section and the database with a radio button whenever any user clicks it. I' m not sure how can I achieve it.


My dropdown looks like this

<div class="dropdown">
<button onclick="myFunction()" class="dropMenu"><span class="col_blue">Welcome:</span> <?php echo $_SESSION['username']; ?></button>
<div id="myDropdown" class="dropdown-content">

<form class="form">
<div class="switch-field">
<div class="switch-title">Make Resume Public?</div>
<input type="radio" id="switch_left" name="switch_2" value="yes" checked/>
<label for="switch_left">Yes</label>
<input type="radio" id="switch_right" name="switch_2" value="no" />
<label for="switch_right">No</label>
</div>
</form>

<?php
$switch_2 = $_POST['switch_2'];

$qry = "UPDATE resumes SET public = :public WHERE memberID = :memberID";
$stm = $db->prepare($qry);
$stm->bindParam(':public', $switch_2, PDO::PARAM_STR);
$stm->bindParam(':memberID', $uid, PDO::PARAM_STR);
$stm->execute();
?>

<?php
$stmt = $db->prepare("select * from jobs where jobposterID = '".$_SESSION['memberID']."'");
$stmt->execute();
$jobs = $stmt->rowCount();
?>
<a href="<?php echo DIR; ?>my-jobs.php"><i class="fa fa-suitcase"></i> MY JOBS (<?php echo $jobs; ?>) </a>
<a href="<?php echo DIR; ?>applied-jobs.php"><i class="fa fa-check-square-o"></i> APPLIED JOBS </a>
<a href="<?php echo DIR; ?>viewed-jobs.php"><i class="fa fa-eye"></i> VIEWED JOBS </a>
<a href="<?php echo DIR; ?>logout.php"><i class="fa fa-power-off"></i> LOGOUT </a>
</div>
</div>


and i needed this portion to be updated whichever gets clicked should update database and remain selected

<form class="form">
<div class="switch-field">
<div class="switch-title">Make Resume Public?</div>
<input type="radio" id="switch_left" name="switch_2" value="yes" checked/>
<label for="switch_left">Yes</label>
<input type="radio" id="switch_right" name="switch_2" value="no" />
<label for="switch_right">No</label>
</div>
</form>

<?php
$switch_2 = $_POST['switch_2'];

$qry = "UPDATE resumes SET public = :public WHERE memberID = :memberID";
$stm = $db->prepare($qry);
$stm->bindParam(':public', $switch_2, PDO::PARAM_STR);
$stm->bindParam(':memberID', $uid, PDO::PARAM_STR);
$stm->execute();
?>


Appreciate your help and time.

Answer

I am assuming that you are able to update the database.

Here is the code to get the radio button selected

        <?php
        $switch_2 = $_POST['switch_2'];

        $qry = "UPDATE resumes SET public = :public WHERE memberID = :memberID";
        $stm = $db->prepare($qry);
        $stm->bindParam(':public', $switch_2, PDO::PARAM_STR);
        $stm->bindParam(':memberID', $uid, PDO::PARAM_STR);
        $stm->execute();
        ?>

        <?php
        $stmt = $db->prepare("select * from jobs where jobposterID = '".$_SESSION['memberID']."'");
                $stmt->execute();
                $jobs = $stmt->rowCount();    
        ?>              
        <a href="<?php echo DIR; ?>my-jobs.php"><i class="fa fa-suitcase"></i> MY JOBS (<?php echo $jobs; ?>) </a>
        <a href="<?php echo DIR; ?>applied-jobs.php"><i class="fa fa-check-square-o"></i> APPLIED JOBS </a>     
        <a href="<?php echo DIR; ?>viewed-jobs.php"><i class="fa fa-eye"></i> VIEWED JOBS </a>
        <a href="<?php echo DIR; ?>logout.php"><i class="fa  fa-power-off"></i> LOGOUT </a>
      </div>
      </div>
    <form class="form" id="form_abc" action="">
        <div class="switch-field">
          <div class="switch-title">Make Resume Public?</div>

          <!-- Here you need to get the public field from database on load -->
          <input type="radio" id="switch" name="switch_2" value="yes" <?php echo ($public =='Yes') ? 'checked' : ''; ?>/>

          <label for="switch_left">Yes</label>

          <!-- Here you need to get the public field from database on load -->
          <input type="radio" id="switch" name="switch_2" value="no" <?php echo ($public =='No') ? 'checked' : ''; ?> />
          <label for="switch_right">No</label>
        </div>
    </form>  

<Script>
$("#switch").on('click',function(){
// see is this alert comes?
alert('Its working');
$("#form_abc").submit();
});
</Script>
Comments