Jquery form submit with PHP - php

I need help with what I thought would be a simple form submit problem with Jquery, but am running into issues.
I have a form, #listing_step_1, in which the user first selects a category from a dropdown. When a category has been selected, if the user clicks "next", the subcategory dropdown is revealed. When both a category and subcategory is selected, clicking next will take them to step 2 of the process using a redirect. On submission, the form is reloading the same page using $_SERVER['PHP_SELF'].
I want to autosubmit the form so clicking on next is not required to reveal the subcategories. I'm trying to do this on an onchange event using JQuery, but the following code does not work.
$('#item_category').change(function(){
$('#listing_step_1').submit();
});
I would appreciate any help with this as I am new to form submission using JQuery and AJAX. Manually, it's working fine - i.e., pressing the page button and with the PHP page refresh. I thought by simply submitting the form in the onchange instance it would reload and execute the required actions, but this does not appear to be the case.
Form code:
<form method="post" name="listing_step_1" id="listing_step_1" action="<?php $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data" <?php if($errormessage != ""){ echo ' style="margin-top: 30px"';}?>>
<div class="formField fullwidth">
<label for="item_category">Category *</label>
<select id="item_category" name="item_category">
<option value="0">Select category...</option>
<?php $cd = new CategoryListing("<option_credits>", "</option>"); ?>
</select>
</div>
<div class="formField fullwidth">
<label for="item_subcategory">Subcategory*</label>
<?php if($dropdownlist != "") { ?>
<select id="item_subcategory" name="item_subcategory">
<?php echo $dropdownlist; ?>
</select>
<?php } else { ?>
<select id="item_subcategory" name="item_subcategory" disabled="true">
<option value="0">Select subcategory...</option>
</select>
<?php } ?>
</div>
<input type="submit" value="Next" id="submit" name="submit" style="width: 100px;" />
</form>
Form Submission:
if(isset($_POST['submit'])){
if($_POST['item_category'] != 0){
$dropdownlist = CategoryListing::getSubCategoryDropdown($_POST['item_category']);
}
else {
$errormessage = "Please select a category";
}
}
Jquery:
$(document).ready(function(){
$('#item_category').change(function(){
this.form.submit();
});
});

Try this instead:
$(function() {
$('#item_category').change(function(){
this.form.submit();
});
});

in your description, you say:
I have a form, #listing_stage_1, in
which the user first selects a c...
then in code you say:
$('#item_category').change(function(){
$('#listing_step_1').submit();
});
I imagine you are just using the wrong selector... change your code to this:
$('#item_category').change(function(){
$('#listing_stage_1').submit();
});

Related

php: Form to reveal more options depending on which radio button checked

I'm trying to create a form for entering Computer or Televisions as electronics. I want first the user to select which of the two he's entering, and depending on which one he chooses, he's going to have different stuff to fill in.
I tried to write this but it doesn't work. I get an error on $answer= $_POST['type'];
Please let me know what is wrong, I'm kind of new at php. Thanks.
<div class="form-group">
<h3 style="color:blue;">Type of Item</h1>
<form name ="form1" method ="post">
<input type="radio" name="type" value="Computer"> Computer<br>
<input type="radio" name="type" value="Television"> Television<br>
</form>
</div>
<?php
$answer = $_POST['type'];
if ($answer == "Computer") {
//show stuff
}
else {
//show other stuff
}
?>
$_POST will be empty until the form is actually submitted. Your form does not have a submit button. That said, this type of task is usually better done in javascript since it doesn't require a full page reload just to open up the next set of options in a form.
Here's an example of how this might work using a simple javascript function:
<script type="text/javascript">
function toggleOptions() {
if ( document.getElementById('type_Computer').checked ) {
document.getElementById('nextSetOfComputerOptions').style.display = '';
} else {
document.getElementById('nextSetOfComputerOptions').style.display = 'none';
}
}
</script>
<input type="radio" name="type" id="type_Computer" value="Computer" onclick="toggleOptions();">
<div id="nextSetOfComputerOptions" style="display:none;">
<!-- more form fields -->
</div>

Submit form redirect variable

I'm trying get a form to redirect to a certain page depending on what option is chosen in a select div. When I press submit, it redirects to the home page. I realize it's because it doesn't change $industry_choice until after the I press the button... is there a method to change this the moment you select a value?
Let's say I choose banking, and $industry_choice = "banking".
if(empty($_POST['industry'])) {
$industry_choice = "";
} else {
$industry_choice = $_POST['industry'];
}
<form method="post" action="http://localhost:8888/wordpress/<?php echo $industry_choice ?>">
<select name="industry">
(lots of options here)
</select>
<input type="submit" class="select-industry" value="Select">
</form>
You are doing it wrong. I think you are not clear with the action attibute of the form.
<?php
if(empty($_POST['industry'])) {
$industry_choice = "";
} else {
$industry_choice = $_POST['industry'];
header('Location: http://localhost:8888/wordpress/'.$industry_choice);
}
?>
<form method="post" action="">
<select name="industry">
(options)
</select>
<input type="submit" class="select-industry" value="Select">
</form>
action attribute will redirect to the page you processed the input. After that you will redirect it to targeted location.

redirect with php buttons in existing item-post.php

<div class="row">
<label for="AdsType"><?php _e('Ads Type', 'modern'); ?></label>
<select id="AdsType" name="AdsType" >
<option>Standard</option>
<option>Premium</option>
</select>
</div>
once user selects Standard button I want it to be redirected to www.site.com/complete.php
once user selects Premium button I want it to be redirected to www.site.com/pay.php
What is the extra coding I add in my existing php file before:
<button type="submit"><?php _e('Publish', 'modern'); ?></button>
You can change action url which is in the form using javascript
// call to change_action() function onChange event
<select id="AdsType" name="AdsType" onChange="change_action();" >
<option value="Standard">Standard</option>
<option value="Premium">Premium</option>
</select>
// change action url
<script language="javascript">
function change_action()
{
if(document.getElementById("AdsType").value=="Standard")
{
form_name.action="www.site.com/complete.php";
}
else
{
form_name.action="www.site.com/pay.php";
}
}
</script>

Form Submit Button Works, but not Submit() in Link

I've done this so often before on different websites, but can't get it to work now.
I've got a simple form that posts perfectly well using a submit button, but for a specific reason I actually need it to submit via a url link instead. I'm using submit(). The form submits, but the data isn't posting.
What am I missing?
<html>
<body>
<?
if(isset($_POST['bar'])) { echo 'testing button<br>'; }
if(isset($_POST['information'])) {
echo $_POST['information'];
echo '</br>Info successfully posted.';
}
?>
<form action="test.php" method="post" id="fooform">
Hello World.<br>
Select checkbox: <input type="checkbox" id="information" name="information" value="yes">
<input type="submit" name="bar" value="Send"><br>
Confirm and Post<br>
Post Directly
</form>
<script type="text/javascript">
function SubmitForm(formId) {
var oForm = document.getElementById(formId);
alert("Submitting");
if (oForm) { oForm.submit(); }
else { alert("DEBUG - could not find element " + formId); }
}
</script>
</body>
</html>
The form starts to submit, then the href of the link is followed, and this cancels the form submission.
If you are using old-style onclick attributes, then return false; at the end to prevent the default action.
You would, however, be better off using a submit button (you are submitting a form). You can use CSS to change its appearance.
Try this code :
<html>
<body>
<?php
if (isset($_POST['bar'])) {
echo 'testing button<br>';
}
if (isset($_POST['information'])) {
echo $_POST['information'];
echo '</br>Info successfully posted.';
}
?>
<form action="test.php" method="post" id="fooform">
Hello World.<br>
Select checkbox: <input type="checkbox" id="information" name="information" value="yes">
<input type="submit" name="bar" value="Send"><br>
Confirm and Post<br>
Post Directly
</form>
<script type="text/javascript">
function SubmitForm(formId) {
var oForm = document.getElementById(formId);
alert("Submitting");
if (oForm) {
oForm.submit();
}
else {
alert("DEBUG - could not find element " + formId);
}
}
</script>
</body>
</html>
try to submit form with form id in jquery
<a class="submit">Post Directly </a>
$('a.submit').click(function(){
$('#fooform').submit();
})

Jquery not submitting my form

Hi I want to submit my form using JQuery but I don't know why it won't work for me. here's the code:
$("#search_result_fake_div2").live("click", function () {
$("#search_result_present_list2").show("fast");
$('body').one('click',function() {
$("#search_result_present_list2").hide();
});
event.stopPropagation();
});
$(".search_result_list_item2").live("click", function () {
$("#search_result_fake2").val($(this).html());
$("#getattendees").submit();
$("#search_result_present_list2").hide();
});
and my HTML code:
<div>
<div id="search_result_fake_container">
<div id="search_result_fake_div2"></div>
<form method="GET" id="getattendees">
<input type="text" id="search_result_fake2" value="<?php if(!empty($city)){echo $city;} else{echo "Select Event";}?>" name="event_name">
</form>
</div>
<div id="search_result_present_list2">
<?php foreach ($events as $events1): ?>
<div class="search_result_list_item2" id="search_result_item_12" style="text-align:center;"><?php echo $events1['event_name']; ?></div>
<?php endforeach ?>
</div>
</div>
This is a drop down menu using div which came from my other question. One thing that I want to happen is for the form to submit once I've clicked a content from the drop down selection. I tried using onchange="javascript: document.getattendees.submit();" on my input box but nothing happens so I'm thinking of using jQuery instead but still the problem persists. I'm just a newbie on the jQuery and in fact I'm not that well versed on this one.
try this:
$('form#getattendees').trigger('submit');

Categories