Trouble getting echo syntax correct for html code - php

I am returning text results from a PHP FOREACH loop, and I want to add the links add and delete for each result where it looks like this: ADD | DELETE. The trick is, I need each link to be a form that is styled as a link so it looks like text links but I can use Post when the links are clicked. I can style the links with the class name "formlink", but my question is, how will the statement syntax look? I keep getting T_variable errors. Can someone help show how the code below so it will display error-free on the page?
<?php
echo "<hr>" . $results[text]
<form action="" method="post"><button type="submit" class="formlink">Approve</button>
</form> | <form action="" method="post"><button type="submit"
class="formlink">Delete</button></form>

That's one messy code though. Well I dunno, I could give you a brief idea, and the rest is up to you.
<?php
// Check clicked button
if (isset($_POST['btn_approve'])) {
$username = $_POST['username'];
}
if (isset($_POST['btn_disapprove'])) {
// Do something
}
?>
<form action="" method="post">
<input type="text" name="username" value="<?php echo (isset($username) ? $username : ''; ?>">
<input type="submit" name="btn_approve" class="form-link" value="Approve">
<input type="submit" name="btn_disapprove" class="form-link" value="Disapprove">
</form>
You could check isset() and How to write ternary operator.

<?php
echo "<hr>" . $results[text];
?>
<form action="" method="post">
<button type="submit" class="formlink">Approve</button>
</form> |
<form action="" method="post">
<button type="submit" class="formlink">Delete</button>
</form>

Related

Can A Form Method Be Post And Get?

I am learning PHP and have a situation where I want the method to be POST to pass variables to my php and
1) Connect to Server2) store results in a variable3) Display an HTML Table
However, I later on in my syntax want to use GET to "recall" that variable and output the results to a CSV file. This is what I have, but when I click the button Generate nothing happens. Is this possible? If not, how should I re-write the syntax to make ithappen?
<body>
<form method="POST">
End Date:<input type="date" name="end" value="<?= $_POST['end'] ?>">
<input type="submit" name="submit" value="Go">
</form>
</body>
<?php
if (isset($_POST['submit']))
{
//Connect To Server
//Store Result Set in Variable
//Display HTML Table
}
?>
<form method="get">
<button type="submit" name="csv" value="1">Generate CSV</button>
</form>

Using POST instead of GET

Maybe I don't understand English as well as I thought. I need some help. I read the other posts but I am still not able to send variables to another page using POST instead of GET. Please help me with this example:
I have two pages. The first one has a query (Users). Then I make a table with the results and using the following code I can send user id to another page where I can edit some information.
First page:
<td><div align="center">
<a href="Users_modify.php?id=<?php echo $row_Users['id'];?>">
<img src="Icons/info-icon.png" width="20" height="20"></a></div></td>
Second page:
$colname_recordset1 = "-1";
if (isset($_GET['id'])) {
$colname_recordset1 = $_GET['id'];}
and after this I can use the variable to make a query.
With respect I ask you for a sample of the first page statement in order to be able to use POST on the second page. Thank you for your time!
You can only use $_POST on a form.
If it is associated in a link, you can use $_GET or you can use $_REQUEST to get the query string.
You'll fill $_POST by using a form:
<form action="…" method="post">
<input type="hidden" value="$id">
…
<input type="submit" value="Save">
</form>
posting it by form
<form action="second page url" method="post">
<input type="hidden" name="id" value="$id">
…
<input type="submit" value="Post">
</form>
First Page
<form method="post" action="page2.php">
<input type="hidden" name="idField" value="<?php echo $id; ?>" />
<input type="submit"/>
</form>
Second Page
<p>
<?php
echo $_POST["idField"];
?>
</p>

How to call php variable input text from another Php page

My question is how do I call input value of user selected which has show by php code from another php.
Normal simple way we get the input this way
$calendar_id_val = $_GET['calendar_id'];
and now it is not working:
For example Show.php, I have one form which show the values from Database and show the result with php variable with While Loop.
<input name="calendar_id" value="<?php echo $calendar_id;?>">
and when user is submit that form I will carry these user selected value and perform insert.php
While you are doing echo in the input use echo $calendar_id_val
You should use form like,
<form action="insert.php" method="get">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>"/>
<input type="submit" name="submit_id" value="Insert"/>
</form>
Insert.php
echo $calendar_id_val = $_GET['calendar_id'];
Does this answer your question?
<form action="insert.php" method="GET">
<input name="calendar_id" value="<?php echo $calendar_id;?>">
</form>
If you want to redirect the user back to show.php, add this to the end of your insert.php script
header('Location: show.php');
exit();
I suggest $_POST var like this:
<form action="insert.php" method="POST">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>" />
<input type="submit" name="submit" value="Submit" />
</form>
insert.php file:
echo $calendar_id = $_POST['calendar_id'];

Getting a php file in an iframe

I have two files one is index.php which contains a form which gets submitted to abc.php.Now the problem is have a frame in index.php where in i want to display the datas that i got after submitting the form to abc.php.I tried but m not able to it.Hope i hav mentioned all the thngs required. Any suggestion will be appreciated.
Set your form's target (phpfiddle demo):
<?php
if(!empty($_POST['submit_to_frame']))
{
echo htmlspecialchars($_POST['the_data']);
}
else
{
?>
<iframe id="the_frame" name="the_frame"></iframe>
<form method="POST" target="the_frame">
<input type="hidden" name="submit_to_frame" value="true" />
<input type="text" name="the_data"/>
<input type="submit" value="OK">
</form>
<?php
}
?>

2 forms with one PHP file

I have 2 FORMS on a single page, One below the other.
I would like to have such that second form should be always in disable mode.
and Once the first form submit button is pressed and validated second should get activated to enter the data in it.
Is there anything in PHP which can help me on this
You have 2 ways:
1) send validation of first form using ajax, and, if you receive 'true', enable second form.
2) make a POST from first form, if everything is good, set "validated" to 'true' and reload the same page. In the second form "enabling" must be only if you have $validated = true;
The logic below should help you out as a starting point:
<form method="post">
<input type="text" name="name" />
<input type="submit" name="form1" value="Proceed" />
</form>
<form method="post">
<input type="text" name="email"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
<input type="submit" name="form2" value="Submit"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
</form>
Of course, it would be much more reliable to use either AJAX to validate the first form, or to have the forms appear on separate pages.
<?php
if(isset($_POST['next'])) {
if($_POST['name']!="") {
$disabled = "";
$val = $_POST['name'];
} else {
$disabled = " disabled='disabled'";
$val="";
}
} else {
$disabled = " disabled='disabled'";
$val="";
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form id="frm1" name="frm1" method="POST" action="">
<label>Name</label><input type="text" id="name" name="name" value="<?php echo $val;?>"/>
<input type="submit" name="next" id="next_frm" value="Next"/>
</form>
<form name="frm2" id="frm2" method="POST" action="">
<label>Address</label><input type="text" name="address" id="address" value="" <?php echo $disabled;?>/>
<input type="submit" name="save" id="save" value="Save" <?php echo $disabled;?>/>
</form>
</body>
</html>
This is somewhat you were looking for ,I hope
You can do it by setting a class on all inputs within second form and set them as disabled of course someone who knows a bit of javascript will be able to change it.
So you can do it as your visual layer, but then check in PHP as well if second form can be passed in case someone wanted to sneak something in.
More complicated approach would be to show images that look like form fields and only change them to inputs where the first form is submitted. That can be done on client or server side
So in reality you will have 3 forms, but one would be "fake"
Thats simple just use if else condition.
// this if condition checks whether the form 1 is submitted or not. If form1 is submitted than form 2 is displayed else form1 wil only be displayed
if(isset($_POST['submit']))
{
//Display your form 2.
}
else
{
//Display your form1.
}

Categories