2015-06-29 14 views
6

Ciao ho una pagina che permette una vista User risultati per un determinato torneo e rotondemodulo presentando con AJAX e PHP

enter image description here

utente selezionerà sport allora torneo è popolato basa sulla selezione sport allora UTENTE selezionare rotonda che è popolato in base alla selezione del torneo

Quando tutto è fatto utente preme il pulsante che cercare i risultati per il risultato sulla base di torneo e rotondo selezionato Invia

M codice y funziona grande:

mainPage.php

<script type="text/javascript"> 
$(document).ready(function() 
{ 
$(".sport").change(function() 
{ 
    var id=$(this).val(); 
    var dataString = 'id='+ id; 

    $.ajax 
    ({ 
    type: "POST", 
    url: "get_sport.php", 
    dataType : 'html', 
    data: dataString, 
    cache: false, 
    success: function(html) 
    { 
     $(".tournament").html(html); 
    } 
    }); 
    }); 


$(".tournament").change(function() 
{ 
    var id=$(this).val(); 
    var dataString = 'id='+ id; 

    $.ajax 
    ({ 
    type: "POST", 
    url: "get_round.php", 
    data: dataString, 
    cache: false, 
    success: function(html) 
    { 
    $(".round").html(html); 
    } 
    }); 
    }); 

}); 
</script> 

get_sport.php

<label>Sport :</label> 
<form method="post"> 
<select name="sport" class="sport"> 
<option selected="selected">--Select Sport--</option> 
<?php 
$sql="SELECT distinct sport_type FROM events"; 
$result=mysql_query($sql); 
while($row=mysql_fetch_array($result)) 
{ 
    ?> 
     <option value="<?php echo $row['sport_type']; ?>"><?php echo $row['sport_type']; ?></option> 
     <?php 
} 
?> 
</select> 

<label>Tournamet :</label> <select name="tournament" class="tournament"> 
<option selected="selected">--Select Tournament--</option> 
</select> 


<label>Round :</label> <select name="round" class="round"> 
<option selected="selected">--Select Round--</option> 
</select> 
<input type="submit" value="View Picks" name="submit" /> 
</form> 

get_round.php

if($_POST['id']) 
{ 
    $id=$_POST['id']; 
    $sql="SELECT DISTINCT round FROM events WHERE tournament='$id'"; 
    $result=mysql_query($sql); 
    ?> 
    <option selected="selected">Select Round</option><?php 
    while($row=mysql_fetch_array($result)){ 
    ?> 
    <option value="<?php echo $row['round'] ?>"><?php echo $row['round'] ?></option> 
    <?php 

    } 
} 
?> 

ESEMPIO

Sport => Calcio; Torneo => EPL; Giro => 5;

Supponendo che quanto sopra è selezionata quando l'utente fa clic presentare il codice interrogherà select results from someTable Where sport='Football' AND...

mio problema

ottengo i dati delle caselle di selezione utilizzando un semplice php isset() funzione

if(isset($_POST['submit'])){ 
    echo $sport=$_POST['sport']; 
    echo $tour=$_POST['tournament']; 
    echo $round=$_POST['round']; 
    : 
    : 

Ora il mio problema è quando inviare si fa clic su tutto funziona MA MA il modulo si ottiene eloaded, che è quello che non voglio

Im che cerca un equivalente AJAX di isset() o un modo per i dati da trasmettere senza la forma ricaricare

Qualsiasi idee/aiuto notevolmente sarà apprezzato

+0

_ “Im alla ricerca di un equivalente AJAX di isset() "_ - non ha senso. – CBroe

risposta

1

Esistono diversi modi per evitare il ricaricamento di un modulo di invio.

Una soluzione potrebbe essere quella di gestire l'azione presentare la forma e tornare 'false' (Example here e here) o prevenire l'azione di default (Example here)

Si può anche sostituire il tipo di ingresso presentare per un tipo di input pulsante (o pulsante) e gestire l'azione del pulsante clic anziché gestire l'azione di invio del modulo. Ciò rappresenterebbe una soluzione semplice per la maggior parte dei problemi di "invio di moduli", ma è una soluzione peggiore nel punto di vista del codice semantico e valido.

0

È possibile eseguire l'invio del modulo da JQuery come richiesta AJAX e ottenere il successo.

+0

Questo metodo è deprecato, dovresti usare event.preventDefault(); anziché. – enguerranws

+0

Grazie a @enguerranws. Aggiornato .. – Yasitha

0

Inizialmente caricare tutti i valori in Sport: discesa

quindi popolare dinamicamente la Tournament e Round

// To Trigger Sport Change 
$(".sport").change(function() { 

    var selected_sport = $(".sport").val(); 
    var dataString = 'sport=' + selected_sport; 
    var urlAddress = "get_sport.php"; 
    $.ajax({ 
     url: urlAddress, 
     cache: false, 
     method: 'post', 
     data: dataString, 
     dataType: 'html', 
     success: function (result_data) { 
      $(".tournament").html(result_data); 
      // This will append the tournament drop-down dynamically 
     } 
    }); 
}); 

// To Trigger Tournament Change 
$(".tournament").change(function() { 

    var selected_sport = $(".sport").val(); 
    var selected_tournament = $(".tournament").val(); 

    var dataString = 'sport=' + selected_sport + '&tournament=' + selected_tournament; 
    var urlAddress = "get_round.php"; 
    $.ajax({ 
     url: urlAddress, 
     cache: false, 
     method: 'post', 
     data: dataString, 
     dataType: 'html', 
     success: function (result_data) { 
      $(".round").html(result_data); 
     } 
    }); 
}); 

in PHP corrispondente get_round.php

while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) { 
    foreach ($row as $r) { 
     $round = $r['round']; 
     echo '<option value = "' . $round . '">' . $round . '</option>'; 
    } 
} 
+1

Shadab, grazie di non provarlo e farti sapere come funziona ... Grazie per aver risposto –

+0

@TimothyCoetzee, ha funzionato? –

+0

no ... so come popolare gli elenchi a discesa ho bisogno di interrogare il mio database una volta che tutti i valori (in tutte e 3 le selectbox) sono selezionati –