Echo Submit Button in PHP - php

I am trying to echo a submit form in my php code:
I am trying the following:
// Display results
foreach ($media->data as $data) {
echo "<a href=\"{$data->link}\"</a>";
echo "<h4>Photo by: {$data->user->username}</h6>";
echo $pictureImage = "<img src=\"{$data->images->thumbnail->url}\">";
echo "<h5>Like Count for Photo: {$data->likes->count}</h5>";
echo "<form>";
echo "<form action='tag.php' method='post'>";
echo "<input type='submit' name='submit'>";
echo "</form>";
}
Then:
if(isset($_POST['submit'])) {
echo "hello";
}
This doesn't seem to echo "hello";
Any help would be appreciated

You're missing a closing parenthesis:
if(isset($_POST['submit'])) {
^
Here

You're missing a value on the input tag. Change it to:
echo "<input type='submit' name='submit' value='Submit'>";

echo '<input type="submit" class="btn btn-primary" value="Active">';

It should be
if(isset($_POST['submit'])) {
^
echo "hello";
}
you missed a parenthesis.
Edit: Also it should be:
<input type='submit' name='submit' value='submit'>
Else $_POST['submit'] will not get set.

Related

In echo sumbit button not redirecting in php

echo html code onclick of button redirection.
I have tried by using button and input tags both.
while($_row = $result->fetch_assoc())
{
echo "<input type=\"checkbox\" value=\"".$_row['id']."\">" . $_row['id'] ." ".$_row['name'].' '.$_row['contact'].'';
echo"<input type='button' value='DELETE' onclick='document.location.href='/project1/delete.php''";
echo"?id=".$_row['id'];
echo" />";
echo"<input type='button' value='EDIT' onclick='document.location.href='/project1/edit.php''";
echo"?id=".$_row['id'];
echo" /><br>";
$id=$_row['id'];
}
1- In your code 'Unexpected end of input' issue is there with onclick.
2- you can use concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side. and last display the output.
<?php
while($_row = $result->fetch_assoc())
{
$id = $_row['id'];
$name = $_row['name'];
$contact = $_row['contact'];
$output ='';
$output.="<input type=\"checkbox\" value=\"".$id."\">" . $id ." ".$name.' '.$contact;
$output.="<input type='button' value='DELETE' onclick=\"document.location.href='/project1/delete.php?id=$id'\"/>";
$output.="<input type='button' value='EDIT' onclick=\"document.location.href='/project1/edit.php?id=$id'\"/>";
$output.="<br/>";
echo $output;
}
?>
Replace with following code:
echo"<input type='button' value='EDIT' onclick='document.location.href=\"/project1/edit.php";
echo "?id=1\"'";
echo" /><br>";
?>

On submit i need to pass anchor tag value in php

while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
echo "<form method='post'><a href='".$row['user_from']."' name='yo' value'".$row['user_from']."'>".$row['user_from']."</a> &nbsp";
echo "<input type='submit' name='acc' value='Accept'> &nbsp <input type='submit' name='cancel' value='Reject Rrquest'></form> <br/><br/>";
}
if(isset($_POST['acc']))
{
}
Blockquote
//on submit here i need to disply corresponding $row['user_from']
value
Use <input type="hidden" name="whateveryouwant" />, if you don't want to display text field to user.
Try this:
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
echo "<form method='post'><input type='hidden' name='user_from' value='".$row['user_from']."' /><a href='".$row['user_from']."' name='yo' value'".$row['user_from']."'>".$row['user_from']."</a> &nbsp";
echo "<input type='submit' name='acc' value='Accept'> &nbsp <input type='submit' name='cancel' value='Reject Rrquest'></form> <br/><br/>";
}
if(isset($_POST['acc']))
{
echo $_POST['user_from'];//echo the value of user_form
}
<?php
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
?>
<form method="post" action="">
<?php $row['user_from'] ?>
<input type="submit" name="acc" value="Accept">
<input type="submit" name="cancel" value="Reject Rrquest">
</form>
<php
}
?>
<?php
if(isset($_POST['acc']) && !empty($_POST['yo']))
{
$link = $_POST['yo'];
// do what you want to do with this `url`
}
?>
NOTE: Don't Complex Your Code With Using Html COde In Php echo. You Can Just Open the While Loop Brackets { and then close the php ?> then simple html you have to written, So Just Avoid to used html code inside the php echo

Send an array of variable instead of a single variable, over html button

I would like to send 2 variables on a click of a button
My Code so far is that:
HTML:
<?php
while ($Case=mysql_fetch_assoc($records)) {
echo "<tr>";
echo "<td>".$Case['SubmissionID']."</td>";
echo "<td>".$Case['AppID']."</td>";
echo "<td>".getFullNameApp($Case['AppID'])."</td>";
echo "<td>".getCourseName($Case['SubmissionCourseID'])."</td>";
echo "<td>".getDepartmentName($Case['SubmissionCourseID'])."</td>";
echo "<td>".DateFormat($Case['Date'])."</td>";
echo "<td>".$Case['SubmissionStatus']."</td>";
if ($Case['SubmissionStatus'] == 'Draft') {
echo "<form action='ApplicantApplyDetails.php' method='POST'>";
echo "<td><button type='submit' class='btn btn-success btn-xs' name='modifybtn' value='".array($Case['SubmissionID'], $Case['SubmissionCourseID'])."'>Modify</button></td>";
}
else {
echo "<td><button type='submit' class='btn btn-danger btn-xs disabled' disabled='disabled' name='modifybtndis' value=''>Modify</button></td>";
}
echo "</form>";
echo "</tr>";
}
mysql_close();
?>
PHP:
if (isset($_POST['modifybtn'])) {
$SubmissionID =$_POST['modifybtn'][0];
$CourseID = $_POST['modifybtn'][1];
echo $CourseID;
}
And then get those variables for further processing. Is that possible? I've done single ones but no arrays. My code is not working. The problem I think is in the HTML part, but I have no idea how to fix it. Any hints?
To answer your question; You can convert an array to a string:
$value = json_encode([$Case['SubmissionID'], $Case['SubmissionCourseID']]);
echo "<td><button type='submit' class='btn btn-success btn-xs' name='modifybtn' value='".$value."'>Modify</button></td>";
And covert the string back to the array when reading.
var_dump(json_decode($_POST['modifybtn'], true));
However there is also the option to submit a hidden field:
<input type="hidden" name="courseid" value="<?= $Case['SubmissionCourseID']>">
And just use:
var_dump($_POST);

another simple PHP page

I'm trying to create a simple PHP page that will Display a number starting with 0. It will then ask for a number input, then add the inputted number to the beginning number. and keep adding the number that is input in the text box. Here's what I have so far... I can get the first variable initialized and get the input from a form box. I just can't seem to add them together and keep a running total. Thanks in advance.
<?php
$_POST['number1'];
echo "Current number is ".$_POST['number1'];
echo "<br>";
echo "Enter your next number.<br>";
echo "<form action='' method='POST'>";
echo "<input type='number' name='number2'>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "<br>";
echo "Your entered number is ".$_POST['number2']."<br>";
$sumtotal = $_POST['number1'] + $_POST['$number2'];
echo "Your new total is ".$sumtotal;
$_POST['number1'] == $_POST['number2'];
?>
Using input type HIDDEN:
<?php
if(isset($_POST['submit'])){
$total=$_POST['number1']+$_POST['number2'];
echo "Your entered number is ".$_POST['number2']."<br>";
}
else {
$total=0;
}
echo "Current total number is ".$total;
echo "<br>";
echo "Enter your number.<br>";
echo "<form action='' method='POST'>";
echo "<input type='hidden' name='number1' value='$total'>";
echo "<input type='number' name='number2'>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "</form><br>";
echo "Your new total is ".$total;
?>
Using SESSION:
<?php
session_start();
if(empty($_SESSION["total"])){
$_SESSION["total"]=0;
}
if(isset($_POST['submit'])){
$total=$_SESSION["total"];
$total=$total+$_POST['number2'];
$_SESSION["total"]=$total;
echo "Your entered number is ".$_POST['number2']."<br>";
}
echo "Current total number is ".$_SESSION['total'];
echo "<br>";
echo "Enter your number.<br>";
echo "<form action='' method='POST'>";
echo "<input type='number' name='number2'>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "</form><br>";
echo "Your new total is ".$_SESSION['total'];
?>
Both works and have the same output, but with different approach from one another.
You could add another hidden input field that keeps the last number that was posted by you.
<input style="display: none" name="tmp_number" value="<?php echo $tmp_number; ?>">
Try (edited for all details and errors)
<?php
$input = $sum = 0;
if (isset($_POST)) {
$input = $_POST['number1'];
$prev_sum = $_POST['prev'];
$sum = $input + $prev_sum;
}
echo "Current number is ".$input;
echo "<br>";
echo "Enter your next number.<br>";
echo "<form action='' method='POST'>";
echo "<input type='number' name='number1'>";
echo "<input type='hidden' name='prev' value='$sum' >";
echo "<input type='submit' name='submit' value='Submit'>";
echo "<br>";
echo "Your entered number is ".$_POST['number1']."<br>";
echo "Your new total is ".$sum;
?>

javascript cannot redirect in php

I have created a form below and some javascript code in php, the it doesnt redirect to other pages.
<form method="post" name="for">
<?php
echo "<input type='text' name='text1'><input type='submit' name='submit' value='go' onclick='fon1();'>";
?>
</form>
<?php
echo "<script type='text/javascript'>";
echo "function fon1(){";
echo "var k = confirm('Confirm Delete');";
echo "if(k == true){";
echo "window.location = 'http://www.google.com'; }";
echo "else{ window.location = 'http://www.yahoo.com';} ";
echo "}</script>";
?>
Put a "return false;" after calling fon1();
echo "<input type='text' name='text1'><input type='submit' name='submit' value='go' onclick='fon1(); return false;'>";
Use
window.location.href
instead of
window.location
I think you change the type of your form buttom from "submit" to "button" :
echo "<input type='text' name='text1'><input type='button' name='submit' value='go' onclick='fon1();'>";
try this!
you can use window.location.replace to simulate a redirect (back button won't work)
or window.location.href so simulate a click
Do this:
<form method="post" name="for">
<?php
echo "<input type='text' name='text1'><input type='button' name='submit' value='go' onclick='return fon1();'>";
?>
</form>
<?php
echo "<script type='text/javascript'>";
echo "function fon1(){";
echo "var k = confirm('Confirm Delete');";
echo "if(k == true){";
echo "location.href = 'http://www.google.com'; }";
echo "else{ window.location = 'http://www.yahoo.com';} ";
echo "}";
?>
<form method="post" name="for">
<?php
echo "<input type='text' name='text1'><input type='button' name='submit' value='go' onclick='fon1();'>";
?>
</form>
<script type='text/javascript'>
function fon1(){
var k = confirm('Confirm Delete');
if(k == true){
document.location = 'http://www.google.com';
}
else
{
document.location = 'http://www.yahoo.com';
}
}</script>
Just use this..
<form method="post" name="for">
<?php
echo "<input type='text' name='text1'><input type='button' name='submit' value='go' onclick='fon1();'>";
?>
</form>
<script type='text/javascript'>
function fon1(){
if(confirm('Confirm Delete')) {;
document.location.href = 'http://www.google.com';
}
else {
document.location.href = 'http://www.yahoo.com';
}
}
</script>

Categories