PHP - Form - show results on next page including selected link - php

I have a simple form where I want users to select items and those items appear on the results page. The items also include a link. I would like the link to be included on the results page too. Right now, I click on a check box with a link and the link does not appear on the results page. Not sure where to go from here.
<html>
<body>
Teaching Tools<br><br>
Testing version only<br><br>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
Building: <input type=“text” name=“bldg”><br><br><br>
Teaching Tools:<br><br>
<input type="checkbox" name="videos[]" value="Word"><label>Word</label><br /><br>
<input type="checkbox" name="videos[]" value="Padlet"> <label>Padlet</label><br />
<br>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
Results Page:
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Email: <?php echo $_POST["email"]; ?><br><br>
Your building: <?php echo $_POST[“bldg”]; ?><br><br>
<?php
if(isset($_POST['submit'])) { // to run PHP script on submit
if(!empty($_POST['videos'])) {
// Loop to store and display values of individual checked checkbox.
foreach($_POST['videos'] as $selected) {
echo $selected."</br>";
}
}
}
?>
<br><br>Testing Mode<br>
</body>
</html>

Remove the link from the form and add this instead:
<input type="checkbox" name="link" value="http://www.bluevalleyk12.org">
On welcome.php get the value of link with :
$link = $_POST["link"];
If you want to hide the link use type="hidden":
<input type="hidden" name="link" value="http://www.bluevalleyk12.org">
Finally, if you want the link visible but non editable, use readonly:
<input type="checkbox" name="link" value="http://www.bluevalleyk12.org" readonly>

Related

Undefined Index on checkbox data

I am getting the error: Undefined index: data when running the code below
Here are the key parts of the code including sections that work fine:
I create a form to select dates - this all works fine
<div class="dates">
<form method="POST" action="">
<label for="StartDate">Start Date:</span></label>
<input type="datetime-local" id="StartDate" name="StartDate" value="<?php
if(isset($_POST['StartDate'])){echo $_POST['StartDate'];}?>"/>
<label for="EndDate">End Date:</span></label>
<input type="datetime-local" id="EndDate" name="EndDate" value="<?php if(isset($_POST['EndDate']))
{echo $_POST['EndDate'];}?>"/>
<input type="submit">
</form>
</Body>
</HTML>
</div>
I then run an sql query and add a checkbox to each line of data. I use lead_id as the value. This all displays fine and the value is being assigned correctly:
$result = mysqli_query($link,$query);
while ($row = mysqli_fetch_assoc($result))
{
?>
<form method="POST">
<?php
echo '<tr><td>'.$row['lead_id'].'</td>';
echo '<td>'.$row['status'].'</td>';
echo '<td>'.$row['campaign_id'].'</td>';
echo '<td>'.$row['call_date'].'</td>';
echo '<td>'.$row['user'].'</td>';
echo '<td>'.$row['status_name'].'</td>';
?>
<td><input type="checkbox" value='<?php echo $row['lead_id']?>' name="data[]"/></td></tr>";
</form>
<?php
}
I then have a submit button which is used to trigger an email - that all works aprt from it needs to contain the lead ids from the checked boxes and it doesnt due to this error
?>
<div>
<form method="post">
<input type="submit" name="Button1"
value="Send Email"/>
</form>
</head>
</div>
The error I get is Undefined Index: data
I am using the below code to attempt to output the data on screen as well as var_dump. All come out NULL as clearly for some reason it is not picking up the POST.
if(isset($_POST['data']) && !empty($_POST['data']))
foreach($_POST['data'] as $name) echo $name;
Id be grateful if anyone can see my error.
The issue is:
<form method="POST">
<?php
...
<td><input type="checkbox" value='<?php echo $row['lead_id']?>' name="data[]"/></td></tr>";
</form>
These checkboxes are in a different <form> tag and your submit button is in a different <form> tag which is below these checkboxes:
<div>
<form method="post">
<input type="submit" name="Button1"
value="Send Email"/>
</form>
</head>
</div>
That's why on form submit you are not getting those checkboxes. To resolve this, move checkboxes in the same <form> tag where submit button is.

submitting result to the end of the url instead of adding an index.php

Id basically like the below submission to place text at the end of the url
example of what i want
http://example.com/(text) -- without the () obviously
example of what i don't want -- http://www.example.com/index.php?firstname=text
<form action="(end of current url)">
<fieldset>
search name
<br>
<input type="text" name="search" value="name">
<br>
<input type="submit" value="Submit"></fieldset>
</form>
id like to fix this via html or php either will do aslong as it submits the request to that :)
thank you in advance.
Using the POST method instead of GET.
<form action="" method="POST">
<fieldset>
search name
<br>
<input type="text" name="search" value="name">
<br>
<input type="submit" value="Submit"></fieldset>
</form>
In short you do the following:
<?php
// Get posted text
$text = strtolower(mysql_real_escape_string($_POST['text']));
// Do some cleanup here
// Redirect to page
if ($text != ''){
header( 'Location: http://www.example.com/' . $text );
}
// HTML output below (not before)
?>
<form method="post" action="">
<fieldset>
Search name<br />
<input type="text" name="text" /><br />
<input type="submit" value="Submit" />
</fieldset>
</form>

PHP,HTML Need to direct from one form to another form

I have written a code to get First name,last name and NIC no. If First name is missing I will show a error call "missing" infront of the textbox. All this thing is working and it will send the information in to the same page.
If first name is missing show the error in the same form which will allow user to refill it. If the user entered information correctly, I need to redirect user to another form. How can I do it?
<?php
$fnameerr="";
$name="";
if($_SERVER['REQUEST_METHOD']=="POST")
{
if(empty($_POST["fname"]))
{
$fnameerr="Missing";
}
}
?>
<head>
<title>Untitled Document</title>
</head>
<body>
<form name="form1" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
First Name:<input type="text" name="fname" value="<?php echo htmlspecialchars($name);?>">
<span class="error" style="color:#FF0000">*<?php echo $fnameerr;?></span>
<br/><br/>
Last Name:<input type="text" name="lname" /><br/><br/>
NIC:<input type="text" name="nic" /><br/><br/>
<input type="submit" name="sub" value="Register"/>
<input type="reset" name="re" value="Reset"/>
</form>
</body>
</html>
Use header() to redirect
if(empty($_POST["fname"])) {
$fnameerr="Missing";
}
else {
header("Location: anotherForm.php");
}

How can i pass values from two html pages to php?

I have 2 html pages :
page1.html
<html>
<body>
<form action="page2.html" method="post">
Enter First name: <input type="text" id="text1">
<input type="submit" value="Next">
</form>
</body>
</html>
page2.html
<html>
<body>
<form action="test.php" method="post">
Enter Last name: <input type="text" id="text2">
<input type="submit" value="Submit">
</form>
</body>
</html>
Now, i would like to retrieve the value of text1 from page1.html and text2 from page2.html. How can i go about it?
Looks like you are looking for forms. See this tutorial http://www.w3schools.com/php/php_forms.asp. Post your form data from page1.php to page2.php. On page2.html you can access them via $_POST.
Please be aware: This is not a secure example, just for showing purposes! When you want to show user generated data in your frontend, please use sanitizing and validation http://www.php.net/manual/en/filter.examples.sanitization.php.
page1.php:
<form action="page2.php" method="post">
Enter first name: <input type="text" name="firstName"><br>
<input type="submit">
</form>
page2.php
<h1>Hey <?php echo $_POST['firstName']; ?></h1>
<form action="lastpage.php" method="post">
Enter last name: <input type="text" name="lastName"><br>
<input type="hidden" name="firstName" value="<?php echo $_POST['firstName']; ?>">
<input type="submit">
</form>
lastpage.php
<h1>Yo, my mate <?php echo $_POST['firstName']; ?> <?php echo $_POST['lastName']; ?>!</h1>

More than one html form in a php file

I have a php and html based tool that has a form that, when submitted, outputs the data reformatted using echo commands.
I'd like to add a 2nd form to the same page that will also output using echo.
My issue is, when I submit the 2nd form the first forms output disappears. I'd like to make it so the echo output from the first form does not go away when the 2nd form is submitted so they will both be on the screen at the same time.
Is there a way I can do this?
Only one <form> block in a page can be submitted at a single time. <input> fields defined in one form will not be submitted when the other form is submitted.
e.g.
<form>
<input type="text" name="foo" />
<input type="submit" />
</form>
<form>
<input type="text" name="bar" />
<input type="submit" />
</form>
Clicking on submit will submit either a foo field, OR a bar field. Not both. If you want both fields to be submitted, then you have to either build them into a SINGLE form:
<form>
<input type="text" name="foo" />
<input type="text" name="bar" />
<input type="submit" />
</form>
or use Javascript to copy the data from one form to another.
<form method="post"> <div>Module1</div> <input type="text"
value="module1" name="module_id"> <input type="text" value="title 1"
name="title"> <input type="text" value="some text 1" name="text">
<input type="submit" name="form_1" value="submit"> </form>
<form method="post"> <div >Module2</div> <input type="text"
value="module2" name="module_id"> <input type="text" value="title 2"
name="title"> <input type="text" value="some text 2" name="text">
<input type="submit" name="form_2" value="submit"> </form>
<?php
if(isset($_POST['form_1'])){
echo '<pre>';
print_r($_POST); }
if(isset($_POST['form_2'])){
echo '<pre>';
print_r($_POST); } ?>
Yes,you can do it.
Eg :
// form1 on page a.php
<form method="post" action="a.php" name="form_one" >
<input type="text" name="form_1" value="if(isset($_POST['form_1'])) echo $_POST['form_1']; ?>" >
<input type="submit" name="submit_1" >
</form>
<?php
if(isset($_POST['submit']))
{
?>
<form method="post" action="a.php" name="form_two" >
<input type="text" name="form_2" value="if(isset($_POST['form_2'])) echo $_POST['form_2']; ?>" >
<input type="submit" name="submit_2" >
</form>
<?php
}
?>
Now when you will submit form_one you will see form_two appear and the value in form one will stay intact in form_one and one the submitting form two the value will remain.
Hope it helped :)

Categories