Jump to content

When submitting a form, how to make it appear the chosen content?


alexndrre

Recommended Posts

Hi guys. As I am new to PHP, I have a question.

Necessarily, this need not be done in php, if you have something in html or js that can be done, let me know! Well, I have a form where you enter information like name, city, phone and product option. These product options include real estate and auto options. By clicking submit (to submit form), I need to direct the visitor to their chosen product option.

For example, when I filled out the form I chose 'real estate'. When I clicked submit, it went to a page where the real estate-only content appeared.

Note: I already have this code ready below and it already directs the information to the database. However, I don't know how to make him redirect to the customer, the product option he chose.

 

      <form action="simulador.php" method="post" name="dados" id="dados" onSubmit="return validaform()">
            <div class="col-md-5 esquerda">
                           Selecione o bem<br/>
                           <select name="tipo" type="text" class="contat3" placeholder="Selecione o bem">
                           <option value="Im�vel"" 
                           style="background-color: #fff;">Imóveis</option>
                           <option value="Autom�vel"" 
                           style="background-color: #fff;">Automóveis</option>
                           <option value="Moto" style="background-color: #fff;">Motos</option>
                          </select><br/>
                         Selecione o plano<br/>
                         <select name="plano" type="text" class="contat3">
                           <option value="Crédito"" 
                         style="background-color: #fff;">Crédito</option>
                           <option value="Parcela"" 
                         style="background-color: #fff;">Parcela</option>
                         </select><br/>
                       
                         
                         <input name="valorcon" type="text" id="valorcon" class="contat3"  placeholder="Digite o valor" maxlength="1000" /><br/>
                       
                       
                         
                         <input name="nomecon" type="text" id="nomecon" class="contat3"  placeholder="Nome"  maxlength="1000" /><br/>
                        
                    
                                       
                       
                        
                         <input name="telefone" type="text" 
						  onkeypress="Mascara('TEL',this,event);"
						 type="text" id="telefone" class="contat3"  placeholder="Telefone" />
						 
						 <!--- 
						 <input name="tel2" type="text" id="tel2" onkeypress="Mascara('TEL',this,event);" /><br/>
						 --->
                   
                   
                         
                         <input name="emailcon" type="text" id="emailcon" class="contat3"  placeholder="E-mail"  maxlength="1000" /><br/>
                   
                      
                       
                         <input name="cidadecon" type="text" id="cidadecon" class="contat3"  placeholder="Cidade"  maxlength="1000" />
                     
                   
                         <br/><br/><br/>
                         <a id="enviar-form" class="button solid-color" href="#">Enviar</a>
                         <input type="submit" id="enviar-form-btn" style="display: none;" />
                    
            </div>         
       </form>

 

Link to comment
Share on other sites

Well, if you can use the product option as an ID in a URL, you could do a simple server-sided redirect with PHP:

 

// You could also use URL parameters. I.e.: ?product_option=real-estate
$destination_url = 'https://example.com/product-option/real-estate';

http_response_code(302); // Set the status code to "302 See Other"
header('Location: ' . $destination_url); // Instruct the client to perform the redirect

 

Afaik, http_response_code automatically handles selecting the right protocol, depending on the client request. If you used header to set response codes, you should remember selecting protocol yourself. It is not enough to simply hard-code the protocol.

Also, make sure to validate input on the server-side as well before communicating with the database.

Link to comment
Share on other sites

2 minutes ago, JacobSeated said:

Afaik, http_response_code automatically handles selecting the right protocol, depending on the client request. If you used header to set response codes, you should remember selecting protocol yourself. It is not enough to simply hard-code the protocol.

If by "protocol" you mean the HTTP response code (like 301, 302, 303, and 307) then you have it backwards: http_response_code() will do exactly what you say, header(Location:) will default to 302 for GET/HEAD and 303 for everything else.

And note that header() supports setting a custom response code at the same time:

header('Location: ' . $destination_url, true, 302);

 

Link to comment
Share on other sites

6 minutes ago, requinix said:

If by "protocol" you mean the HTTP response code (like 301, 302, 303, and 307)...

Nope. I am talking about HTTP/1.0, HTTP/1.1 and HTTP/2.0 part that people often incorrectly hard-code in their scripts when using the header function. If you google things to do with headers, people will even tell you it does not matter which use, which is clearly incorrect.

I did not know about the defaulting behavior when sending the Location header, however. I still got some old PHP code that uses the header function for sending status codes, and this code selects the protocol depending on the SERVER_PROTOCOL variable. It is a neat little DRY complaint function, so I do not have repeated header statements all over my code—that would be annoying to maintain in the long run. But thanks for letting me know about the default.

Moving forward, I should probably replace my code with http_response_code though 🤣

Link to comment
Share on other sites

20 hours ago, requinix said:

Your form is going to simulador.php. Make that script process the form and decide where the user is supposed to go. The URL of the page.

Don't output anything, only send the user to that page with


header("Location: " . $url_to_send_the_user_to);
exit;

 

 

In fact, it directs me through the resultado.php page, because that code is included.

<script language= "JavaScript">
location.href="resultado.php"
</script>
    <?php
}    
?>

I need it to indicate according to the desired option on the form.

Link to comment
Share on other sites

1 hour ago, alexndrre said:

In fact, it directs me through the resultado.php page, because that code is included.

That is the wrong way to redirect a user. Don't do that.

1 hour ago, alexndrre said:

I need it to indicate according to the desired option on the form.

Then do that. I've told you how to redirect (properly) so now you have to figure out where to redirect them to. Because I can't possibly how your application works.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.