How to incorporate a div to the form article or the other way around?

Asked

Viewed 137 times

0

I’m developing a Fomulário for a project and I needed to add images only that these images represent the place of the client’s chairs and when the user clicks on any of these chairs the place is marked, only that it is necessary to have this marked location displayed on another page next to the other form data like name, password, etc. Thus remaining:

Name: Java

Password: 123

Chosen chair: 1 - A , (And other chairs if the user has chosen more than one)

Unlike the form these images representing the client’s place are inside a div(making it easier to style them by CSS), and I came across the following error when I tried to create a variable in my PHP file for it to appear as the other information typed by the user:

Notice: Undefined index: tCadeira1 in C:\xampp\www\teste\dados.php on line 54

"tCadeira1" was the variable I created in PHP. Now I don’t know how to do it because I tried to put the closing of the tag form after div’s, inside the div but it’s no use.

My HTML file:

<form action="dados.php" method="post">

    <input type="text" id="cNome" name="tNome" title="Com no máximo 20 letras" size="20" maxlength="20" placeholder="       Digite Seu Nome"/>

    <input id="submit" type="image" src="imagens/ButtonAceitar.png" />
    <input type="reset" id="resetar" value=""  />
</form>
<div id="cadeiras">
    <img src="imagens/cadeiraAzul.png" name="tCadeira1" onClick="cor(this)">
    <img src="imagens/cadeiraAzul.png" name="tCadeira2" onClick="cor(this)">
    <img src="imagens/cadeiraAzul.png" name="tCadeira3" onClick="cor(this)">
    <img src="imagens/cadeiraAzul.png" name="tCadeira4" onClick="cor(this)">
</div>

My PHP file:

<?php

$nome = $_POST["tNome"];
$cadeira = $_POST["tCadeira1"];

echo "$nome, obrigado por se cadastrar e comprar o seu ingresso em nosso site.<br>Cadeira(as) escolhida(as): $cadeira ";

?>

My JS file:

function cor(e) {
  if (e.getAttribute("src") == "imagens/cadeiraAzul.png") {
    e.setAttribute("src", "imagens/cadeiraPreta.png");
  } 

  else {
    e.setAttribute("src", "imagens/cadeiraAzul.png");
  }
}

Show solutions with possible tag languages of this question, if possible.

2 answers

2

This error is PHP and not HTML, $_POST with input, select, textarea and button, no use putting name="" that he won’t recognize.

Maybe use input type=image resolve:

<div id="cadeiras">
    <input type="image" src="imagens/cadeiraAzul.png" name="tCadeira1" onClick="cor(this)">
    <input type="image" src="imagens/cadeiraAzul.png" name="tCadeira2" onClick="cor(this)">
    <input type="image" src="imagens/cadeiraAzul.png" name="tCadeira3" onClick="cor(this)">
    <input type="image" src="imagens/cadeiraAzul.png" name="tCadeira4" onClick="cor(this)">
</div>

Something I always say, stop doing things random, most of the mistakes are in this, has documentation of almost everything that is popular technology and there are still alternative documentations, read and learn first, does not invent from the head that will give problem.

Documentation and references

  • I tried to put the input but it did not recognize the same way, I tried to put the tag form after the div and when I clicked on the image it redirected to the other page with the user data containing the same error.

  • @Lonetonberry inputs are inside the FORM? No? If not it will not send even

  • When I did what you said I tried with the FORM tag above the div and it was not after tried to put after and it switched to the page.

  • It is not above, the inputs have to be within <form> </form>, no use putting away.

  • Yes but I just want to add to the form apart from appearing the data typed by the user also appear typed on the other page the chairs chosen with onClick.

  • @Lonetonberry but you’re already saying something else, sorry to be honest, it’s not bad, but I think you don’t even know the basics of HTML and you’re driving all this like a runaway car, a lot of your mistake is for skipping the basics. For example, what the function cor() does? You didn’t inform. I’m not saying anything badly, I’m telling you to learn the best way possible without suffering so much.

  • I will edit by placing this part of the JS, it changes the color of the chair passing the impression that it was selected by the user and if he clicks again it changes to the previous color, doing this with two images only.

Show 2 more comments

1

There’s some stitches you need to fix:

  • Everything you post needs to be inside the form (the div #chairs needs to go inside the form)

  • Images are not sent this way in a form, you need an input with the name attribute and the value of the selected chair, images will not work.

Given this, what you can do is the following

  • When the user selects an image, you add to the form in a javascript event an input with type Hidden with the value of the selected chair, the rest should work perfectly.

With Jquery it would be as follows

$('img').on('click', function(){
    $(this).addClass('selected');
});
$('form').on('submit', function(){
   var form = $(this);
   $('img.selected').each(function(){
      var html = '<input type="hidden" name="NOME PARA REFERÊNCIA NO PHP" value="VALOR DA IMAGEM SELECIONADA" />'
      form.prepend(html);
   });
});
  • A tip: It is not given example like this when the guy does not even know the basics, in addition to make him skip the necessary steps of learning yet you give cloth for him to mess up more and appear more errors. ;)

  • The only languages that I’m learning at the moment are HTML, PHP, JS and CSS, apart from these, I’m not going to add to my project something I don’t even know where it goes. Sorry you haven’t clarified this before, I’ll edit the question by adding this.

  • If you’re just getting started, the best thing you can do is to get on the Mozilla Developer Network website that @Guilhermenascimento recommended, has everything you need and is very easy to understand. The problem is that simple things involve several concepts that, when you’re a beginner, you don’t even realize.

Browser other questions tagged

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