Form submit is not targeting and shaving the value to the php file

Asked

Viewed 155 times

1

I’m having a problem here with the transfer when clicking on Ubmit was to pass to another php file, the values coming from a php loop, tested without the loop goes... more searching the Wordpress database data will not someone has any suggestion?

  <script>
        total = 0;
        
        function adiciona(id)
        {
            calcula(id,"adicao");
        }
        
        function remove(id)
        {
            calcula(id,"subtracao");
        }    
            
        function calcula(id,operacao)
        {
                nomeid  = "nome"+id;
                precoid = "preco"+id;
                qtdid   = "qtd"+id;
                
                nome  = document.getElementById(nomeid).innerHTML;
                
                preco = document.getElementById(precoid).innerHTML;    
                preco = parseInt(preco);
                
                qtd   = document.getElementById(qtdid).innerHTML;
                qtd   = parseInt(qtd);

                //Debug
                //alert("Produto: " + nome + "\n Preço: " + preco);    
                
                if(operacao=="adicao")
                {
                    total = total + preco;
                    qtd = qtd + 1;
                }
                else
                {
                    total = total - preco;
                    qtd = qtd - 1;
                }
                
                document.getElementById(qtdid).innerHTML = qtd;
                
                document.getElementById("total").innerHTML = total;
        }    
            
        
    </script>
		
		
		
		 <!-- REPASSA PARA O PHP -->         

		
		
		
		
		<script>
			function verifica_e_envia()
			{
				array_dados = new Array();
			
				colecao = document.getElementsByTagName("tr");
				
				qtd_blocos = colecao.length - 1; // O último tr da tabela é onde fica o total e está sendo descontado
				// É necessário saber a quantidade de blocos para poder usar em um loop catando os valores
				
				// Percorre os blocos catando nomes, quantidades e valores dos produtos com quantidade maior que zero
				for(i=1; i<=qtd_blocos ;i++)
				{
					qtdid = "qtd"+i;
					qtd   = document.getElementById(qtdid).innerHTML;
					qtd   = parseInt(qtd);
					
					if(qtd>0)
					{
						obj_tmp = {};
						
						nomeid = "nome"+i;
						nome   = document.getElementById(nomeid).innerHTML;
						
						precoid = "preco"+i;
						preco   = document.getElementById(precoid).innerHTML;
						preco   = parseInt(preco);

						obj_tmp.nome  = nome;
						obj_tmp.preco = preco;
						obj_tmp.qtd   = qtd;
						obj_tmp.subtotal = qtd*preco;
						
						// adiciona elemento no array de dados que será enviado
						array_dados.push(obj_tmp);
					}
				}
				
				// põe o array_dados no input hidden json_dados
				document.getElementById("json_dados").value = JSON.stringify(array_dados);
				
				// envia o formulário form_pedido_produtos
				document.getElementById("form_pedido_produtos").submit();

			}
		</script>		
  <table>
		
		  <!-- INICIA Bloco gerado por LOOP PHP -->
		
		
 <?php query_posts('showposts=2category_name=gas');?>

<?php if (have_posts()): while (have_posts()) : the_post();?>
                       
			
			 
        <tr>
            <td class="prodtd">

                <div id="nome<?php the_id(); ?>"  class="nomeprod"> <?php the_title(); ?></div>

                <div id="preco<?php the_id(); ?>" class="preco"><?php echo get_post_meta( $post->ID, 'preco', true ); ?></div>

            </td>
            <td align="center" valign="middle">

                <input type="button" value="-" onclick="remove(<?php the_id(); ?>)"> 

                <span id="qtd<?php the_id(); ?>">0</span> 

                <input type="button" value="+" onclick="adiciona(<?php the_id(); ?>)">

            </td>
        </tr>
      
            
<?php endwhile; else:?>
<?php endif;?>
            
 <!-- FINALIZA Bloco gerado por LOOP PHP -->         
           
            
            <tr>
                <td align="center"><b>Total: <span id="total">0<span></b></td>
                <td>&nbsp;</td>
            </tr>
            
        </table>
        
		
		
		  
		
		<form action="http://localhost/soma_total/pedido_produtos.php" method="post" id="form_pedido_produtos">
			<input type="hidden" name="json_dados" id="json_dados">		
			<input type="button" value="Verifica e envia valores" onclick="verifica_e_envia()">
		</form>

  • Instead of using an input type="button", trial type="submit". In that case you would remove the event onclick input and include in the form, only as onsubmit. This way you don’t need to force via Javascript, and you can cancel the event with a simple return false; if the data is invalid.

  • In the case of <input type="Submit" value="Checks and sends values" onsubmit="verifica_e_sends()">

  • No, like this: <form action="http://localhost/soma_total/pedido_produtos.php" method="post" id="form_pedido_produtos" onsubmit="verifica_e_envia()">

  • <form action="http://localhost/soma_total/pedido_products.php" method="post" id="form_pedido_products"> <input type="Hidden" name="json_data" id="json_data"> <input type="button" value="Checks and sends values" onclick="verifica_e_sends()"> </form>

  • Da esse erro Invalid argument supplied for foreach() in

  • Hemerson, in your form tag that you wrote in the comment above has a semicolon (;) lost inside the tag. &#xA;------------------------------------------------------------------------------------------------------------------------------------------------------------------ Outra coisa:&#xA;Essa função the_id(); já tem um echo dentro dela ou é um return do conteúdo? If it’s just a Return, use echo the_id();

  • This is a PHP error, so congratulations, you managed to post the data. Now it’s another problem.

  • the post identification comes when I add it inside the loop

  • Dude I think I know what’s going on. I’m going to put in an answer because here as a comment the code gets all squeezed.

  • 1

    When I put <input type="Submit" value="Checks and sends values" onclick="verifica_e_send()"> goes further without values

  • When I place action=final.php from this error Invalid argument supplied for foreach() in when I do not place will go further without the values

  • Take a look at this answer below now. Has numbers that are concatenated with names of html ids starting with 1 and being incremented. These ids only serve to javascript find them and send the data. If you put your bank ids it will not follow that order of the right javascript is.

Show 7 more comments

1 answer

0

Dude I think I know what’s going on.

Try the following, instead of using this function the_id(), create a $i variable starting from $i = 1; and inside the loop you put $i++;

Will be next:

<?php 
$i=1;   // $i começando com 1 aqui
if (have_posts()): while (have_posts()) : the_post();?>     

        <tr>
            <td class="prodtd">

                <div id="nome<?php echo $i; ?>"  class="nomeprod"> <?php the_title(); ?></div>

                <div id="preco<?php echo $i; ?>" class="preco"><?php echo get_post_meta( $post->ID, 'preco', true ); ?></div>

            </td>
            <td align="center" valign="middle">

                <input type="button" value="-" onclick="remove(<?php echo $i; ?>)"> 

                <span id="qtd<?php echo $i; ?>">0</span> 

                <input type="button" value="+" onclick="adiciona(<?php echo $i; ?>)">

            </td>
        </tr>

<?php $i++; ?>


<?php endwhile; else:?>
<?php endif;?>

Explanation: Your javascript is looking for these "ids" and is trusting that they are sequential (I know this because I helped with this code in another question of yours, hahaha). But they only serve to help to collect the data from the page, do not have to be bank ids, as is what is happening there. Please give a check and tell me if so resolves.

  • When I put this action="http://localhost/souza/wordpress/index.php" from this error Invalid argument supplied for foreach() when shooting index.php will no longer will the values appears post cannot be sent

  • Let’s go to chat rsrsrs

  • Let’s do it! hahaha

  • I think the chat link only appears when you already have a lot of comment here... Go to the chat of the other topic, but leave this tab open.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.