<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Rails Forum - Ruby on Rails Help and Discussion Forum - How to convert PHP AJAX code to Ruby/Rails]]></title>
		<link>http://railsforum.com/viewtopic.php?id=52894</link>
		<description><![CDATA[The most recent posts in How to convert PHP AJAX code to Ruby/Rails.]]></description>
		<lastBuildDate>Thu, 14 Feb 2013 19:04:24 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[How to convert PHP AJAX code to Ruby/Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=159741#p159741</link>
			<description><![CDATA[<p>I&#039;m converting a PHP website to Ruby/Rails. I have a form and when a User signs up he is asked for an email address among other things. I check if the email is a duplicate via AJAX. So my only field in the form which requires an AJAX check is the email field.&nbsp; I set up the JavaScript like so:</p><div class="codebox"><pre><code>function checkEmail()
{
  $.ajaxSetup({async:false});
  $.post(&#039;duplicateEmail.php&#039;, {email: $(&#039;#email&#039;).val()}, 
  function(data)
  {
    if(!data.success)
    {
      $(&#039;#email&#039;).addClass(&#039;emptyField&#039;);
      setError(&#039;error&#039;);
      setError(&#039;dupEmail&#039;);
    }
    else if(data.success)
    {
      $(&#039;#email&#039;).removeClass(&#039;emptyField&#039;);
      resetError(&#039;dupEmail&#039;);
    }
  },
&#039;json&#039;);
}</code></pre></div><p>Then in a PHP file I have the following:</p><div class="codebox"><pre><code>$email = $_POST[&#039;email&#039;];

$query = &quot;SELECT id FROM users WHERE email=&#039;$email&#039;&quot;;
$result = $mysqli-&gt;query($query);

$data[&#039;error&#039;] = mysql_error();

if($result-&gt;num_rows == 0)
{
  $result-&gt;free();
  $data[&#039;success&#039;] = true;
  $data[&#039;message&#039;] = &#039;&#039;;
}
else
{
  $result-&gt;free();
  $data[&#039;success&#039;] = false;
  $data[&#039;message&#039;] = &#039;This email address has already been registered.&#039;;
}

echo json_encode($data);</code></pre></div><p>Can anyone point me in the right direction as to where to place the Ruby code?&nbsp; Or am I thinking about this in the wrong way?</p>]]></description>
			<author><![CDATA[dummy@example.com (mikeglaz)]]></author>
			<pubDate>Thu, 14 Feb 2013 19:04:24 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=159741#p159741</guid>
		</item>
	</channel>
</rss>
