PDA

View Full Version : Problems with post when loading content with ajax


ideocentro
03-28-2008, 09:31 PM
I´m programming an application that has a form that has some conditionals, that means that if they press some option on a radio a part of the form appears or if they press other option another external code placed in another file appears.

I´m using this javascript:

function crearInstancia() {
XMLHttp = false;

if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
var versiones = ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
for (var i=0;i<versiones.length;i++) {
try {
XMLHttp = new ActiveXObject(versiones[i]);
if (XMLHttp) {
return XMLHttp;
break;
}
} catch (e) {};
}
}
}
function cargarTexto(urltogo) {
XMLHttp = crearInstancia();

if (XMLHttp) {
var url=urltogo
XMLHttp.open("GET",url,true)
XMLHttp.onreadystatechange=cambiaEstado
XMLHttp.send(null)
} else {
alert('No se pudo crear la instancia');
}
}

function cambiaEstado() {
if (XMLHttp.readyState==4) {
document.getElementById("invoice").innerHTML = XMLHttp.responseText
}
}
//-->

------------------------------------------------------------------
And here is the script that calls it:
------------------------------------------------------------------

Choose invoice type
<input name="invoice_type" id="invoice_type" type="radio" value="invoice" onclick="cargarTexto('span.php');">
I want an INVOICE <input name="invoice_type" id="receipt" class="req" type="radio" value="receipt" onclick="cargarTexto('none.php');">
I want a RECEIPT <input name="invoice_type" type="radio" id="nothing" class="req" value="nothing" onclick="cargarTexto('none.php');">
I do not want anything</span>
<div id="invoice"></div>

------------------------------------------------------------------

As you can see when it calls cargarTexto('span.php') it loads span.php inside the div called "invoice", and that div is situated inside the general form that submits all the information.
Inside the span.php there are some inputs taht the form uses to complete the information needed.

------------------------------------------------------------------

Ok, the problem i have is that this works fine on Internet Explorer, but when i use it on firefox, thos POST variables that are on span.php don´t work.

Does anybody no why Mozilla navigators won´t find this variables?
I would really appreciate any suggestions you may give me about it.

Thanks in advance.

Sebastian

A1ien51
03-28-2008, 10:05 PM
Well you are using a GET and not a post and with Ajax you have to speicy the values being sent to the server in the send() method when using post.

Eric

ideocentro
03-29-2008, 03:14 AM
So i have to define each post i´m going to pass?
That means if i have 20 inputs i have to define something on the ajax function to make them work?
The form I have is not processed in ajax, it is processed using a regular php, because I think that what you are telling me here applies for a post done by ajax, but here the post is done by a regular form.

Could you please tell me if you think it would still matter the way in which the data is rescued by the ajax instance?

Here I paste the entire code of the file:

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><table width="100%" height="110" border="0" cellpadding="0" cellspacing="0" background="img/fondo_logo.gif">
<tr>
<td width="140"><img src="img/logo.gif" alt="CRL - Back to Apllication Form" width="140" height="110"></td>
<td align="right"><table width="100%" height="110" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td align="right"><span class="Titulos_forms">Application Form&nbsp;&nbsp;</span></td>
</tr>
<tr>
<td height="20" align="right" valign="bottom"><b><span class="titulos_navigation">Steps &gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; </span></b> <b><a href="index.php" class="links"><font color="#2E6C90">1. Application Form <?php echo $application_form ?> </font></a></b><span class="titulos_navigation">&nbsp; |&nbsp; <b>2.</b> Form1 <?php echo $form1 ?> &nbsp; |&nbsp; <b>3.</b> Form2 <?php echo $form2 ?> &nbsp; |&nbsp;<b>4.</b> Form3 <?php echo $form3 ?></span>&nbsp;&nbsp;</td>
</tr>
<tr>
<td height="5" align="right" valign="bottom"></td>
</tr>
</table> </td>
</tr>
</table></td>
</tr>
<tr>
<td><table width="100%" border="0" cellspacing="0" cellpadding="10">
<?php if ($mensaje) { ?><tr>
<td bgcolor="#FF0000" class="Form_Titles_Azul"><b><font color="#FFFFFF"><?php echo $mensaje.$mensaje2.$mensaje_email_duplicado ?></font></b></td>
</tr> <?php } ?>
<?php if ($emailok) { ?>
<td class="Form_Titles_Azul"><p><b><?php echo $_POST['laboratory_name'] ?></b> <br>
This is the information you submitted (also a copy of this email has been sent to email: <?php echo $valid_email ?> )</p>
<p>
<input name="Submit22" type="submit" class="Form_Titles_Azul" onClick="javascript:print()" value="Print"></p>
<table width="90%" border="0" cellspacing="0" cellpadding="5">
<tr>
<td><?php echo $body ?></td>
</tr>
</table>
<p>
<input name="Submit2" type="submit" class="Form_Titles_Azul" onClick="javascript:print()" value="Print">
</p></td>
</tr><?php } ?>
<?php if ($mensaje=="") { ?>
<tr>
<td><span class="Form_Titles_Azul">The Laboratory agrees to participate in the CRL European Proficiency Test in Fruits and Vegetables 10 (EUPT-FV-10) </span></td>
</tr>
<tr>
<td align="center"><table width="90%" border="1" cellpadding="3" cellspacing="0" bordercolor="#CCCCCC">
<form id="formulario_registro" name="formulario_registro" class="validate" method="post" action="<?php $_SERVER['PHP_SELF'] ?>" >

<tr>
<td width="26%" align="left"><table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="19%" class="Form_Titles_Azul"><b>
<label for="laboratory_name">Laboratory Name </label><span class="Form_Titles_Azul"><font color="#FF0000">*</font></span>
</b></td>
<td width="40%"><input name="laboratory_name" type="text" class="field req" id="laboratory_name" value="" size="50"></td>
<td width="41%"><b><span class="Form_Titles_Azul">
<label for="NRL">Are you NRL?</label>
</span>
<label for="label"> <span class="Form_Titles_Azul"><font color="#FF0000">*</font></span> </label>
<span class="Form_Titles_Azul">
<label for="NRL">&nbsp;&nbsp;&nbsp; Yes</label>
</span>
<input name="NRL" id="NRL_YES" class="field req" type="radio" value="1" <?php if ($_POST['NRL_YES']==1) echo "checked" ?>>
&nbsp;&nbsp;&nbsp;<span class="Form_Titles_Azul">No</span>
<input name="NRL" id="NRL_NO" class="field req" type="radio" value="0">
</b></td>
</tr>
</table></td>
</tr>
<tr>
<td align="left"><table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="19%" class="Form_Titles_Azul"><b><label for="contact_name">Contact Name </label><font color="#FF0000">*</font></b></td>
<td width="81%"><input name="contact_name" type="text" class="field req" id="contact_name" size="50" required="1"></td>
</tr>
</table></td>
</tr>
<tr>
<td align="left"><table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="65%" class="Form_Titles_Azul"><b>
<label for="samples_MRM">Number of samples analyzed by MRMs in 2007</label> <font color="#FF0000">*</font></b></td>
<td width="35%"><input name="samples_MRM" type="text" class="field req" id="samples_MRM" size="20"required="1"></td>
</tr>
</table></td>
</tr>
<tr>
<td align="left"><table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="65%" class="Form_Titles_Azul"><b>
<label for="samples_EU_official">Number of samples analyzed for the EU Official Monitoring Program in 2007</label> <font color="#FF0000">*</font></b></td>
<td width="35%"><input name="samples_EU_official" type="text" class="field req" id="samples_EU_official" size="20" required="1"></td>
</tr>
</table></td>
</tr>
<tr>
<td align="left"><table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td class="Form_Titles_Azul"><b>Laboratory Delivery Address</b></td>
</tr>
<tr>
<td class="Form_Titles_Azul"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="Form_Titles_Azul"><label for="laboratory_address_street">Street </label><b><font color="#FF0000">*</font></b>
<input name="laboratory_address_street" type="text" class="field req" id="laboratory_address_street" size="60">
&nbsp;&nbsp;</td>
<td class="Form_Titles_Azul">Number
<input name="laboratory_address_number" type="text" class="field" id="laboratory_address_number" size="50"></td>
</tr>
</table></td>
</tr>
<tr>
<td class="Form_Titles_Azul"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="Form_Titles_Azul"><label for="laboratory_address_postal_code">Postal Code </label><b><font color="#FF0000">*</font></b>
<input name="laboratory_address_postal_code" type="text" class="field req" id="laboratory_address_postal_code" size="10" required="1">
&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td class="Form_Titles_Azul"><label for="laboratory_address_city"> City</label> <b><font color="#FF0000">*</font></b>
<input name="laboratory_address_city" type="text" class="field req" id="laboratory_address_city" size="40" required="1"></td>
<td class="Form_Titles_Azul"><label for="laboratory_address_country"> Country </label><b><font color="#FF0000">*</font></b>
<input name="laboratory_address_country" type="text" class="field req" id="laboratory_address_country" size="45" required="1"></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td align="left"><table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td class="Form_Titles_Azul"><b>Laboratory Postal Address</b> (if different from delivery address) </td>
</tr>
<tr>
<td class="Form_Titles_Azul"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="Form_Titles_Azul">Street
<input name="postal_address_street" type="text" class="field" id="postal_address_street" size="60">
&nbsp;&nbsp;</td>
<td class="Form_Titles_Azul">Number
<input name="postal_address_number" type="text" class="field" id="postal_address_number" size="50"></td>
</tr>
</table></td>
</tr>
<tr>
<td class="Form_Titles_Azul"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="21%" class="Form_Titles_Azul">Postal Code
<input name="postal_address_postal_code" type="text" class="field" id="postal_address_postal_code" size="10" required="1">
&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td width="37%" class="Form_Titles_Azul"> City
<input name="postal_address_city" type="text" class="field" id="postal_address_city" size="45" required="1"></td>
<td width="42%" class="Form_Titles_Azul"> Country
<input name="postal_address_country" type="text" class="field" id="postal_address_country" size="45" required="1"></td>
</tr>
</table></td>
</tr>

</table></td>
</tr>
<tr>
<td align="left"><table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="50%" class="Form_Titles_Azul"><b>
<label for="telephone">Telephone</label><font color="#FF0000"> *</font> </b><span class="titulos_navigation"> (including the necessary prefixes to call from other country):</span></td>
<td width="50%" valign="middle"><input name="telephone" type="text" class="field req" id="telephone" size="50" required="1"></td>
</tr>
</table></td>
</tr>
<tr>
<td align="left"><table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="50%" class="Form_Titles_Azul"><b>Fax </b><span class="titulos_navigation"> (including the necessary prefixes to call from other country):</span></td>
<td width="50%" valign="middle"><input name="fax" type="text" class="field" id="fax" size="50"></td>
</tr>
</table></td>
</tr>
<tr>
<td align="left"><table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="50%" class="Form_Titles_Azul"><b>Mobile (optional) </b><span class="titulos_navigation"> (including the necessary prefixes to call from other country):</span></td>
<td width="50%" valign="middle"><input name="mobile" type="text" class="field" id="mobile" size="50"></td>
</tr>
</table></td>
</tr>
<tr>
<td align="left"><table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="50%" class="Form_Titles_Azul"><b><label for="email">Email </label><font color="#FF0000">*</font> <font size="1">(this will be your username to enter in the Private Area)</font></b></td>
<td width="50%" valign="middle"><input name="email" type="text" class="field req" id="email" size="50" required="1" mask="email"></td>
</tr>
</table></td>
</tr>
<tr>
<td align="left"><table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="50%" class="Form_Titles_Azul"><b><label for="password">Password </label>
<font color="#FF0000">* </font><font size="1">(this will be your password to enter in the Private Area)</font></b></td>
<td width="50%" valign="middle"><input name="password" type="password" class="field req" id="password" size="50" required="1"></td>
</tr>
</table></td>
</tr>
<tr>
<td align="left"><table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="30"><input name="receive_standard_solvent_solution" type="checkbox" id="receive_standard_solvent_solution" value="1" ></td>
<td valign="middle" class="Form_Titles_Azul"> I would like to receive the standard-solvent solution free of charge after the deadline for submitting results is over.</td>
</tr>
</table></td>
</tr>
<tr>
<td align="left"><table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="30" valign="top"><input name="payment_agreement" type="checkbox" class="req" id="payment_agreement_ACCEPTED" value="1" checked>
<b><span class="Form_Titles_Azul"><font color="#FF0000">*</font></span></b></td>
<td valign="middle" class="Form_Titles_Azul"><td>
</tr>
</table></td>
</tr>
<tr>
<td align="left"><table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td><span class="Form_Titles_Azul"><b>Payment must be done before <?php echo $payment ?></b> <br>
<label for="invoice_type">Choose invoice type:</label>&nbsp;<b><span class="Form_Titles_Azul"><font color="#FF0000">*</font></span></b>

&nbsp;
<input name="invoice_type" id="invoice_type" type="radio" value="invoice" onclick="cargarTexto('span.php');">
I want an <font color="#006633"><b>INVOICE</b></font>&nbsp;&nbsp;&nbsp;
<input name="invoice_type" id="receipt" class="req" type="radio" value="receipt" onclick="cargarTexto('none.php');">
I want a RECEIPT&nbsp;&nbsp;&nbsp;
<input name="invoice_type" type="radio" id="nothing" class="req" value="nothing" onclick="cargarTexto('none.php');">
I do not want anything</span>
<div id="invoice"></div></td>
</tr>
</table></td>
</tr>
<tr>
<td align="left"><table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="9%" class="Form_Titles_Azul"><b>Date:</b></td>
<td width="91%"><script>DateInput('date', true, 'YYYY-MM-DD')</script></td>
</tr>
</table></td>
</tr>

<td align="left" class="Form_Titles_Azul"><br>
<b>The above information is essential to up-date all the laboratories data. <br>
</b>The delivery deadline for this Application Form is: <b><?php echo $deadline ?></b><br>
<br></td>
</tr>
<tr>
<td align="center" class="Form_Titles_Azul"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="30%" align="left"><b><font color="#FF0000">* Required Fields</font></b></td>
<td width="40%"><div align="center">
<input name="Submit" type="submit" class="Form_Titles_Azul" value=" Send ">
<!--onClick="MM_validateForm('laboratory_name','','R','contact_name','','R','samples_MRM','','R','samples_EU_offi cial','','R','laboratory_address_street','','R','laboratory_address_postal_code','','R','laboratory_ address_city','','R','laboratory_address_country','','R','telephone','','R','mobile','','R','email', '','RisEmail','vat_number','','R','invoice_address_laboratory_name','','R','invoice_address_street', '','R','invoice_postal_code','','R','invoice_city','','R','invoice_country','','R');return document.MM_returnValue" -->
<input name="enviar" type="hidden" id="enviar" value="1">
</div></td>
<td width="30%">&nbsp;</td>
</tr>
</table></td>
</tr>

</form>
</table>
<br></td>
</tr><?php } ?>
<tr>
<td bgcolor="#2E6C90" class="texto"><font color="#FFFFFF">If you have <b>problems filling the forms</b> please contact with <b>xx</b>: <a href="mailto:xx" class="linksBLANCOS">xx</a> <b>(xxx) xx </b><br>
If you have any <b>doubt about the required fields</b> please contact with <b>Paula Medina</b>: <a href="mailto:xxx" class="linksBLANCOS">xxx</a> <b>xxx </b></font></td>
</tr>
</table></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
</table>
</body>
</html>

tomws
03-29-2008, 05:17 AM
First, wrap your posted code in [ code ] tags. Otherwise, it's just an ugly pile of characters that no one wants to take the time to read.

This may be your problem. It looks like you're using the AJAX connection incorrectly. But it's hard to read.
function cargarTexto(urltogo) {
XMLHttp = crearInstancia();

if (XMLHttp) {
XMLHttp.onreadystatechange=cambiaEstado

var url=urltogo

// If 'togo' is really a url/page like somepage.php, then we need to add more.
// Recall that the GET method for 'normal' forms passes parameters with the
// url. You have to do the same thing with AJAX.
url += "?first=" + encodeURIComponent(myFirstVariable)
url += "&second=" + encodeURIComponent(mySecondVariable)
url += "&third=" + encodeURIComponent(myThirdVariable)

XMLHttp.open("GET",url,true)
XMLHttp.send(null)
} else {
alert('No se pudo crear la instancia');
}


POST is a little different, if you're trying to use that.

function cargarTexto(urltogo) {
XMLHttp = crearInstancia();

if (XMLHttp) {
XMLHttp.onreadystatechange=cambiaEstado

var url=urltogo
// POST passes encoded values; not like GET on the URL line
postvars = "?first=" + encodeURIComponent(myFirstVariable)
postvars += "&second=" + encodeURIComponent(mySecondVariable)
postvars += "&third=" + encodeURIComponent(myThirdVariable)

XMLHttp.open("POST",url,true)
XMLHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
XMLHttp.send(postvars)
} else {
alert('No se pudo crear la instancia');
}


Note: I use encodeURIComponent to properly escape special characters that may appear in the passed parameters.