This is probably a really stupid question, but I can't figure out why my code is doing this. I'm trying to do a simple recordset update and yet when I hit the submit button on the page, it will reload the page and in the url have multiple url parameters of the info from the form.
I have two forms in the whole document, one, the one I'm working with is supposed to change a variable in the database to the username of the user
<form action="<?php echo $editFormAction; ?>" method="POST" name="Catch">
<input name="Horseid" type="hidden" value="<?php echo $colname_WildHorse?>"/>
<input name="Owner" type="hidden" value="<?php echo $colname_HorseImage?>"/>
<input name="Catch" type="submit" value="Catch"/>
<input type="hidden" name="MM_update" value="Catch"/>
</form>
This is the one that returns all the url stuff after I hit the submit button. The second form is under it, and I want that one to display in the Url, which it does perfectly fine. Why is this one freaking out though?
URL produced:
http://localhost:8888/TheMeadow.php?Horseid=38&Owner=redlilac78&Catch=Catch&MM_update=Catch
This is the result code: I left out the style sheet since it's a lot
<title>The Meadow</title>
<style type="text/css">
//Left out style
</style>
</head>
<body>
<div id="Background">
<div id="Logo"><img src="Images/RR_Art/Website pages/Logo/Ropin' Ranch logo.gif" width="229" height="192" /></div>
<div id="Map">Map</div>
<div id="Directories">Directories</div>
<div id="LogOut">Log out</div>
<div id="Note">
<form action="" method="get">
<p>
Welcome to the wild, where you can catch and search for wild horses! You can only catch one horse every two days so choose your horse wisely! </p>
<div id="Name"><br> <br> <br> <br> <br>
<form name = "WildHorse" id = "WildHorse" onSubmit="TheMeadow.php?HorseId43">
<input type = "hidden" name = "HorseId" value = "43"/>
<input type = "submit" name = "" value = "Search" />
</form>
</div>
</div>
</body>
In your form action the problem is with $editFormAction in php , it is either empty or having the same files name due to which the form is being submitted to the same page.
Related
I am very new to PHP and I am trying to display the value of a selected radio button in a string. But all I get returned is "on" instead of the value associated with the selection. Any help would be great!
<form action="Handle_form_PHP.php" method="post">
<div class= "row">
<div class ="col-25">
<label for="student">Student</label>
</div>
<div class ="col-75">
<input type="radio" name="student" value ="student">
</div>
</div>
<div class= "row">
<div class ="col-25">
<label for="student">Employee</label>
</div>
<div class ="col-75">
<input type="radio" name="student" value = "employee">
</div>
</div>
<div class="row">
<input type="submit" value="Submit">
</div>
</form>
This is the PHP I am using to print:
// Create a shorthand for the form data:
$fname = $_REQUEST['firstname'];
$lname = $_REQUEST['lastname'];
$address = $_REQUEST['address'];
$state1 = $_REQUEST['state1'];
$state2 = $_REQUEST['state2'];
$student = $_POST['student'];
// Print the submitted information:
echo "<p>Thank you, <b> $student </b>, <b>$fname $lname</b>, for the filling out our survey:<br>
<p>We will Send you your report at: <i>$address</i>, <i>$state1</i><i>$state2</i>.</p>\n";
If your $_POST['student'] superglobal variable was holding a value of 'on', then the issue has nothing to do with method case-sensitivity.
The default value for a radio button is 'on' unless you provide a value attribute inside the radio button tag.
Let me prove it to you. Go to [cringe] https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_form_checkbox then paste the following snippet in the left-side box, then Run, then Submit.
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php" method="get">
<input type="radio" name="student" checked> Student<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
This is submitting with a method of get (notice the lowercase get is inconsequential. post will behave the same way). See that without a value in the student radio input, the submitted value is on.
If you write value="Red42" inside the radio input, you will no longer receive on; the submitted value is Red42.
use POST not post when saying method:
<form action="Handle_form_PHP.php" method="POST">
not
<form action="Handle_form_PHP.php" method="post">
For the record, the book was incorrect and had "post"
I'm trying to get user input in a progressive sequence that leads to that input being sent by email. Sending by email is a whole other issue that I haven't tackled yet so not really worried about that.
The part I am having difficulty with is once the user gets to the "Send Email?" (Yes/No) radio buttons, the input from that question is not processed correctly.
I've gotten further with this by using a separate php file as the form action but still get errors related to emailName, emailAddress, and emailMsg not existing ("Notice: Undefined index...").
Furthermore, I still need to be able to use the $_POST[athletes] array further down but I'm guessing it's outside of the variable scope at that point.
So to bring that all together, I'm really asking a few questions:
1) How can I get all of the forms to work together in the same file?
2) When the program actually goes past the "Send Email?" radio buttons when I use a separate php file as the form action, why am I getting undefined index errors?
3) Why do I get an error when I try to use the athletes[] array further down in the code? Should I somehow be passing the array values to that part of the code?
The exact steps the user would take to get to the issue is:
Select 1 or more athlete checkboxes and click the 'Display Selection(s)' button.
Select 'Yes' for "Send Email?" and click the 'Submit' button.
Restarts the code for some reason.
Any help would be greatly appreciated. Also, this is my first post so sorry if I asked the question incorrectly or not according to site etiquette.
I also apologize for the long code fragment but I'm not sure what parts might be causing this to be incorrect.
<b><h1><center>Athelete Selection Screen</center></h1></b>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>
<fieldset>
<legend>Athletes Available: </legend>
<input type="checkbox" id="student1"
name="athletes[]" value="Student1 Test">
<label for="student1">Student1 Test</label><br/>
<font color="grey">Football - Running back</font><br/>
<p>
<input type="checkbox" id="student2"
name="athletes[]" value="Student2 Test">
<label for="student1">Student2 Test</label><br/>
<font color="grey">Soccer - Left Forward</font><br/>
</p>
<p>
<input type="checkbox" id="student3"
name="athletes[]" value="Student3 Test">
<label for="student1">Student3 Test</label><br/>
<font color="grey">Baseball - Pitcher/Left Outfield</font><br/>
</p>
</fieldset>
<p>
<?php echo("\t\t\t\t\t"); ?><button type="submit" name="submit" value="submit">Display Selection(s)</button>
</p>
</form>
<fieldset>
<legend>Athletes You Selected: </legend>
<?php
if (!empty($_POST['athletes']))
{
echo "<ul>";
foreach($_POST['athletes'] as $value)
{
echo "<li>$value</li>";
}
echo "</ul>";
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>
<fieldset>
<legend>Send Email? </legend>
<input type="radio" id="Yes"
name="radioSendMsg[]" value="Yes">
<label for="student1">Yes</label>
<p>
<input type="radio" id="No"
name="radioSendMsg[]" value="No">
<label for="student1">No</label><br/>
</p>
<button type="submit" name="submitRadio" value="submit">Submit</button>
</p>
</form>
<?php
if (!empty($_POST['radioSendMsg']))
{
foreach($_POST['radioSendMsg'] as $radioMsg)
{
if($radioMsg == "Yes")
{
echo "\tPlease enter information regarding the email to be sent: ";
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>
<label for="emailName"> Name: </label><br/>
<input type="text" size="25" id="emailName" name="emailName" />
</p>
<p>
<label for="emailAddress">E-mail Address: </label></br>
<input type="text" size="25" id="emailAddress" name="emailAddress" />
</p>
<p>
<textarea id="emailMsg" name="emailMsg" cols="30" rows="5"></textarea>
</p>
<button type="submit" name="emailSubmit" value="send">Send Message</button>
</form>
<?php
$msg = "Name: ".$_POST['emailName']."\n";
$msg.= "E-Mail: ".$_POST['emailAddress']."\n";
$msg.= "Message: ".$_POST['emailMsg']."\n";
$msg.= "<ul>";
foreach($_POST['athletes'] as $value)
{
$msg.= "<li>$value</li>\n";
}
$msg.= "</ul>";
$emailRecipient = "sjzerbib#gmail.com";
$emailSubject = "Athlete Selection Submission";
$emailHeaders = "From: Sebastien\n"."Reply-To: ".$_POST['emailAddress'];
mail($emailRecipient,$emailSubject,$msg,$emailHeaders);
echo "Message sent: \n".$msg;
}
else
{
?> <p /> <?php
echo "\n\nNo email will be sent for your last athlete selection.";
?>
<br/>Please click here
to return to the Athlete selection screen.
<?php
}
}
}
}
When you submit a form, only those controls contained within that form are included. The exception is successful controls that have the form attribute set to the id value of the form that was submitted.
So, given you had something like:
<form id="form-1" method="post">
<input type="text" name="first-input" />
</form>
<input type="text" name="second-input" />
The only value to be submitted would be that of first-input. If you add the form attribute to second-input:
<input type="text" name="second-input" form="form-1" />
Then the submission of the form would include both values. Unfortunately, the form attribute is not fully supported (IE and Edge have no support).
Of course, your markup is invalid, so that's a moot point. For starters, you cannot nest a form within a form. How a browser responds to markup that violates that rule is up to it's vendor, but in my experience is somewhat unpredictable. You're also using deprecated tags (<font> and <center> are no longer valid) and nesting elements incorrectly (<h1> is a block level element, whereas <b> is inline).
If you're doing a full submit each time (so the page gets submitted to itself and then reloads), then just use some sort of conditional to only render the dependent controls if the preceding form submissions were successful:
<?php
$canDoNextStep = !empty($_POST['input-1']);
?>
<form id="form-1" method="post">
<input type="text" name="first-input" />
<?php if(canDoNextStep): ?>
<input type="text" name="second-input" />
<?php endif; ?>
</form>
Lastly, whitespace is (mostly) ignored when your browser parses and displays your HTML, so you can lose the \t and \n values in your strings, unless you're concerned about how your markup looks if someone chooses to view source when using your form.
On my web page, when I press the "Add Customer" link, the following happens:
the onclick handler is called, which
sets values into the forms two text fields
displays an alert (at this point you can see the new values in the text fields on the screen)
calls the form's submit button (Note: the form submits back to it's own PHP file)
The same php file is called, but there are no POST values received
I've verified this with
the code in the first line that counts the values in $_POST then displays later
using fiddler to look at the request sent to the page
I've done this type of thing numerous times before with no problems. I've copied this code to two different web servers (linux, apache) with the same result. It's probably a minor problem but I can't find it.
I've stripped out a whole bunch of code to get down to this little bit, but haven't figured out why no POST values are being sent.
You can see a working copy of this at http://www.daleann.org/dla.php. The only thing need besides the code below is /js/jquery.min.js.
Thanks for your help.
<?php
$pc=count($_POST)."<br />".date("H:i:s");
?>
<html>
<head>
<script src="/js/jquery.min.js" /></script>
<script type="text/javascript">
$(document).ready(function() {
alert("inside docReady");
$(document).on('click', "a.menuBillingOwner", function() {
$("#selectedBillingOwner").val("11");
$("#lastCustomNavSelected").val("selectedBillingOwner");
alert("selectedBillingOwner = "+document.forms['formBillingOwner'].elements['selectedBillingOwner'].value);
document.forms['formBillingOwner'].submit();
});
});
</script>
</head>
<body>
<ul id="menuBillingOwner">
<li><a href='#' id='menuBillingOwnerAdd' class='menuBillingOwner'>Add Customer</a></li>
</ul>
<?php
$lastCustomNavSelected = $selectedBillingOwner = "";
if (count($_POST) > 0 && isset($_POST['selectedBillingOwner'])) {
$lastCustomNavSelected = "selectedBillingOwner";
$selectedBillingOwner = $_POST['selectedBillingOwner'];
}
?>
<?php echo "pc = ".$pc."<br />\n"; ?>
<form name="formBillingOwner" id="formBillingOwner" method="POST" action="/dla.php">
<input type="text" id="lastCustomNavSelected" value="<?php echo $lastCustomNavSelected; ?>" />
<input type="text" id="selectedBillingOwner" value="<?php echo $selectedBillingOwner; ?>" />
</form>
</body>
</html>
Simple, because your form fields don't have name attributes, see below
<form name="formBillingOwner" id="formBillingOwner" method="POST" action="/dla.php">
<input type="text" id="lastCustomNavSelected" value="<?php echo $lastCustomNavSelected; ?>" />
<input type="text" id="selectedBillingOwner" value="<?php echo $selectedBillingOwner; ?>" />
</form>
Add names to them, for example:
<form name="formBillingOwner" id="formBillingOwner" method="POST" action="/dla.php">
<input type="text" id="lastCustomNavSelected" name="lastCustomNavSelected" value="<?php echo $lastCustomNavSelected; ?>" />
<input type="text" id="selectedBillingOwner" name="selectedBillingOwner" value="<?php echo $selectedBillingOwner; ?>" />
</form>
Note: Probably your jQuery assignments need to be fixed too but if that was the only issue then atleast a wrong value should have been POSTed to PHP, hence that is not the issue.
Delete these 2 lines of your jQuery.
$("#selectedBillingOwner").val("11");
$("#lastCustomNavSelected").val("selectedBillingOwner");
because that will change the value of textfield before your submit the form
Weird question this, going round in circles.
I have 2 pages.
Page 1. Has a button on it. Like
<form action="goto_page_2.php"><p class="longdesc"><span class="spanBold">Scrap Collections in North Lakes:</span><br />
<button class="readViewMoreBtn" value="North Lakes">Book a Collection in North Lakes</button>
</p></form>
The above is a simple mockup. But what I want to do is, onclick of the button, parse the Value to ...
Page 2. Which has a form built in.
One of the fields is Suburb.
<!-- EMAIL SUBURB -->
<span class="commonControlLabel">Suburb:</span>
<span class="commonControlLabelItalic">(required)</span>
<span id="contactSuburbErrorMsg" class="commonControlErrorMsg"></span><br />
<input class="commonInput" type="text" id="inputSuburb" value=""/><br />
So what I want to do, is grab the VALUE of the button on PAGE 1, and add it to the Value in the input element on Page 2.
To complicate matters, Page 1. Has quite a few buttons, with different values, all unique, which we would like to pass, to the input element. Obviously onlcick of the button on page 1 we go direct to page 2.
Have the button post it's value to Page2:
Page1, I added type="submit" and name="suburb"
<button type="submit" name="suburb" class="readViewMoreBtn" value="North Lakes">Book a Collection in North Lakes</button>
Page2: I added the php part in the value attribute
<input class="commonInput" type="text" id="inputSuburb" value="<?= $_POST['suburb'] ?>"/>
<!--page1::-->
<html>
<body>
<form action="test2.php" method="post">
<button name="desc" value="1">description!</button>
</form>
</body>
</html>
<!--page2::-->
<?php
echo "hello";
echo $_POST["desc"];
?>
There are several things you can do.
E.g: on form 2 say:
<input type="text" value="<?php if(isset($_POST['inputNameForm1'])){echo htmlentities($_POST['inputNameForm1']);} ?>
Or if that is not an option for you, try something like:
<? php
session_start();
$_SESSION['sessionName'] = $_POST['inputNameForm1']?>
<input type="text" value="<?php if(isset($_SESSION['sessionName']])){echo htmlentities($_SESSION['sessionName']]);} ?> />
Note: didn't test the code
I seem to be having a very simple problem but I can't wrap my head around the issue. Thanks in advance for any help you may have.
First, here is a description of the code:
The following snippet of code is from a user's activity page. This snipped displays all bids that have been placed on a user's "canvas."
<table width="400" align=center frame=below>
<ul>
<?php for ($j = 0 ; $j < $numBids ; ++$j) { $cbid= mysql_fetch_row($bids); ?>
<li><strong>$<?php echo "$cbid[2]"; ?>.00 </strong> by <?php $name=getUser($cbid[4]); echo "$name"; ?>
<br/>"<?php echo "$cbid[3]"; ?>"<br/>
<p align=CENTER>
<!-- Message Artist-->
<form method="post" action="messages.html?msg=<?php echo $cbid[4]; ?>" >
<input type="submit" name="message" value="Message Artist" >
</form>
<!-- Accept Bid-->
<form method="post" action="contract.html?bid=<?php echo "$cbid[0]"; ?>" class="login-form">
<input type="submit" value="ACCEPT BID!" >
</form>
</p>
</li>
<?php } ?>
</ul>
If $numBids = 3, The html output of this code is:
<ul>
<li><strong>$150.00 </strong> by Artist Name
<br/>"I would like to paint this in acrylic."<br/>
<p align=CENTER>
<!-- Message Artist-->
<form method="post" action="messages.html?msg=151" ><input type="hidden" name="sid" value="d0ba8340cdb156c233baf364960ac3e9" />
<input type="submit" name="message" value="Message Artist" >
</form>
<!-- Accept Bid-->
<form method="post" action="contract.html?bid=76" class="login-form"><input type="hidden" name="sid" value="d0ba8340cdb156c233baf364960ac3e9" />
<input type="submit" value="ACCEPT BID!" >
</form>
</p>
</li>
<li><strong>$150.00 </strong> by Artist Name
<br/>"I would like to paint this in acrylic."<br/>
<p align=CENTER>
<!-- Message Artist-->
<form method="post" action="messages.html?msg=151" ><input type="hidden" name="sid" value="d0ba8340cdb156c233baf364960ac3e9" />
<input type="submit" name="message" value="Message Artist" >
</form>
<!-- Accept Bid-->
<form method="post" action="contract.html?bid=72" class="login-form"><input type="hidden" name="sid" value="d0ba8340cdb156c233baf364960ac3e9" />
<input type="submit" value="ACCEPT BID!" >
</form>
</p>
</li>
<li><strong>$150.00 </strong> by Artist Name
<br/>"I would like to paint this in acrylic."<br/>
<p align=CENTER>
<!-- Message Artist-->
<form method="post" action="messages.html?msg=169" ><input type="hidden" name="sid" value="d0ba8340cdb156c233baf364960ac3e9" />
<input type="submit" name="message" value="Message Artist" >
</form>
<!-- Accept Bid-->
<form method="post" action="contract.html?bid=70" class="login-form"><input type="hidden" name="sid" value="d0ba8340cdb156c233baf364960ac3e9" />
<input type="submit" value="ACCEPT BID!" >
</form>
</p>
</li>
</ul>
A live test version of the page has been set up: http://www.canvasmatch.com/test/activity.html.
Now to the problem:
If a user clicks the "Message Artist" button, the user jumps to a messages page where the can send a message to the artist, obviously. This button works for all bids EXCEPT on the first one. If you were to click the first "Message Artist" button it would just refresh the page, incorrectly. However if you clicked the second "Message Artist" button it would jump to messages.html appropriately. If there were say 6 bids, all of the "Message Artist" buttons would work EXCEPT the first one.
The weird thing for me is that all "Accept Bid" buttons work, even the first one.
Has anybody experienced anything like this before?
Any thoughts on what I should be looking for?
Do you need the entire html file to find the problem?
I appreciate any help you can provide!
Thanks,
Jake
Basically, when you inspect the element with Firebug you'll see it's not encapsulated within a form. Something strange is going on because the HTML actually looks fine for that particular element. The rest of the document is full of errors though, so while I can't pinpoint it directly, I'd recommend first at least fixing the majority of the errors that can be found by doing a W3C Validation test. I can almost guarantee that it'll work once these are fixed.
http://validator.w3.org/check?uri=http%3A%2F%2Fwww.canvasmatch.com%2Ftest%2Factivity.html
It looks like you need quotes around $cbid[4]
Current:
<form method="post" action="messages.html?msg=<?php echo $cbid[4]; ?>" >
Change to
<form method="post" action="messages.html?msg=<?php echo "$cbid[4]"; ?>" >
If $numBids = 3, The html output of this code is:
Are you setting $numBids to 3 each iteration? If it is a comparison, it should be $numBids == 3 but it's a bit ambiguous because the snippet is not in the code block.
The issue is the increment for $j. The for statement should look like:
for ($j = 0 ; $j < $numBids ; $j++)
++$j increments first then does the loop so instead of the first time being 0, it ends up 1. The quotations around the variable in the php script is not necessary.
I do have a question, Why are you using a for loop instead of a while loop with a counter defined before the loop and an increment at the end?
EDIT
Just remembered had an issue with a gallery that I was using forms on and the first one did not work. To fix, I had to add a random string name to each one. Try adding a name to each form tag with a random generated string or number.
Example:
<form method="post" action="messages.html?msg=151" class="login-form" name="fEd5t89">
Edit - Working
After looking at the files it was found that this was a nested <form>. After removing the wrapping <form>, the page started working properly.