Forum Pplware

Versão Completa: Passar um valor gerado por JS para PHP
Está de momento a ver uma versão reduzida do nosso conteúdo. Ver versão completa com o formato adequado.
Olá,

tenho a seguinte duvida...
o projecto consiste no seguinte:

tenho uma base de dados com produtos, no backoffice o utilizador coloca o preço de cada produto por metro quadrado. Depois quando vou buscar o produto à base de dados abaixo do preço por metro quadrado coloco um formulário onde o utilizador coloca a altura e a largura e abaixo disso é calculado em tempo real quanto é que o cliente terá que pagar pelo produto.

Depois tenho um botão submit para comprar o produto com aquelas características, mas não consigo passar o valor que foi gerado para o carrinho de compras... alguém faz alguma ideia de como é que isto se faz?

se alguém souber de algum exemplo deste género também ajudava :-)
Então se colocas no formulário o valor, não consegues aceder por $_POST['variavel'] ? Não estou a perceber o problema.
sim, mas isto não foi programado por mim, peguei agora no projecto e eu não tenho muita experiência com JS... vou postar o código e assim ja me podem ajudar a colocar o resultado num form..


codigo JS

Código PHP:
<script type="text/JavaScript">
<!--
function 
MM_preloadImages() { //v3.0
  
var d=document; if(d.images){ if(!d.MM_pd.MM_p=new Array();
    var 
i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0i<a.lengthi++)
    if (
a[i].indexOf("#")!=0){ d.MM_p[j]=new Imaged.MM_p[j++].src=a[i];}}
}

function 
MM_swapImgRestore() { //v3.0
  
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function 
MM_findObj(nd) { //v4.01
  
var p,i,x;  if(!dd=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    
d=parent.frames[n.substring(p+1)].documentn=n.substring(0,p);}
  if(!(
x=d[n])&&d.allx=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(
i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!
&& d.getElementByIdx=d.getElementById(n); return x;
}

function 
MM_swapImage() { //v3.0
  
var i,j=0,x,a=MM_swapImage.argumentsdocument.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((
x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrcx.oSrc=x.srcx.src=a[i+2];}
}

function 
on_focus(id)
{
 if(
document.getElementById(id).value == 'Enter the height of your wall' || document.getElementById(id).value == 'Enter the width of your wall'){document.getElementById(id).value '';}
}

function 
on_change()
{
 if(
document.getElementById('haut').value == 'Enter the height of your wall'){
 
Haut '1';}
 else{
 
Haut document.getElementById('haut').value
 
Hautinch = eval(document.getElementById('haut').value/2.54);
 
document.getElementById('inch1').value Hautinch.toFixed(2);
 
 }
 if(
document.getElementById('larg').value == 'Enter the width of your wall'){Larg '1';}
 else{
 
Larg document.getElementById('larg').value;
 
Larginch  = eval(document.getElementById('larg').value/2.54);
 
document.getElementById('inch2').value Larginch.toFixed(2);
 
 }
 
Tot = eval((Haut/100)*(Larg/100));
 
document.getElementById('prix').innerHTML = eval(Math.ceil(Tot)*<?php echo $preco?>);
 document.getElementById('model').value = Haut+'X'+Larg;
}
//-->
</script> 





codigo php


Código PHP:
<form action="cart.php?id=<?php echo $idproduto; ?>&&preco=<?php echo $preco; ?>" method="post" name="p">
<
td width="251" height="135" valign="top"><br>
           </
td>
        <
td class="grasrouge" width="20" valign="top">&nbsp;</td>
        <
td class="grasrouge" width="377" valign="top"><br>
          <
input name="taille" id="taille" value="S" type="hidden">

          <
input name="add" id="add" value="ok" type="hidden">
          <
input name="model" id="model" value="" type="hidden">
          <
input name="id" id="id" value="5099" type="hidden">
          <
table width="369" border="0" cellpadding="0" cellspacing="0">
          <
tbody><tr>
            <
td width="91" height="25">HEIGHT :</td>
            <
td width="142" height="25"><label>
              <
input name="haut" class="paragraphe" id="haut" value="Enter the height of your wall" onFocus="on_focus('haut')" onKeyUp="on_change()" size="12" maxlength="30" type="text">

            
cm</label></td>
            <
td width="136"><label>
              <
input name="inch1" class="paragraphe" id="inch1" size="12" maxlength="15" onKeyUp="on_change2()" type="hidden">
              <
span class="paragraphe">inch</span></label></td>
          </
tr>
          <
tr>
            <
td height="25">WIDTH :</td>

            <
td height="25"><input name="larg" class="paragraphe" id="larg" value="Enter the width of your wall" onFocus="on_focus('larg')" onKeyUp="on_change()" size="12" maxlength="30" type="text">
              
cm</td>
            <
td height="25"><label>
              <
input name="inch2" class="paragraphe" id="inch2" size="12" maxlength="10" onKeyUp="on_change2()" type="hidden">
              <
span class="paragraphe">inch</span></label></td>
          </
tr>
          <
tr>
            <
td height="25">PRICE : </td>
            <
td colspan="2" class="grasrouge" height="25"><p id="prix" style="display: inline;">0</peuros</td>
          </
tr>
          <
tr>
            <
td height="30">&nbsp;</td>
            <
td><input name="submit" type="submit"></td>
          </
tr>
        </
tbody></table>
        </
td>
      </
form


Todos os campos estão num campo de formulário, excepto o resultado, e esse é que eu queria, que posso fazer para mudar isso?
Código PHP:
<form action="cart.php?id=<?php echo $idproduto; ?>&&preco=<?php echo $preco; ?>" method="post" name="p"

Tu aqui estás a abrir tags php, e a por echos com variáveis não definidas.
Sim isso tinha sido para um tentativa anterior e quando coloquei aqui o código esquecime de tirar, no entanto vou colocar aqui a solução que arranjei...


Código PHP:
}
                         if(
document.getElementById('larg').value == 'Enter the width of your wall'){Larg '1';}
                         else{
                         
Larg document.getElementById('larg').value;
                         
Larginch  = eval(document.getElementById('larg').value/2.54);
                         
document.getElementById('inch2').value Larginch.toFixed(2);
                         
                         }
                         
Tot = eval((Haut/100)*(Larg/100));
                         
document.getElementById('prix').value = eval(Math.ceil(Tot)*<?php echo $preco?>);
                         document.getElementById('model').value = Haut+'X'+Larg;
                        } 

ou seja deixei de usar o innerHTML para usar value, assim podendo colocar o valor numa caixa de texto e passar o valor por $_POST como disse o Bruno Bernardino...

Thanks Bruno! Smile
URL's de Referência