0

I want to make it so that new input boxes only appear if information in the previous box is correct, by checking it with a mysql_query can I adapt this code

function show(id){
  if(document.getElementById(id+"-link").style.display=="none") {
     document.getElementById(id+"-link").style.display="block";
     document.getElementById(id+"-input").style.display="block";
  }
}

<tr> 
  <td><input type="text" id="a1" name="em_1"></td>
  <td><a href="#null" onclick="show('b1')">and</a></td>
</tr><tr>
  <td><input type="text" id="b1-input" name="em_2" style="display:none"></td>
  <td><a href="#null" style="display:none" id="b1-link"</a></td>
</tr><tr>

so somewhere in here I need to add :

$userCheck=mysql_query(SELECT email FROM users WHERE name = "em_1")
$row=mysql_num_rows($usercheck)
if($row!==0){"show('b1')";}

or something to that affect, without refreshing the page. Is this possible?

8
  • with AJAX, yes but not without re-loading a page somewhere, as php runs server side, not client side. Commented Jan 8, 2013 at 21:45
  • 3
    Please do not use mysql_query in new applications. Commented Jan 8, 2013 at 21:47
  • @Stu I think you mean you need to reload the page sometimes if you don't use AJAX. Commented Jan 8, 2013 at 21:49
  • To extend on tadman's comment: the mysql_* functions are deprecated, use PDO or MySQLi now. Commented Jan 8, 2013 at 21:50
  • @11684 no, I mean you could update the values on the page without reloading it if you used ajax to call another page and do the sql calls there... i.e. you have to reload a page somewhere (the ajax page), you can't do it without loading any new pages :) Commented Jan 8, 2013 at 21:50

3 Answers 3

1

Ajax:

function XMLDoc()
{
if (window.XMLHttpRequest)
  {
    xmlhttp=new XMLHttpRequest();
  }
else
  {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            // xmlhttp.responseText -> return value from php file , so from here you can do your action
        }
    };
xmlhttp.open("POST","yourfile.php?email={value}",true); // from here send value to check in server side
xmlhttp.send(); 
}

Php:

if (isset($_POST['email'])){ // which is variable from `yourfile.php?email`
    $userCheck=mysql_query("SELECT email FROM users WHERE name = 'em_1'");
    $row=mysql_num_rows($usercheck);
    if ($row==0){
        echo "no";
    } else {
        echo "yes";
    }
}

Pass in yourfile URL of your server side script which checks if email exists or not and return for example yes or no => Then with help of Ajax it will be return in current document

Note: Don't use mysql extension , use mysqli or PDO instead

Sign up to request clarification or add additional context in comments.

1 Comment

And if you just want to find out whether or not the query returns results, you should use LIMIT 1. That will give you a simple found/not found report.If you want to know the exact number of results, it's best to use a COUNT() query. That way you always get one row, not potentially thousands.
1

Yes it is possible, but you need to know how to use ajax or jquery-ajax

which will help you get the data from php to javascript and then appended to html.

Comments

1

You need AJAX to make this work without a refresh. Check out this page on W3Schools: http://www.w3schools.com/php/php_ajax_database.asp

It's almost exactly what you want, but instead of returning a block of text, just enliven the b1 pieces.

1 Comment

Regardless of your bias, there is code on W3Schools that can serve as a perfectly fine example. Paraphrasing an answer on Meta Stack Overflow, determine for yourself if the information provided is a good reference instead of relying on a blanket statement by a 3rd party.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.