zmogusnamas
zmogusnamas

Reputation: 61

Radio-buttons to change queries in php

I'm trying to make a multi-lingual dictionary with one main language. I would like to use radio buttons to change languages (and the query in php) My idea is to have for example 3 radio-buttons: -English, -German, -Spanish. That is to change languages/queries.

Now I have such a code (in index.php):

<script type='text/javascript'>
$(function(){
    $("form#search-form").submit(function(){
        $("#results").fadeOut();
        $.ajax({
            type:"GET",
            data: $(this).serialize(),
            url: "search.php",
            success: function(msg)
                {
                $("#results").html(msg);
                $("#results").fadeIn();
                }
        });
    return false;
    });

});
</script>

That was jquery script to get instantly results. And the search form in div:

<div id='container'>
<div id='search-box'>
    <form id='search-form'>
        <input type='text' id='input-text' name='s' value=''/>
    </form>
</div>

In search.php:

$q = "SELECT * FROM lauk_pamed WHERE diakr LIKE '%".$search_term. "%'or mask LIKE '%" .$search_term. "%' ORDER BY words LIMIT 21";
$r = mysql_query($q);


     if(mysql_num_rows($r)==0)//no result found
    {
    echo "<div id='search-status'>Nothing was found</div>";
    }
 else //result found
    {
    echo "<ul>";
    while($row = mysql_fetch_assoc($r))
        {
        $prus = str_ireplace($search_term, "".$search_term."", $row['wirds']);
        $des = $row['descr'];
?>

So my idea is: if -English is on, than query must be:

    $q = "SELECT * FROM lauk_pamed WHERE diakr LIKE '%".$search_term. "%'or english LIKE '%" .$search_term. "%' ORDER BY words LIMIT 21";

if -German is on, than query must be:

    $q = "SELECT * FROM lauk_pamed WHERE diakr LIKE '%".$search_term. "%'or german LIKE '%" .$search_term. "%' ORDER BY words LIMIT 21";

And so on. How could I do it using radio-buttons?

Upvotes: 1

Views: 2290

Answers (2)

Michael Lynch
Michael Lynch

Reputation: 1743

In your HTML do something like this:

<div id='search-box'>
    <form id='search-form'>
        Translate this:
        <input type='text' id='input-text' name='translate' value=''/>

        <br/><br/><br/>

        Into:<br/>
        <input type="radio" name="language" value="English"> English<br>
        <input type="radio" name="language" value="Spanish"> Spanish<br>
        <input type="radio" name="language" value="German" checked> German

        <br/><br/><br/>

        <button type="submit">Submit</button>
    </form>
</div>​​​​

And then in PHP you can get the string and the language like this:

<?php
    $search_string = $_GET['translate'];
    $language      = $_GET['language'];

    // Rest of your code...

If you want the form to auto submit when you click on a different language you can do this in your JS:

$('input[name=language]').change(function() {
    $('#search-form').submit();
});

Upvotes: 1

J A
J A

Reputation: 1766

HTML:

<input id="radioBtn" type="radio" name="radioButton" value="english">

JS:

$("#radioBtn").onChecked{

}

Upvotes: 2

Related Questions