How to make bootable functionality snippet change value in MySQL

I am trying to add a poll / poll panel

in the sidebar of my website HTML

/ PHP

/ MySQL

.

Scenario I have a list of plans for individual locations in the MySQL

Database Table, when some search for a location they will also see a list of plans selected for that location. They can vote on which plan will be executed next in this place.

What have I been working on so far? I can get plans for specific locations when viewers are looking for a location. Used code:

<div class="panel panel-default">
<div class="panel-heading">
    <h3 class="panel-title">
        Vote next plan
    </h3>
</div>
<div id="voting">
    <form>
        <div class="panel-body">                                
           <ul class="list-group">
                <?php
                    while ($planvote = mysql_fetch_array($planresultv)) {
                        echo '<li class="list-group-item">';
                        echo '<div class="radio">';
                        echo '<label>';
                        echo '<input type="radio" name="optionsRadios">';
                            echo $planvote['plan_name'];                                                
                        echo '</label>';
                        echo '</div>';
                        echo '</li>';
                    }
                ?>
            </ul>
        </div>
        <div class="panel-footer">
            <button type="button" class="btn btn-primary btn-sm">
                Vote</button>
            <a href="#">View Result</a>
        </div>
    </form>
</div>

      

Screenshot enter image description here

Now I have a database table

with columns

|sn|plan_name|poll|
|1 |Path way |0   |

      

How can I add / change the value poll

in some cases when choosing radio button

when voting form

and voting.

PS You can answer the ideas or help me with the code if you like.

+3


source to share


2 answers


You can add a value to you:

echo '<input type="radio" name="optionsRadios">';

      

After that you can make an ajax call where you will update the results with the radioblock value. Don't forget to add an id to your button:

<button id='vote_button' type="button" class="btn btn-primary btn-sm">

      

Ajax call:



$("#vote_button").click(function(){
    if($("input[type='radio'][name='optionsRadios']:checked").length > 0){
        var chosenvalue = $("input[type='radio'][name='optionsRadios']:checked").val();
        var dataString = 'paramX=' + chosenvalue;
        $.ajax({
            type: "POST",
            url: "yourfile.php",
            data: dataString,
            cache: false,
            success: function (html) {
            }
        });
    }
});

      

In your ajax call, you can update the voting system using some PHP code and SQL, for example, where $ val = the passed value for your ajax:

UPDATE your_table SET poll = poll + 1 WHERE sn = $val

      

But you probably have to add a lot more code than that, otherwise people might just spam the voting system.

+1


source


Decision

JS / Ajax Script

<script src="js/jquery-latest.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
$("#vote_button").click(function(){
if($("input[type='radio'][name='optionsRadios']:checked").length > 0){
var chosenvalue = $("input[type='radio'][name='optionsRadios']:checked").val();
var dataString = 'paramX=' + chosenvalue;

$.ajax({
    type: "POST",
    url: "yourfile.php",
    data: dataString,
    cache: false,
    success: function (html) {
        console.log(html);
        $('.notice').html(html);

    },
    error: function(err) {
        console.log(err);
    }
});
}
});

      

HTML / PHP



<div id="voting">
<form>
    <div class="panel-body">                                
       <ul class="list-group">
            <?php
                while ($planvote = mysql_fetch_array($planresultv)) {
                    echo '<li class="list-group-item">';
                    echo '<div class="radio">';
                    echo '<label>';
                    echo '<input type="radio" name="optionsRadios" value="'.$planvote['sn'].'">';
                        echo $planvote['vdc_PlName'];                                               
                    echo '</label>';
                    echo '</div>';
                    echo '</li>';
                }
            ?>
        </ul>
    </div>
    <div class="panel-footer">
        <button id="vote_button" type="button" class="btn btn-primary btn-sm">
            Vote</button>
        <a href="#">View Result</a>
    </div>
</form>

      

Finally, it updates the poll value in the database table.

0


source







All Articles