klemme 3 Posting Whiz in Training

Hi all,

This is a ? about Ajax, PHP and form processing.

When I submit the form further below, nothing happens!

I didnt set the form-"action" to anything. I left it out, to make the form submit to the same page, so I can validate on the same page too (Which is not made yet, as youll see..)

The form does work fine when I used in a "normal php script", but when I include it using Ajax, the form doesnt submit after it has been sent to the page.

Anyone knows what is going on, and what I can do to make it work?

I guess it must be the action attribute of the form which is wrong, but I dont understand that since i made the form submit to itself, by leaving out the action attribute entirely.

I have two links on a page, which calls a javascript function, opening a xmlhttprequest:

I have only shown one of the forms, as they are almost similar, but there are two forms, in the real page, which displays depending on the link selected.

1)

<a href="javascript:vis_jagt_annonce_form('danmark')">Opret Annonce For Jagt I Danmark</a>

2)

<a href="javascript:vis_jagt_annonce_form('udland')">Opret Annonce For Jagt I Udlandet</a>

It calls this script:

function makeRequestObject(){
var xmlhttp=false;
try {
  xmlhttp = new ActiveXObject('Msxm12.XMLHTTP');
} catch (e) {
	try {
		xmlhttp = new ActiveXObejct('Microsoft.XMLHTTP');
	} catch (E) {
		xmlhttp = false;
	            }
	}
	if(!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest ();
		}
return xmlhttp;
}
 
function vis_jagt_annonce_form(region)
{ 
var xmlhttp=makeRequestObject();
var file = 'ajax/jagt_annonce_form.php?region=';
xmlhttp.open('GET', file+region, true);
xmlhttp.onreadystatechange=function() {
	if(xmlhttp.readyState==4) {
			document.getElementById('dansk_annonce').innerHTML = xmlhttp.responseText;
		}
	}
xmlhttp.send(null);
return;
}

Then opening the PHP script, which checks to see which form to send back to the page for display:
This is rather long, but really simple - its just the table layout that makes it a mile, sorry for that

$annonce_type = $_GET['region'];
 
if($annonce_type == 'danmark') // Så vises formen til oprettelse af dansk jagt annonce
{
if(isset($_POST['opret_annonce_dk'])){
// Hent data via post:
$region = mysqli_real_escape_string($connection, $_POST['region']);
 
$postnr = mysqli_real_escape_string($connection, $_POST['postnr']);
 
$by = mysqli_real_escape_string($connection, $_POST['by']);
 
$størrelse = mysqli_real_escape_string($connection, $_POST['størrelse']);
 
$størrelse_værdi = mysqli_real_escape_string($connection, $_POST['størrelse_værdi']);
 
$pris = mysqli_real_escape_string($connection, $_POST['pris']);
 
$jagthytte = mysqli_real_escape_string($connection, $_POST['jagthytte']);
 
$vildt = mysqli_real_escape_string($connection, $_POST['vildt']);
 
$vandhul = mysqli_real_escape_string($connection, $_POST['vandhul']);
 
$telefon = mysqli_real_escape_string($connection, $_POST['telefon']);
 
$email = mysqli_real_escape_string($connection, $_POST['email']);
 
$beskrivelse = mysqli_real_escape_string($connection, $_POST['beskrivelse']);
 
// Validate data her:
 
// Indsæt annoncen databasen:
$sql = "INSERT INTO jagt
(region, postnr, city, størrelse, pris, jagthytte, vildt, vandhuller, img1, img2, img3, tlf, email, beskrivelse)
VALUES
('$region', '$postnr', '$by', CONCAT('$størrelse $størrelse_værdi'), '$pris', '$jagthytte', '$vildt', '$vandhul', 'styles/test-img1.gif', 'styles/test-img2.gif', 'styles/test-img3.gif', '$telefon', '$email', '$beskrivelse')";
$result = mysqli_query($connection, $sql);
// Hvis formen er indsendt og alle data er valideret:
if($result)
	{
echo $annonce_oprettet = '<p>Mange tak for at du har oprettet en annonce på jagt-leje.dk!<br /><br />
Vi ønsker dig det bedste held og lykke med at få udlejet jagten hurtigst muligt!
<br /><br />
Husk, at hvis der er noget der skal laves om eller et billede der skal skiftes ud, så
kan du altid logge ind på din profil og foretage ændringerne!
<br /><br />
Med Venlig Hilsen<br /><br />
jagt-leje.dk</p>';
	}
}
if(empty($annonce_oprettet)){
	echo '
<hr />
<br />
<fieldset class="opret_jagt_annonce_fieldset">
<legend class="opret_jagt_annonce_legend"><p>Skal Udfyldes&nbsp;<span style="color:red;">*</span></p></legend>
<table>
<form name="jagt_annonce" id="jagt_annonce" method="POST">
<tr>
<td><p><b>Region:</b>&nbsp;<span style="color:red;">*</span></p></td>
<td style="padding-left:40px;"><p><b>Postnummer:</b>&nbsp;<span style="color:red;">*</span></p></td>
<td style="padding-left:40px;"><p><b>By:</b>&nbsp;<span style="color:red;">*</span></p></td>
</tr>
<tr>
<td>
<select name="region" id="region" style="padding:3px; border:1px dotted #333;">
<option value="Jylland">Jylland</option>
<option value="Seeland">Sjælland</option>
<option value="Fyn">Fyn</option>
</select>
</td>
<td style="padding-left:40px;">
<input type="text" name="postnr" id="postnr" style="padding:3px; border:1px dotted #333;"/>
</td>
<td style="padding-left:40px;">
<input type="text" name="by" id="by" style="padding:3px; border:1px dotted #333;"/>
</td>
</tr>
 
<tr>
<td style="padding-top:5px;"><p><b>Størrelse:</b>&nbsp;<span style="color:red;">*</span></p></td>
<td style="padding-top:5px; padding-left:40px;"><p><b>Pris:</b>&nbsp;<span style="color:red;">*</span></p></td>
</tr>
<tr>
<td>
<input type="text" name="størrelse" id="størrelse"  style="width:50px; padding:3px; border:1px dotted #333;"/>
 
<select name="størrelse_værdi" id="størrelse_værdi" style="padding:3px; border:1px dotted #333;">
<option value="Tønder Land">Tønder land</option>
<option value="Hektarer">Hektarer</option>
<option value="Km2">Km2</option>
</select>
</td>
<td style="padding-left:40px;">
<input type="text" name="pris" id="pris" style="padding:3px; border:1px dotted #333;"/>
</td>
</tr>
</table>
</fieldset>
 
 
<fieldset class="opret_jagt_annonce_fieldset">
<legend class="opret_jagt_annonce_legend"><p>Ekstra Information</p></legend>
<table>
<tr>
<td style="padding-top:5px;"><p><b>Jagthytte:</b></p></td>
<td style="padding-top:5px; padding-left:40px;"><p><b>Vandhuller:</b></p></td>
</tr>
<tr>
<td>
<input type="radio" name="jagthytte" id="jagthytte" value="ja" />&nbsp;Ja
&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="jagthytte" id="jagthytte" value="nej" checked />&nbsp;Nej
</td>
<td style="padding-left:40px;">
<input type="radio" name="vandhul" id="vandhul" value="0" checked />&nbsp;0
&nbsp;&nbsp;
<input type="radio" name="vandhul" id="vandhul" value="1" />&nbsp;1
&nbsp;&nbsp;
<input type="radio" name="vandhul" id="vandhul" value="2+" />&nbsp;2+
</td>
</tr>
 
<tr>
<td style="padding-top:5px;"><p><b>Billede 1:</b></p>
<input type="file" name="img1" id="img1" style="padding:2px;" />
</td>
<td style="padding-left:40px; padding-top:15px;">
<p style="color:#666; font-size:12px;">
<i>"Husk at med billeder får en mulig lejer den bedste mulighed for at få et godt indtryk af jagt-annoncen!</i></p>
</td>
</tr>
 
<tr>
<td style="padding-top:5px;"><p><b>Billede 2:</b></p>
<input type="file" name="img2" id="img2" style="padding:2px;" />
</td>
<td style="padding-left:40px;">
<p style="color:#666; font-size:12px;">
<i>Derudover bliver ekstra information vist i annnocen allerede når der er valgt en region!</i></p>
</td>
</tr>
 
</tr>
<td style="padding-top:5px;"><p><b>Billede 3:</b></p>
<input type="file" name="img3" id="img3" style="padding:2px;" />
</td>
<td style="padding-left:40px;">
<p style="color:#666; font-size:12px;">
<i><b>Det vil sige at jo mere der udfyldes, jo bedre indtryk får en mulig lejer når han/hun bladrer igennem alle annoncerne!"</i></b></p>
</td>
</tr>
 
</table>
</fieldset>
 
<fieldset class="opret_jagt_annonce_fieldset">
<legend class="opret_jagt_annonce_legend"><p>Skal Udfyldes&nbsp;<span style="color:red;">*</span></p></legend>
<table>
<tr>
<td><p><b>Beskrivelse:</b>&nbsp;<span style="color:red;">*</span></p></td>
</tr>
<tr>
<td>
<textarea name="beskrivelse" id="beskrivelse" rows="6" cols="30" style="padding:3px; border:1px dotted #333;"></textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" name="opret_annonce_dk" id="opret_annonce" value="Indryk Annoncen &rarr;" style="padding:3px;" />
</td>
</tr>
</fieldset>
</table>
</form>';
}
}

Anyone? :-)
Regards, Klemme

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.