I have this code that is to fetch a particular item in the database after the user presses a button. If the item is found I would like to display a confirm message via javascript and I don't know how to show it.
After obtaining the item from the database
if(null!=mysqli_fetch_array($res)))
{
echo ""; // how can I call the javascript code here ?
}
and the following confirm box
<script type="text/javascript">
function onConfirm()
{
return confirm("Display this item ?");
}
</script>
Use jquery and ajax to get the value from the database:
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
$.get("ajax.php", function(data){
if(data != "0"){
if(confirm("Display this item ?"))
{
//do something
}
}
});
ajax.php:
//other db code here
if(null!=mysqli_fetch_array($res)))
{
echo "1";
}
else{
echo "0";
}
There are number of ways you can solve this. A good way is to:
Send an ajax request to a page
The page does the mysql stuff and returns(echo in case of PHP) the message
On the callback function use simple way such as alert() to show the message.
Related
I'm trying to make a simple registration page that makes the user enter the username and email.
The problem is how can I check if the input values(email)already exists in the mysql when I press the submit button without going to the next page instantly ? if the input value doesn't exist in the mysql database I want to display a message like "email not registered". I'm trying to use ajax,jquery, and php but I cant find a decent solution.
//this is the script to check if the emails that the users entered matches.
//I'm trying to post the values to the'checkPage.php' to check if the email exists
//The problem is how can I on move to the next page after the result have been returned?
Sorry for my bad explanation.
<script>
$('input#submitbutton').on('click',function(){
var mail=$('input#mail').val();
var mail2=$('input#mail2').val();
if($.trim(mail)===$.trim(mail2)){
$.post('checkPage.php',{mail:mail},function(data){
$('#name-data').text(data); // displays the result if the email exists or not
});
}else{
}
});enter code here
</script>
//CheckPage.php
//I want the registration page to go to the next page only if the email
//haven't been found in the mysql database.
<?php
if(isset($_POST['mail'])&&isset($_POST['mail2'])){
$mail = $_POST['mail'];
$mail2 = $_POST['mail2'];
try{
$con = mysql_connect("localhost","root",""); echo "connected";
$db = mysql_select_db("db",$con);
$query = mysql_query("select email,id from user where email ='". mysql_real_escape_string(trim($mail))."'",$con);
echo (mysql_num_rows($query)!==0) ? mysql_result($query,0,'email'):'none';
} catch (Exception $ex) {
}
}
?>
*/
you can use this;
$('input#submitbutton').on('click',function(event){
event.preventDefault();
var mail=$('input#mail').val();
var mail2=$('input#mail2').val();
if($.trim(mail)===$.trim(mail2)){
$.post('checkPage.php',{mail:mail},function(data){
$('#name-data').text(data); // displays the result if the email exists or not
if (data !== "none") {
$('#your_form_id').submit();
}
});
}else{
}
});
First of all on submit button make an ajax call as follows
<script>
function myformsubmit()
{
var mail=$('#mail').val();
$.post( "check.php", { mail: mail})
.done(function( data ) {
if(msg == 'error')
{
$('#diverr').html('Match found');
return false;
}
else
{
//redirect to a valid page
}
});
}
In the check.php you will have to get your data by post make an sql select query loop thru the mail ids returned by the query and if there is a match found return 'error' or return blank
try to put your javascript code in $(document).ready();
$(document).ready(function() {
//your code here
$('input#submitbutton').on('click',function(){
var mail=$('input#mail').val();
var mail2=$('input#mail2').val();
if($.trim(mail)===$.trim(mail2)){
$.post('checkPage.php',{mail:mail},function(data){
$('#name-data').text(data); // displays the result if the email exists or not
});
}else{
}
});e
});
I'm trying to get a jQuery script to update the content of an HTML paragraph tag variously according to whether or not a PHP if statement, which checks for data in a database, evaluates to true or not:
if ($num_rows > 0) { ?> // If results are returned from database...
<script>
$(document).ready(function() {
$('p.message').text('Some data'); // ... p should read 'Some data'
});
</script>
<?php else { // otherwise
<script>
$(document).ready(function() {
$('p.message').text('No data'); // ...p should read 'No data'
});
</script>
}
The contents of are intended to change dynamically without the need for a page refresh, but do not do so. Only after the page has been refreshed is the new value shown.
Any ideas as always much appreciated.
Use AJAX to get the content from PHP. You can have the success populate the data into your HTML.
What You are trying to do can be achived through Ajax. Belwo Is a simple Example
Jquery
setInterval(function(){UpdateMessage()},10000) // Will Eecute after 10 Seconds
function UpdateMessage()
{
$.ajax({
type:"POST",
url:"Script.php",
success: function(data)
{
$(".message").text(data);
}
});//END OF Second AJAX
}
HTML
<p class='message'></p>
Script.php
if ($num_rows > 0)
echo "SOME DATA";
else
echo "No Record";
above code will refresh Your Data every after 10 Seconds Without Refresh
did you try include jquery into that php file? like
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
hi i have a message alert box that appears when a user gets a new message, what i want to do is add sound to this box when it pops up, i am using a php if statement to check when a user gets a new message and i have tried adding sound by doing the following but its not working please can someone show me how to do this. thanks.
<?php
$check_new_chats = check_new_chats();
while ($chat = mysql_fetch_array($check_new_chats))
if (isset($_SESSION['user_id'])) {
if ($chat['to_user_id'] == $_SESSION['user_id']){
echo( "<embed name='sound_file' src='/assets/music/sound_file.mp3' loop='true' hidden='true' autostart='true'/>"); ?>
<?php
$check_new_chats = check_new_chats();
while ($chat = mysql_fetch_array($check_new_chats))
if (isset($_SESSION['user_id'])) {
if($chat['to_user_id'] == $_SESSION['user_id']){
echo '<script type="text/javascript">play_sound();</script>';
}
}
?>
Here's the Javascript function:
<script type="text/javascript">
function play_sound() {
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', '/assets/music/sound_file.mp3');
audioElement.setAttribute('autoplay', 'autoplay');
audioElement.load();
audioElement.play();
}
</script>
According to the question:
hi i have a message alert box that appears when a user gets a new
message, what i want to do is add sound to this box when it pops up
You already have some sort of javascript function that displays an alert box when needed. Use the info from this answer to play a sound with it.
<audio id="soundHandle" style="display: none;"></audio>
<script>
soundHandle = document.getElementById('soundHandle');
soundHandle.src = '/assets/music/sound_file.mp3';
</script>
//With your message alert function....
alert("Message box");
soundHandle.play();
I am getting data from an MySQL database through PHP. I am sending the and getting the data from PHP using jQuery. Here is the code.
$.POST("SubmitCode.php", $("#questionCodeForm").serialize(),'json').done(function(data) {});
Now the problem is that once I send this data the page refreshes. How can I stop the page refresh. If I delete 'json' then the page stops refreshing but the problem is that I want to get the json data without page refresh. How can I do this?
-------------------------------------------after edit-----------------------------------------------------------------------------
Here is the updated code
$(document).ready(function() {
initialization();
codeSubmission();
});
function initialization() {
$("#answerForm").hide();
}
function codeSubmission() {
$("#submitButton").click(function(e) {
e.preventDefault();
$.post("SubmitCode.php", $("#questionCodeForm").serialize()).done(function(data) {
var questionName = data.questionName,
options = data.options,
pollingStatus = data.pollingStatus,
codeExist = data.codeExist;
alert(data);
alert(data[1]);
alert(questionName);
alert(options);
if(codeExist == true) {
$("#questionTitle").text("questionName");
for(rowNum=1;rowNum<=5;rowNum++) {
$("#checkbox-"+rowNum).val("Answer1");
$("#checkbox"+rowNum+"label").text("Answer"+rowNum);
}
$("#answerForm").slideDown(500);
} else if(codeExist == false) {
alert("This quiz code is invalid");
}
},'json');
//return false;
});
//return false;
}
Now the problem is that the output of alert(questionName) is undefined. The data is passed as a string. How do I get the correct information in the correct variables?
Try this instead: (note the placement of the callback function, and lowercase .post method)
$.post("SubmitCode.php", $("#questionCodeForm").serialize(),function(data) {
//manipulate your data here
},'json');
Also make sure that whatever is triggering the post isn't an actual link and if it is, you need to stop the default action from occuring. For example:
Click here to submit
javascript:
$(a).click(function(e) {
e.preventDefault();
$.post("SubmitCode.php", $("#questionCodeForm").serialize(),function(data) {
//manipulate your data here
},'json');
});
You also have to parse the json on the client side. You can do this using
obj = jQuery.parseJSON(data);
i have made a login form on a light box using a javascript function. Now i want to store a value on session variable, so to check if the user has logined , and not to show him login lightbox again and again on his navigations on the page.. My javascript function is:
<script language="javascript" type="text/javascript">
function createlightbox()
{
document.getElementById('light').style.display='block';
document.getElementById('fade').style.display='block'
}
function closelightbox()
{
document.getElementById('light').style.display='none';
document.getElementById('fade').style.display='none'
}
function checksession()
{ if (admin=="admin")
{closelightbox();}
else
{createlightbox();}
}
function check(form)/*function to check userid & password*/
{
/*the following code checkes whether the entered userid and password are matching*/
if(form.name.value == "admin" && form.password.value == "admin")
{
closelightbox();
var admin = <?php $_SESSION['Admin']= 1; ?>
}
else
{
document.getElementById("error").style.display='block';/*displays error message*/
}
}
</script>
And i m calling the checksession function in my forms onsubmit event as
<form id="Admin" onreset="checksession()">
The problem is, on every reset or submit of form, even on the page changes, the login form is shown. Why it is not checking the check session function.
Please tell me any fault i m making
i'm not sure where your conditions are.
but the following code should present in php script that generates your lightbox:
<?php echo '<script> var admin ='.$_SESSION['Admin'].'</script>'; ?>
(to check above is working correctly, you could View source code of your page and see if there is a line like: <script> var admin =1</script>)
the following should be before you access admin variable setted above:
<script language="javascript" type="text/javascript">
.... //other code
function checksession()
{ if(admin =="admin")
{closelightbox();}
else
{createlightbox();}
}
....
also note that if statement should compare == not assign =