Sending the data
To send a value per POST
, via javascript, the easiest way is by using jQuery.
Using jQuery
$.ajax({
url: 'pagina-para-receber-os-dados.php',
type: 'POST', // GET é o padrão
dataType: 'json', // pode ser xml, json, script, ou html, o jQuery também detecta automáticamente,
// mas é bom sempre informar
data: {'vpart': $('#id_tama_moni').val()}, // Também pode usar serialize para enviar todo o formulário
success: function(data){ // script executado, quando o ajax é enviado com sucesso
console.log(data);
alert(data.msg);
},
error : function(XMLHttpRequest, textStatus, errorThrown) { // Script executado quando houve erro
console.log(XMLHttpRequest, textStatus);
console.log('----------------------------------');
console.log(XMLHttpRequest.responseText);
console.log('----------------------------------');
console.log(errorThrown);
alert('Houve um erro ao enviar os dados');
}
});
Pure Javascript
var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST","pagina-para-receber-os-dados.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
valor = document.getElementById('id_tama_moni').value;
xmlhttp.send("vpart="+valor);
xmlhttp.onreadystatechange= function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) { // script executado, quando o ajax é enviado com sucesso
alert(xmlhttp.responseText);
} else { // Script executado quando houve erro
alert('Houve um erro ao enviar os dados');
}
}
Receiving the data
PHP (page-to-receive-the-data.php)
<?php
if (isset($_POST['vpart'])){
echo json_encode(array('msg' => 'Dados recebidos com sucesso pelo servidor'));
} else {
echo json_encode(array('msg' => 'Os dados não foram recebidos pelo servidor'));
}
exit;
Changing the value of an element
Using jQuery
v_patr = v_patr + $("#nm_cb_tama_moni").val();
$("#w_patr_seri").val(v_patr);
Using Javascript
v_patr = v_patr + document.forms['sai_frm_incl'].nm_cb_tama_moni.value;
document.getElementById("w_patr_seri").value = v_patr;
HTML
<html>
<head><title>Titulo da Pagina</title></head>
<body>
<form name="sai_frm_incl" method="POST">
<table border="0" width="100%">
<tr>
<td colspan="3" bgcolor="Silver" align="center">
<input type="hidden" name="w_patr_seri" id="w_patr_seri" value="<?=$w_patr_seri?>" />
</td>
</tr>
<tr>
<td>
<font face="arial" align="center" color="blue" size="-1">Teste</font><br>
<input type="text" name="nm_cb_tama_moni" id="id_tama_moni" maxlength="12" size="12" style="font-size:11; color:Black;" value="">
</td>
</tr>
</table>
</form>
</body>
</html>
Note: Put an ID in the HIDDEN field you want to assign the value
Do you want to submit an ajax form? Have you tried jQuery?
$.ajax({type:'POST',...});
– KaduAmaral
I don’t understand well, it seems more like a php problem... And the markup should be <?php ... ? > otherwise it might not work.
– Gustavo
@Kaduamaral, I was using v_patr to pick up the next program via $_GET content, but the problem is that GET only supports a maximum number of characters, so I decided to store the contents of v_patr in a Hidden variable and pass via POST!
– Alexandre
It’s kind of hard to understand your logic, but come on. You want a value of
javascript
, be passed to aform
, for PHP to receive this data? Where is the elementdocument.forms['sai_frm_incl'].nm_cb_tama_moni.value;
that you’re getting the value? I didn’t find it in your code.– KaduAmaral
Error my @Kaduamaral, I ended up copying the wrong excerpt. The excerpt would be this
<input type="text" name="nm_cb_tama_moni" id="id_tama_moni" maxlength="12" size="12" style="font-size:11; color:Black;" value="">
. And yes I want the value ofJS
get caught byPHP
.– Alexandre