Fancybox script POST my form data to go.php page then open go.php in fancybox iframe
<script>
$(document).ready(function() {
$("#fancybox-manual-b").click(function() {
$.post('go.php', $(this).parent().serialize())
.done(function() {
$.fancybox.open({
href : 'go.php',
type : 'iframe',
padding : 5
});
});
});
});
</script>
<select name="country">
<option value="US">US</option>
<option value="EU">EU</option>
</select>
<input type="button" value="Calculate" id="fancybox-manual-b"/>
in go.php, I receive the POST data from the form correctly and when I try to insert this data into DB, it's been inserted correctly too!
But now I want to select * from my DB table to echo all the data in the table where column = POST data, but this query doesn't work!
<?php
$country = $_POST[country];
mysql_query("INSERT INTO calculator (country) VALUES ('$country')"); //Works Correctly
$result = mysql_query("SELECT * FROM calculator WHERE country='$country' ORDER BY ID ASC");
while($row = mysql_fetch_array($result)){
echo $row['ID']." ".$row['country'];
} //Nothing appear
?>
Please checkout this method,
$country = $_POST[country];
if(mysql_query("INSERT INTO calculator (country) VALUES ('$country')"));
{
$result = mysql_query("SELECT * FROM calculator WHERE country='$country' ORDER BY ID ASC");
while($row = mysql_fetch_array($result)){
echo $row['ID']." ".$row['country'];
}
}
I used the same code you shared. But added an additional if loop on mysql_query part. It will make sure your select works after data being inserted.
We have to use jquery ajax to send form data to the php page with POST method then receive back any printed data from the php page in the ajax instead of the console.
We will open the fancybox iframe into ajax success instead of the console.
<script type="text/javascript">
$(document).ready(function() {
$("#fancybox-manual-b").click(function() {
$.ajax({
type: 'POST',
url: 'go.php',
data: $(this).parent().serialize(),
success: function(data){
$.fancybox(data);
},
fail:function(data){console.info(data)}
});
});
});
</script>
Related
Is that possible to select variable in DB (such as : $username) and display their info in textbox ?
Example:
Select : $username
after the selection, in the text input , will show:
$username , $usernameEmail , $usernameContactNumber
and click on "Register", it will insert to database with the information
I have no idea how does this work. I have no problem in inserting value to database.
The only problem that I am unable to solve right now is the condition I needed.
When I choose $username (example: userA),
all the information of userA will be appearing in the text input below
$query = "SELECT * FROM userdatabase";
$result1 = mysqli_query($db,$query);
<select>
<?php while($row1 = mysqli_fetch_array($result1)):; ?>
<option value="<?php echo $row1[0];?>"><?php echo $row1[2];?></option>
<?php endwhile;?>
</select>
The code above is the selection (PHP
<script>
$(document).ready(function(){
$("form#get").submit(function(event) {
event.preventDefault();
var input = $("#username");
$.ajax({
type: "POST",
url: "userregisteraccount.php",
success: function(data) { input.val(data); }
});
});
});
</script>
code above is the ajax
so that when i choose "userName" , "userName" data will be show in the registerform
I am just learning Ajax and am trying to update a mysql select query to filter results. I think the query is working fine, but not sure that I have the event working correctly.
Here is my HTML with the selection;
<div class="volunteers">
<h1>Volunteers</h1>
<div class="volunteerSelection">
<select id="vtSelect" name="volunteerS">
<option value="1">Comittee</option>
<option value="2">Day of Event</option>
</select>
</div>
<div id="volunteers">
</div>
</div>
This is the script that displays the data;
I have updated this to the suggestion and it works fine. Then I started working on my PHP and mysql query. See below;
$(document).ready(function(){
$('#vtSelect').change(function (e) {
e.preventDefault();
var selectedOption = $(this).find('option:selected');
$('#vtSelect option').removeAttr('selected');
$(selectedOption).attr('selected', 'selected');
var selectedOptionValue = $(selectedOption).val();
$.ajax({
type: "POST",
url: "includes/backDataProcess.php",
data: {
data: selectedOptionValue
},
success: function (data) {
$("#volunteers").html(data);
}
});
});
});
This is the PHP with the select query; I have update this and am getting data. I updated the name in the select tag, but I am not getting the select tag value in the query. When I comment out the post data and just fill in a value it posts fine. I am not getting any errors, it is just displaying 0 results.
<?php
include('readerConnect.php');
$sql = "SELECT * FROM contacts prc
JOIN states as st
ON (prc.stateId = st.idStates)
INNER JOIN volunteers as v
ON (prc.idContacts = v.contactId)
INNER JOIN volunteerType as vt
ON (v.volunteerTypeId = vt.idVolunteerType)
WHERE (volunteerTypeId = 1 /*`mysql_real_escape_string('$_POST[volunteerS]')`*/)";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo
"<div class='contacts'>
<div class='contactName'>" . $row["firstName"] ." " . $row["lastName"] ."</div>"
."<div class='contactAddress'>" .$row["address1"] ." " .$row["address2"] ."<br>"
.$row["city"] ." " .$row["state"] ." " .$row["zip"] ."</div>"
."<div class='contactVolunteer'>" .$row["volunteerTypeId"]
."</div>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I am not sure exactly where the issue is, I have tried both change and onchange to update, do I need an update button? I thought jquery would be able to do this with no update. Any help is greatly appreciated.
There are several obvious problems in your jQuery code. If you open your browser console you should see error onChange is not a function since there is no such jQuery method. You want change() or on()
Also you create a function showVolunteers() but it never gets called.
try:
$('#vtSelect').change(function (e) {
e.preventDefault();
var selectedOption = $(this).find('option:selected');
$('#vtSelect option').removeAttr('selected');
$(selectedOption).attr('selected', 'selected');
var selectedOptionValue = $(selectedOption).val();
$.ajax({
type: "POST",
url: "includes/backDataProcess.php",
data: {
data: selectedOptionValue
},
success: function (data) {
$("#volunteers").html(data);
}
});
});
Should you encounter more problems, use browser console network tab to inspect ajax request, and always use console to check for errors
In a file following code is written which populate the value of combobox :
<select id="company" required="required" class="form-control" value = "select" name="subject_code" placeholder="Select" >
<?php
//Iterating list of subjects available to be filled .
echo "<option> Select </option>";
foreach ($subjects_to_fill as $subject_id => $subject_name) {
# code...
echo "<option value=".$subject_id."> ".$subject_name." </option>";
}
?>
</select>
On selecting a particular item from the combobox, I want to display the faculty_name dynamically from faculty_table on the basis of $subject_id.
table structure:
faculty_table(faculty_id,faculty_name,subject_id)
subject_table(subject_id,subject_name,faculty_id)
You will need to use ajax for this task.
Add a <div> and below script in your current file.
<div id="faculty"></div>
<script type="text/javascript">
function get_faculty()
{
var id = document.getElementById("company").value;
var dataString = 'id='+ id;
$.ajax
({
type: "POST",
url: "get_faculty.php",
data: dataString,
success: function(html)
{
$("#faculty").html(html);
}
});
}
</script>
Now create another get_faculty.php file in same folder which will be called on onchange event of company dropdown.
Write below code in that file
if($_POST['id'])
{
$id=$_POST['id'];
$con=mysqli_connect("localhost","username","pass","dbname");
$sql=mysqli_query($con,"SELECT faculty_name from faculty_table where subject_id = ".$id);
while($row=mysqli_fetch_array($sql))
{
echo $row['faculty_name'];
}
}
Dont forget to write mysqli_connect credentials and query accordingly.
I am looking to get the selected field from a dropdown box in order to use it in a future dropdown box, but I can't figure out how to echo the variable through the ajax back to the html.
<p>Trainer</p>
<select name = "trainer_has_update_pokemon">
<option>Select Trainer</option>
<?php
$query = "SELECT name FROM Trainer";
if ($stmt = $mysqli->prepare($query)) {
$stmt->execute();
$stmt->bind_result($name);
while ($stmt->fetch()) {
echo"<option>$name</option>";
}
$stmt->close();
}
?>
</select>
Pokemon
<select name = "type_of_update_pokemon">
</select>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function(){
$('select[name="trainer_has_update_pokemon"]').change(function(){ // when trainer_has_update_pokemon changes
$.ajax({
type:"POST", //send a post method
url:'pkmn_dropdown.php', // path to ajax page
data:"trainer_name="+$(this).val(), //set trainer_name to value
success:function(response){ // retrieve response from php
$('select[name="type_of_update_pokemon"]').html(response); // update select
}
});
});
});
</script>
<select name="nickname_of_update_pokemon">
</select>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function(){
$('select[name="type_of_update_pokemon"]').change(function(){ // when trainer_has_update_pokemon changes
$.ajax({
type:"POST", //send a post method
url:'nickname_dropdown.php', // path to ajax page
data:"pkmn_name="+$(this).val() & "trainer_name=" +$trainer_name,//set trainer_name to value
success:function(response){ // retrieve response from php
$('select[name="nickname_of_update_pokemon"]').html(response); // update select
}
});
});
});
</script>
the php for the name dropdown:
<?php
//connect to db
$trainer_name = $_POST['trainer_name'];
$query = "SELECT DISTINCT p.name FROM Pokemon p WHERE p.owner_id = (SELECT t.trainer_id FROM Trainer t WHERE t.name = '$trainer_name')";
if ($stmt = $mysqli->prepare($query)) {
$stmt->execute();
$stmt->bind_result($pkmn_name);
while ($stmt->fetch()) {
echo"<option>$pkmn_name</option>";
}
$stmt->close();
echo $trainer_name;
}?>
The php for the nickname dropdown:
<?php
//connect to db
$pkmn_name = $_POST['pkmn_name'];
$query = "SELECT p.nickname FROM Pokemon p, Trainer t WHERE p.name = '$pkmn_name AND p.owner_id = t.trainer_id AND t.name = $trainer_name";
if ($stmt = $mysqli->prepare($query)) {
$stmt->execute();
$stmt->bind_result($nickname);
while ($stmt->fetch()) {
echo"<option>$nickname</option>";
}
$stmt->close();
}?>
Any ideas how to get the selected trainer name from the first dropdown box so i can use it in the nickname dropdown box
What exactly is happening when you run the above code? Does the nickname select lose all of it options? Does your ajax response contain the options string as expected?
Modifying the innerHTML of a select does not work in IE. You will need to add actual elements. Try something like this.
$( 'select[name="nickname_of_update_pokemon"]' ).empty().append( $( response ) );
In your ajax success function.
I have a page with multiple drag&drop boxes, that works great, the thing that does not work are the links in the boxes. I would appreciate if someone could help me :). So I have a page where people can drag&drop boxes (it works fine, as I said before), the links inside the boxes are sortable aswell, but I can't seem to get them to save the values to mysql. I think there is a conflict between the two drag&drops, maybe I am doing it wrong, 'cause I haven't used ajax and jquery before.
//here is the jquery where I need to add some ajax
$(function() {
$('.dragbox-content').sortable({
connectWith: '.dragbox-content',
update: function(event, ui) {
var order=$(this).attr('id');
alert(order); // I get the order alert and it has one value that I need, but I need the sort order aswell
}
});
});
//this is the <div> that has the links in them and mysql query that gets the values
//from two different databases, one is for the boxes and the other for links.
//boxes db id = links title_id
echo '<div class="dragbox-content" id="order'.$widget['id'].'"';'>''</div>';
$sql10 = "SELECT u.*, w.id, w.link_address, w.link_name FROM db_boxes u LEFT
JOIN db_links w ON u.link_id = w.id WHERE
(u.username = '$username' AND u.link_id !='0' AND w.title_id = '".$widget['id']."'
AND w.link_name !='pilt' AND w.rights = '0') OR
(u.username = '$username' AND u.link_id !='0' AND w.title_id = '".$widget['id']."'
AND w.link_name !='pilt' AND w.rights LIKE '%26%') ORDER BY w.id ASC";
$result10 = mysql_query($sql10) or die (mysql_error());
while ($row = mysql_fetch_array($result10)) {
$link_id = $row['id'];
$link_address = $row['link_address'];
$link_name = $row['link_name'];
$title_id = $row['title_id'];
?>
<div class="move" id="<?php echo $link_id;?>">
<span class="link_style">
<div><?php echo $link_name;?> </div</span></div>
I just need somebody to tell me how can I save tile_id and sort_order to boxes database with ajax on every click a user makes on that page
See my example below:
http://jsfiddle.net/gRoberts/vMy7r/
$(function () {
$('ul').sortable({
update : function(e, ui) {
var ul = $(ui.item).closest('ul');
var index = 0;
var toPost = {};
ul.find('> li').each(function() {
index++;
$(this).find('input').val(index);
toPost[$(this).find('input').attr('name')] = index;
});
$.ajax({
url : '/echo/json/',
data : toPost,
type : 'POST',
dataType : 'json',
success : function(resp) {
alert(resp);
},
error : function() {
alert('There was a problem');
}
});
}
});
});
The above example can be used in two ways, if you remove the $.ajax it will update hidden form fields which you can then post normally.