Jump to content

form, validation, ajax call and php insert in database. And nothing happens!


Guest

Recommended Posts

Hy guys! I 'm busting my balls with this for 2 days now. And it simply does not want to cooperate with me. Any help would be much appreciated.

 

What I wanted to have:

Form for registered users in which they would type in some data. And on button click function would validate this data, send it through AJAX to php processing, which would finally insert it in to database. If all well function would display success message and if any error it would display error message.

 

Sounds simple huh :)!

 

What I did:

Some research for possibly already existing code which I could modify for my needs or at least take it as some guideline. I tested 2 options and finally decide to go with this one:

http://www.myphpetc.com/2010/01/save-to-database-via-ajax-using-jquery.html

 

WHAT I NOW HAVE:

I modify example code so that it fits my needs. And this is what I have:

 

html form:

 

...

<script type="text/javascript" src=" !full path to file! /forms.js"></script>
<script type="text/javascript" src=" !full path to file! /priporocilo-shrani.js"></script>

	<form id="novopriporocilo" style="display: none;" action=" !full path to file! /priporocilo-nov.php" method="post">
		<fieldset>
			<label for="IDreferentpriporocila">ID REFERENT: </label>
			<input type="text" id="IDreferentpriporocila" name="IDreferentpriporocila" readonly="readonly" />
			<label for='datum'>DATUM: </label>
			<input type="text" id="datum" name="datum" readonly="readonly" value="<?php echo $poizvedbaterminidatum = Date("Y-m-d"); ?>" />
			<label for="IDuporabnik">UPORABNIK: </label>
			<select name="IDuporabnik" readonly="readonly"><option value="<?php echo $delovnomestoporabnikavhermesu; ?>"><?php echo $user_identity; ?></option></select>
			<label for="imepriporocila">*IME: </label>
			<input type="text" id="imepriporocila" name="imepriporocila" />
			<label for="priimekpriporocila">*PRIIMEK: </label>
			<input type="text" id="priimekpriporocila" name="priimekpriporocila" />
			<label for="telefon1priporocila">*TELEFON 1: </label>
			<input type="text" id="telefon1priporocila" name="telefon1priporocila"/>
			<label for="naseljepriporocila">*NASELJE: </label>
			<?php
				echo "<select name='naseljepriporocila'>";
				echo "<option value=''>izberi naselje priporocila</option>";
				while($row = mysqli_fetch_array($resultsnaseljavslovenijidropdownbox, MYSQLI_ASSOC)) {
				echo "<option value='{$row['ID_SeznamNASELJA']}'>{$row['NASELJE']}</option>";
				}
				echo "</select>";
			?>
			<label for="opombapriporocila">OPOMBA: </label>
			<input type="textarea" id="opombapriporocila" name="opombapriporocila" />

			<label for="naslovpriporocila">NASLOV: </label>
			<input type="text" id="naslovpriporocila" name="naslovpriporocila" />
			<label for="telefon2priporocila">TELEFON 2: </label>
			<input type="text" id="telefon2priporocila" name="telefon2priporocila" />
			<label for="poznanstvopriporocilo">POZNANSTVO: </label>
			<?php
				echo "<select name='poznanstvopriporocilo'>";
				echo "<option value=''>kako se z referentom poznata</option>";
				while($row = mysqli_fetch_array($resultspoznanstvodropdownbox, MYSQLI_ASSOC)) {
				echo "<option value='{$row['ID_SeznamPOZNANSTVO']}'>{$row['POZNANSTVO']}</option>";
				}
				echo "</select>";
			?>
			<label for="referentovaanonimnost">REFERENTOVA ANONIMNOST: </label>
			<input type="checkbox" name="referentovaanonimnost" value="1" />
			<label for="moderneboleznipriporocila">BOLEZNI: </label>
			<?php
				echo "<select name='moderneboleznipriporocila'>";
				echo "<option value=''>bolezni priporočila</option>";
				while($row = mysqli_fetch_array($resultsmoderneboleznidropdownbox, MYSQLI_ASSOC)) {
				echo "<option value='{$row['ID_SeznamMODERNE_BOLEZNI']}'>{$row['MODERNE_BOLEZNI']}</option>";
				}
				echo "</select>";
			?>
			<label for="clanigospodinjstvapriporocila"># ČLANOV: </label>
			<input type="text" id="clanigospodinjstvapriporocila" name="clanigospodinjstvapriporocila" />
			<label for="razmerjavgospodinstvupriporocila">RAZMERJA: </label>
			<input type="textarea" id="razmerjavgospodinstvupriporocila" name="razmerjavgospodinstvupriporocila" />
			<button type="button" onclick="priporociloShrani();">Shrani Priporočilo</button>
			<div style='clear:both;'></div>
		</fieldset>
	</form>

 

 

jQuery file to declare function and validate data before submiting

$("document").ready(function() {
$("#novopriporocilo").submit(function() {
	priporociloShrani();
	return false;
});
});

function priporociloShrani() {
var errors = '';

// Validate name
var IDreferentpriporocila = $("#novopriporocilo [name='IDreferentpriporocila']").val();
if (!IDreferentpriporocila) {
	errors += ' - Hej, kako ti je pa to uspelo!? Ni zabelezeno kdo je referent tega priporocila!\n';
}
var datum = $("#novopriporocilo [name='datum']").val();
if (!datum) {
	errors += ' - Hej, kako ti je pa to uspelo!? Datum je obvezni podatek!\n';
}
var IDuporabnik = $("#novopriporocilo [name='IDuporabnik']").val();
if (!IDuporabnik) {
	errors += ' - Hej, kako ti je pa to uspelo!? Kdo je dobil priporočilo je bovezni podatek!\n';
}
var imepriporocila = $("#novopriporocilo [name='imepriporocila']").val();
if (!imepriporocila) {
	errors += ' - Ime priporočila je obvezni podatek!\n';
}
var priimekpriporocila = $("#novopriporocilo [name='priimekpriporocila']").val();
if (!priimekpriporocila) {
	errors += ' - Priimek priporocila je obvezni podatek!\n';
}
var telefon1priporocila = $("#novopriporocilo [name='telefon1priporocila']").val();
if (!telefon1priporocila) {
	errors += ' - Vsaj EDEN kontaktni telefon je obvezni podatek!\n';
}
var naseljepriporocila = $("#novopriporocilo [name='naseljepriporocila']").val();
if (!naseljepriporocila) {
	errors += ' - Kraj iz kjer izhaja priporočilo je obvezni podatek!\n';
}
var opombapriporocila = $("#novopriporocilo [name='opombapriporocila']").val();
var naslovpriporocila = $("#novopriporocilo [name='naslovpriporocila']").val();
var telefon2priporocila = $("#novopriporocilo [name='telefon2priporocila']").val();
var poznanstvopriporocilo = $("#novopriporocilo [name='poznanstvopriporocilo']").val();
var referentovaanonimnost = $("#novopriporocilo [name='referentovaanonimnost']").val();
var moderneboleznipriporocila = $("#novopriporocilo [name='moderneboleznipriporocila']").val();
var clanigospodinjstvapriporocila = $("#novopriporocilo [name='clanigospodinjstvapriporocila']").val();
var razmerjavgospodinstvupriporocila = $("#novopriporocilo [name='razmerjavgospodinstvupriporocila']").val();

if (errors) {
	errors = 'Pri?lo je do napak:\n' + errors;
	alert(errors);
	return false;
}
else {
	// Submit our form via Ajax and then reset the form
	$("#novopriporocilo").ajaxSubmit({success:showResult});
	return false;
}

}

function showResult(data) {
if (data == 'save_failed') {
	alert('Shranjevanje priporočila je spodletelo! Obvezno kontaktiraj podjetje preko obrazca zgoraj!');
	return false;
} else {
	$("#novopriporocilo").clearForm().clearFields().resetForm();
	alert('Shranjevanje obrazca uspe?no!');
	return false;
}
}

 

 

php file to proccess data and insert it in to database:

<?php
// Retrieve form data
$IDreferentpriporocila = $_POST['IDreferentpriporocila'];
$datum = $_POST['datum'];
$IDuporabnik = $_POST['IDuporabnik'];
$imepriporocila = $_POST['imepriporocila'];
$priimekpriporocila = $_POST['priimekpriporocila'];
$telefon1priporocila = $_POST['telefon1priporocila'];
$naseljepriporocila = $_POST['naseljepriporocila'];
$opombapriporocila = $_POST['opombapriporocila'];
$naslovpriporocila = $_POST['naslovpriporocila'];
$telefon2priporocila = $_POST['telefon2priporocila'];
$poznanstvopriporocilo = $_POST['poznanstvopriporocilo'];
$referentovaanonimnost = $_POST['referentovaanonimnost'];
$moderneboleznipriporocila = $_POST['moderneboleznipriporocila'];
$clanigospodinjstvapriporocila = $_POST['clanigospodinjstvapriporocila'];
$razmerjavgospodinstvupriporocila = $_POST['razmerjavgospodinstvupriporocila'];

if (!$IDreferentpriporocila || !$datum || !$IDuporabnik || !$imepriporocila || !$priimekpriporocila || !$telefon1priporocila || !$naseljepriporocila) {
echo "save_failed";
return;
}

include('anteros_hermes_connect.inc'); //connection to database

if (!$dbhermes) {
echo "save_failed";
return;	
}

// Clean variables before performing insert
$clean_IDreferentpriporocila = mysql_real_escape_string($IDreferentpriporocila);
$clean_datum = mysql_real_escape_string($datum);
$clean_IDuporabnik = mysql_real_escape_string($IDuporabnik);
$clean_imepriporocila = mysql_real_escape_string($imepriporocila);
$clean_priimekpriporocila = mysql_real_escape_string($priimekpriporocila);
$clean_telefon1priporocila = mysql_real_escape_string($telefon1priporocila);
$clean_naseljepriporocila = mysql_real_escape_string($naseljepriporocila);
$clean_opombapriporocila = mysql_real_escape_string($opombapriporocila);
$clean_naslovpriporocila = mysql_real_escape_string($naslovpriporocila);
$clean_telefon2priporocila = mysql_real_escape_string($telefon2priporocila);
$clean_poznanstvopriporocilo = mysql_real_escape_string($poznanstvopriporocilo);
$clean_referentovaanonimnost = mysql_real_escape_string($referentovaanonimnost);
$clean_moderneboleznipriporocila = mysql_real_escape_string($moderneboleznipriporocila);
$clean_clanigospodinjstvapriporocila = mysql_real_escape_string($clanigospodinjstvapriporocila);
$clean_razmerjavgospodinstvupriporocila = mysql_real_escape_string($razmerjavgospodinstvupriporocila);

// Perform insert
$shranipriporocilo = "INSERT INTO tblKontaktPRIPOROCILA (ID_KontaktREFERENTI, DATUM, IME, PRIIMEK, NASLOV, ID_SeznamNASELJA, TEL_1, TEL_2, ID_SeznamMODERNE_BOLEZNI, GOSPODINJSTVO, RAZMERJA, OPOMBA, ID_SeznamPOZNANSTVO, REF_ANONIMNOST, ID_SeznamUPORABNIKI_PRIPOROCILO_DOBIL) 
VALUES ('$clean_IDreferentpriporocila', '$clean_datum', '$clean_imepriporocila', '$clean_priimekpriporocila', '$clean_naslovpriporocila', '$clean_naseljepriporocila', '$clean_telefon1priporocila', '$clean_telefon2priporocila', '$clean_moderneboleznipriporocila', '$clean_clanigospodinjstvapriporocila', '$clean_razmerjavgospodinstvupriporocila', '$clean_opombapriporocila', '$clean_poznanstvopriporocilo', '$clean_referentovaanonimnost', '$clean_IDuporabnik');";

if (@mysqli_query($dbhermes, $shranipriporocilo)) {
echo "save_failed";
return;
} else {
echo "success";
return;
}
?>

 

And finally a file called forms.js which came with an example and is containing all the wonderful functions for processing forms and automating ajax strings and stuff :)! Or so they say. I'm including it as attachment.

 

 

WHAT HAPPENS WHEN I CLICK ON BUTTON:

Nothing actually. Which is frustrating a hell out of me! Validation works. If any of required field is empty, I get an error warning.  But if all required data are ok, and they obviously pass the validation, simply nothing happens! NOTHING! No error, no new entry in database, no success message, no error message, nothing! And I'm comparing my code line by line with example and can not find any mistakes!  :wtf:

 

Please, I'm desperate here. Additional set of eyes, or even some guidance would be much appreciated!

 

Thank you!

18687_.zip

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.