Jump to content
php.lv forumi

json_encode un reģistrācija


ziedinjsh

Recommended Posts

Sveiki.

Lieta tāda, ka nospiežu pogu reģistrēties man vnk pārlādējas lapa un neparāda erroru vai success msg. Lapai nemaz nevajag pārlādēteies :?

 

html:

<div id="error" style="display:none;">error</div>
<div id="success" style="display:none;">success</div>
<form method="post" id="register">
<p>Pseidonīms</p>
<input type="text" name="username" class="input" />
<p>Dzimums</p>
<select name="genere" class="select">
<option value="m">Vīrietis</option>
<option value="f">Sieviete</option>
</select>
<p>Epsts</p>
<input type="text" name="email" class="input" />
<p>Parole</p>
<input type="password" name="password" class="input" />
<p><input type="submit" name="reg" value="Reģistrēties" class="button" /></p>
</form>

 

jQuery:

jQuery(document).ready(function(){
jQuery('#register').submit(function(event) {
 event.preventDefault();
 dataString = jQuery("#register").serialize();
 jQuery.ajax({
 type: "post",
 url: "process/register.php",
 dataType:"json",
 success: function (response) {
	 if(response.status === "success") {
		 jQuery('#success').html(response);
	 } else if(response.status === "error") {
		 jQuery('#error').html(response);
	 }
 }
});
return false;
});
});

 

un php:

<?php
include("dbase.php");
$username = mysql_real_escape_string($_POST['username']);
$genere = mysql_real_escape_string($_POST['genere']);
$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);
$password2 = md5($password);
$date = date('d.m.Y');
$check = mysql_query("select * from users where email='$email'") or die(mysql_error());
$result = mysql_num_rows($check);
if(empty($username) || empty($email) || empty($password)){
echo json_encode(array('status' => 'error', 'message' => 'Kāds no lauciņiem ir palicis tukšs!' ));
}elseif($result==1){
echo json_encode(array('status' => 'error', 'message' => 'Šāda epasta adresi ir jau reģistrēta.'));
}elseif(!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email)){
echo json_encode(array('status' => 'error', 'message' => 'Epasts norādīts nepareizi.'));
}else{
mysql_query("insert into users (username, genere, email, password, date) values ('$username', '$genere', '$email', '$password2', '$date')") or die(mysql_error());
echo json_encode(array('status' => 'success', 'message' => 'Reģistrācija veiksmīga'));
}
?>

Edited by ziedinjsh
Link to comment
Share on other sites

<p><input type="button" name="reg" value="Reģistrēties" class="button" /></p>

vienkaršāk nomaini pogas tipu uz button un tad ar jquery click funkciju izpildi savas darbības.

Pilnīgi un galīgi nepareizs piegājiens.

 

Kas tādā gadījumā būs, ja lietotājam nebūs javascript atbalsta, vai arī viņš gluži vienkārši nospiedīs "enter"?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...