Getting data from a php file using Javascript and Ajax -
hi trying create survey personal project working on teach self web development. survey f1 website , want able ask users 5 questions , based on there answers want able tell them grand prix best suited them.
my questions users are:
what grand prix’s closest you? (i have given them option select 3 grand prix’s suing select boxes)
what countries visit? (i have given them option select 2 countries suing select boxes)
when available go grand prix (i have given them option select 3 months suing select boxes)
what favorite team? (one option using select box)
who favorite driver? (one option using select box)
so far have created database tables countries, grand prix’s, months, teams , drives.
i have survey.php file populates select boxes database.
so question how create javascript file link php file populates select boxes input user , display result out refreshing page using ajax?
my survey.php <?php $link = mysqli_connect("localhost","student","student"); mysqli_select_db($link,"gpdb"); //question 1 $query = "select * gp"; $result = mysqli_query($link,$query); $row = mysqli_fetch_assoc($result); ?> <div class="circuits"> <h1>which grand prix's closest you<h1> <form action="<?php $_server['php_self']; ?>" method="get" > <p>option 1</p> <select name="grandprix"> <?php { ?> <option value="<?php echo $row['circuits']; ?>"><?php echo $row['circuits']; ?></option> <?php } while ($row = mysqli_fetch_assoc($result)); ?> </select> <?php $query = "select * gp"; $result = mysqli_query($link,$query); $row = mysqli_fetch_assoc($result); ?> <p>option 2</p> <select name="grandprix2"> <?php { ?> <option value="<?php echo $row['circuits']; ?>"><?php echo $row['circuits']; ?></option> <?php } while ($row = mysqli_fetch_assoc($result)); ?> </select> <?php $query = "select * gp"; $result = mysqli_query($link,$query); $row = mysqli_fetch_assoc($result); ?> <p>option 3</p> <select name="grandprix3"> <?php { ?> <option value="<?php echo $row['circuits']; ?>"><?php echo $row['circuits']; ?></option> <?php } while ($row = mysqli_fetch_assoc($result)); ?> </select><br /> </form> </div> <?php $query = "select * place"; $result = mysqli_query($link,$query); $row = mysqli_fetch_assoc($result); ?> <div class="country" <h1>what countries visit?</h1> <p>country 1</p> <select name="country1"> <?php { ?> <option value="<?php echo $row['location']; ?>"><?php echo $row['location']; ?></option> <?php } while ($row = mysqli_fetch_assoc($result)); ?> </select> <?php $query = "select * place"; $result = mysqli_query($link,$query); $row = mysqli_fetch_assoc($result); ?> <p>country 2</p> <select name="country1"> <?php { ?> <option value="<?php echo $row['location']; ?>"><?php echo $row['location']; ?></option> <?php } while ($row = mysqli_fetch_assoc($result)); ?> </select> </div> <?php $query = "select * month"; $result = mysqli_query($link,$query); $row = mysqli_fetch_assoc($result); ?> <div class="month"> <h1>when available go grand prix</h1> <p>month 1</p> <select name="month"> <?php { ?> <option value="<?php echo $row['monthofgp']; ?>"><?php echo $row['location']; ?></option> <?php } while ($row = mysqli_fetch_assoc($result)); ?> </select> <?php $query = "select * month"; $result = mysqli_query($link,$query); $row = mysqli_fetch_assoc($result); ?> <p>month 2</p> <select name="month"> <?php { ?> <option value="<?php echo $row['monthofgp']; ?>"><?php echo $row['monthofgp']; ?></option> <?php } while ($row = mysqli_fetch_assoc($result)); ?> </select> <?php $query = "select * month"; $result = mysqli_query($link,$query); $row = mysqli_fetch_assoc($result); ?> <p>month 3</p> <select name="month"> <?php { ?> <option value="<?php echo $row['monthofgp']; ?>"><?php echo $row['monthofgp']; ?></option> <?php } while ($row = mysqli_fetch_assoc($result)); ?> </select><br /> </div> <div class="driver"> <?php $query = "select * teamname"; $result = mysqli_query($link,$query); $row = mysqli_fetch_assoc($result); ?> <h1>what favourite team?</h1> <p>team</p> <select name="taem"> <?php { ?> <option value="<?php echo $row['team']; ?>"><?php echo $row['team']; ?></option> <?php } while ($row = mysqli_fetch_assoc($result)); ?> </select><br /> </div> <div class="driver"> <?php $query = "select * drivername"; $result = mysqli_query($link,$query); $row = mysqli_fetch_assoc($result); ?> <h1>who favourite driver?</h1> <p>driver</p> <select name="taem"> <?php { ?> <option value="<?php echo $row['driver']; ?>"><?php echo $row['driver']; ?></option> <?php } while ($row = mysqli_fetch_assoc($result)); ?> </select><br /> </div> <input type="submit" /> <input type="reset" /> </form>
use jquery validation validate form... simple hard explain me.
for without post page save data... 1. make php function save data.. 2. call function using ajax/jquery..
sample code here...
$.ajax({ type: 'get', url: "your function path/url", crossdomain: 'true', success: function (data){ // here alert function response.. alert(data); } });
Comments
Post a Comment