1
Basically I’m trying to use value of a select returned via ajax with switch case. This switch case needs to be on the same form submission page using the select value for a specific case.
My script returns the value of select perfectly via ajax. The only problem is that i can’t use this value in the switch case on the current page, for the value of $_POST["aaa"] is not recognized.
I have tried redirecting to the same page using url: this.action in ajax but did not succeed.
Follow below in detail step by step what I need and then the script.
- I take the value of select with onChange
- Displays a hidden div (displays the main form)
- The value is sent to return.php (server)
- I take the value and use the switch case on the current page
- With the switch case custom fields are included for the main form according to what was chosen in select.
index php.
<div class="container">
<form method ="" id="formStart" class="formStart" enctype="multipart/form-data">
<h2 style='color:blue'>Escolha o tipo de anúncio:</h2>
<select name="aaa" id="valorSel" >
<option value="-1">Selecionar...</option>
<option value="1">Imóveis</option>
<option value="2">Veículos</option>
<option value="3">Produtos</option>
</select>
<!-- <input type="Submit" name="submit" value="Next step" /> -->
<br><br>
</form>
<br>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$("#formStart").change(function(event){
$("#formHide").show(); // exibir o form principal (oculto)
event.preventDefault();
$.ajax({
data: $(this).serialize(),
url: "retorno.php",
type: "POST",
dataType: "html",
success: function(data){
$("#resultado").html(data);
},
error: function(){
alert("Problema ao carregar a solicitação via Ajax.");
}
});
});
</script>
<div id="resultado"></div>
<div id="formHide" style="display:none;">
<form action="<?php echo esc_url( $action ); ?>" method="post" id="submit-form" class="submit-form" enctype="multipart/form-data">
<?php
// Usar o valor do select retornado via ajax pela página "retorno.php" como switch case
switch ($_POST["aaa"]) {
case 1:
echo "Imóveis"; // Para testes adicionei um echo e comentei os includes por enquanto.
//include "imoveis.php";
break;
case 2:
echo "Veículos";
//include "veiculos.php";
break;
case 3:
echo "Produtos";
//include "produtos.php";
break;
case -1:
echo "Por favor escolha uma opção.";
break;
default:
echo "Nada foi selecionado ainda. Por favor escolha uma opção.";
break;
}
?>
</form>
</div>
php return.
<?php
if (isset($_POST['aaa'])) {
echo $_POST['aaa'];
} else {
echo "Não retornou nenhum valor com ajax.";
}
From now on I thank you for your attention!
Friend, logic is not cool. If you are going to make an ajax request, why don’t you return the fields you need? For you to return PHP value and deal with Javascript, it would be better to use JSON. But in your case, there is no need. You can "create" your fields directly with ajax, as you said... If you are interested, I can formulate an answer. Of course you’ll have to redo at least half your code...
– LipESprY
Lipespry, I would prefer to handle everything with php, but without ajax I would have to use the Submit button and the refresh page which is not what I need. I tried to do with javascript, but in the call of includes gave error. However your idea is welcome and maybe above my need. Feel free and look forward to your fluffy. Hug.
– Skyline
Still I see no need for ajax, I made a small modification in my reply to meet that remark of yours. Before you go sticking ajax in everything, at least think before. Was it necessary ? really improved user experience ? Or I just found the technology cool, and I decided to swell the load of my application or site, with several lines of javascript code to do something simple?
– user60252