Im having trouble getting my form code to work, i want it to when they submit it to change the current page url, ex: weaw.com?id=Summitedtext and i am echoing it out from php.
$cmd = 'htmlspecialchars($_SERVER["PHP_SELF"])'
echo '<form id="idform" action="' . $cmd . '" method="GET">';
echo '<label><center>To search an inventory please put the users Profile URL in the input box below.</center></label>';
echo '<div class="input-group">';
echo '<span class="input-group-addon" id="basic-addon1"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></span>';
echo '<input type="text" class="form-control" placeholder="Profile URL" aria-describedby="basic-addon1">';
echo '<span class="input-group-btn">';
echo '<button class="btn btn-default" type="submit" name="submit" value="Submit">Go!</button>';
echo '</span>';
echo '</form>';
echo '</div>';
Totally disagree with echoing form but change your code to this and add your ingredients such class and so on:
<?PHP
if(isset($_GET['url'])
$cmd = htmlspecialchars($_GET['url']);
else
$cmd = htmlspecialchars($_SERVER["PHP_SELF"]);
?>
<form id="idform" action="<?PHP echo $cmd; ?>" method="GET">
<input type="text" name="url">
<input class="btn btn-default" type="submit" name="submit" value="Go!" />
</form>
Related
echo '<form action="query.php" method="POST">';
echo '<input style="display: none;" type="text" name="name" value="'.$name.'"> ';
echo '<input style="display: none;" type="text" name="password" value="'.$password.'"> ';
echo '<input style="display: none;" type="text" name="resume" value="'.$Resume->path.'"> ';
echo '<input style="display: none;" type="text" name="mobile" value="'.$mobile.'"> ';
echo '<input style="display: none;" type="text" name="profilePicture" value="'.$profilePicture->path.'"> ';
echo '<input style="display: none;" type="text" name="identification" value="'.$identification->path.'"> ';
echo '<input style="display: none;" type="text" name="whitecard" value="'.$whitecard->path.'"> ';
echo '<input style="display: none;" type="text" name="email" value="'.$email.'"><br> ';
echo '<button type="submit" style="">Confirm Register</button>';
echo '<input type="button" name="Cancel" id="Cancel" value="Cancel Registration">';
echo '</form>';
if(isset($_POST['Cancel'])) {
$path = $_SERVER['DOCUMENT_ROOT'].'Files/'.$_SESSION['Identification']; unlink($path);
$path = $_SERVER['DOCUMENT_ROOT'].'Files/'.$_SESSION['Resume']; unlink($path);
$path = $_SERVER['DOCUMENT_ROOT'].'Files/'.$_SESSION['Whitecard']; unlink($path);
$path = $_SERVER['DOCUMENT_ROOT'].'Files/'.$_SESSION['Profile']; unlink($path);
session_unset();
session_destroy();
header("Location: http://index.php");
}
I am am trying to get a pre-confirm script running just before my users are inserted into the database so i am trying to generate a button just before they confirm. The confirm button is working however the Cancel appears but is not doing the task it is supposed to do is there a reason why this is not working?.
An <input type="button" ..> does not submit the form. You would use this type to cause the button to run some javascript.
To submit the form change it to type="submit"
the cancel-button doesn't submit the form and if you want to check what's in $_POST you need to have a submitted form.
You could do that manually with Javascript (https://www.w3schools.com/jsref/met_form_submit.asp).
Another problem: header() cannot be called after you have printed out something, this will throw you an error.
You cannot do
<html>
<body>
<?php
if (isset($_POST['Cancel'])) {
header('Location: index.php');
}
</body>
</html>
You'd have to do
<?php
if (isset($_POST['Cancel'])) {
header('Location: index.php');
}
?>
<html>
<body>
</body>
</html>
I am quite new to PHP so I need some help with my project.
I have a form on the page where the user writes its content and name, like this:
<div class="white shadow padding-10 margin-bottom">
<form class="form xl relative" method="POST" action="
<?php echo $_SERVER['PHP_SELF']; ?>
">
<div class="col-1 col-persist -margin">
<img class="pull-left width-100 round" src="images/logo.gif" alt = "logo"/>
</div>
<div class="col-9 col-persist gutter-h-10 padding-top-5 -margin">
<textarea placeholder="What is new?" name="content"></textarea>
<input type="text" name="name" placeholder="What is your name?">
</div>
<div class="col-2 col-persist -margin">
<input type="button" name="submit" value="Send">
</div>
</form>
</div>
PHP_SELF I use to stay on the same page
In php part I have this:
$userName = "";
$userInput = "";
if($_SERVER["REQUEST_METHOD"] === "POST"){
$userName = $_POST['name'];
$userInput = $_POST["content"];
}
if(isset($_POST)){
echo '<div class="white shadow padding-10 margin-bottom">';
echo '<form class="form xl relative" method="POST">';
echo '<div class="col-1 col-persist -margin">';
echo '<img class="pull-left width-100 round" src="images/logo.gif" alt = "logo"/>';
echo '</div>';
echo '<div class="col-9 col-persist gutter-h-10 padding-top-5 -margin">';
echo '<textarea placeholder="What's new?" name="content">'.$userInput.'</textarea>';
echo '</div>';
echo '<input type="text" name="name" placeholder="What's your name?">'.$userName;
echo '</div>';
echo '</form>';
echo '</div>';
}
However, this code does not work at all. When debugging, the browser shows some problem in these lines:
echo '<textarea placeholder="What's new?" name="content">'.$userInput.'</textarea>';
echo '</div>';
echo '<input type="text" name="name" placeholder="What's your name?">'.$userName;
What is the problem? I guess it is something in syntax, but maybe more in code logic itself?
Thanks in advance!!!
First your form needs a submit button, so change <input type="button" name="submit" value="Send"> to <input type="submit" name="submit" value="Send">
Also, you don't need to use PHP_SELF, simply leaving the action attribute as action="" will keep it on the same page.
As for the error in the php, you have single quote marks that are not escaped. This should work:
echo '<textarea placeholder="What\'s new?" name="content">'.$userInput.'</textarea>';
echo '</div>';
echo '<input type="text" name="name" placeholder="What\'s your name?">'.$userName;
You need to change <input type='submit' name='submit' value='send'> in your html and not button.
Here are the codes.
Form
<form method="POST" action="" id="search">
<div class="col-lg-4 center"><a class="btn btn-success" type="submit" name="publish" id="publish" alt="Publish"> Publish</a></div>
<div class="col-lg-4 center"><a class="btn btn-success" type="submit" name="edit" id="edit" alt="Edit"> Edit </a></div>
<div class="col-lg-4 center"><a class="btn btn-success" type="submit" name="draft" id="draft" alt="Save as Draft"> Save as Draft</a></div>
</form>
Post
<?php
if ($_POST['publish'] == 'publish'){
echo '<script>alert("Publish"); </script>';
}
if ($_POST['edit'] == 'publish'){
echo '<script>alert("edit"); </script>';
}
if ($_POST['draft'] == 'publish'){
echo 'draft';
}
?>
If I click on any button nothing happens. I am not getting anything nor any error. Nothing appears in firebug as well.
You wanted to submit to the same page. Try giving the page name in your action
<form method="POST" action="yourpage.php" id="search">
Try with this:
<?php
if (isset($_POST['publish'])) {
echo '<script>alert("Publish"); </script>';
}
if (isset($_POST['edit'])) {
echo '<script>alert("edit"); </script>';
}
if (isset($_POST['draft'])) {
echo 'draft';
}
?>
use button instead of links
like
<input type="submit" ..... />
like this
<input type="submit" class="btn btn-success" name="publish" id="publish" alt="Publish" value="Publish" />
You will get data then
and try to on error reporting in php.ini file
You need to add a submit n
Button. <input type=submit name="submit"> also, specify action attribute in form tag to your Php script.
<?php
if (isset($_POST['submit'])){
echo '<script>alert("Publish"); </script>';
}
?>
<form method="POST" action="" id="search">
<input name="submit" type="submit" />
</form>
First thing you can not use type="submit" as attribute in your anchor tag.
Second if you wants to submit button using input type as submit then use the following code:
<?php
if(isset($_POST))
{
if (isset($_POST['publish'])){
echo '<script>alert("Publish"); </script>';
}
if (isset($_POST['edit'])){
echo '<script>alert("edit"); </script>';
}
if (isset($_POST['draft'])){
echo '<script>alert("Draft"); </script>';
}
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="search">
<div class="col-lg-4 center"><input class="btn btn-success" type="submit" name="publish" id="publish" value="Publish"> </input></div>
<div class="col-lg-4 center"><input class="btn btn-success" type="submit" name="edit" id="edit" value="Edit"> </input></div>
<div class="col-lg-4 center"><input class="btn btn-success" type="submit" name="draft" id="draft" value="Draft"> </input></div>
</form>
Third, In case if you really wants to use only anchor for submit then you have to use help of javascript function submit() and use the
onclick="document.getElementById('search').submit();
in your anchor tag.
I have two submission buttons on my webpage which are assigned to do different things. My problem is that clicking 1 of the buttons is invoking the actions of both buttons. How can I make it so that clicking 1 button only invokes the associated form submission action?
(This is my first day using php).
First form:
<form id="frm1" onsubmit="document.getElementById('frm1').submit();">
<center><br>
<input style="width:150px" name="data" type="text" autocomplete="off" placeholder="Add Time (hh:mm:ss)">
</input>
<input name="id" type="hidden" value="<?php echo $ID; ?>">
<input name="addTime" type="hidden" value="true">
<button style="width:40px" type="submit" class='btn btn-success pull-right'>
<i class="icon-plus"></i>
</button
</form>
Second form:
<form id="frm2" onsubmit="document.getElementById('frm2').submit();">
<center>
Edit Group:
<?php
#
$styles = sqlToQuery("SELECT groupID, Name from sha_scheduler_dev.sha_scheduler_map_job_group");
echo '<select style="width:100" name="groupNum">';
for($i = 0; $i < count($styles); $i ++){
echo ' <option style="width:100" value="'.$styles[$i][0].'">'.$styles[$i][1].'</option>';
}
echo '</select><br>';
?>
<input name="editGroup" type="hidden" value="true"><br>
<button type="submit" class='btn btn-info pull-right'>
Submit Group
</button>
</form>
Checking if buttons are submitted:
if ($_GET['addTime']==='true'){
sqlToQuery("replace into sha_scheduler_dev.sha_scheduler_map_jobtimes (job_ID, time_to_run) values (".$ID." ,'".$_GET['data']."')");
echo '<script>window.location.assign("'.$redir.'?id='.$ID.'");</script>';
}
if ($_GET['editGroup']==='true'){
sqlToQuery("UPDATE sha_scheduler_dev.sha_scheduler_jobs a SET a.group = '".$_GET['groupNum']."' WHERE a.job_ID = ".$ID." ");
echo '<script>window.location.assign("'.$redir.'?id='.$ID.'");</script>';
}
In your first form, button tag is not closed properly, that's why first form is tag is not closed properly,use this,
<form id="frm1" onsubmit="document.getElementById('frm1').submit();">
<center><br>
<input style="width:150px" name="data" type="text" autocomplete="off" placeholder="Add Time (hh:mm:ss)">
<input name="id" type="hidden" value="<?php echo $ID; ?>">
<input name="addTime" type="hidden" value="true">
<button style="width:40px" type="submit" class='btn btn-success pull-right'>
<i class="icon-plus"></i>
</button>
</center>
</form>
Also change your php code like this
if ( isset($_GET['addTime']) && $_GET['addTime']==='true'){
sqlToQuery("replace into sha_scheduler_dev.sha_scheduler_map_jobtimes (job_ID, time_to_run) values (".$ID." ,'".$_GET['data']."')");
echo '<script>window.location.assign("'.$redir.'?id='.$ID.'");</script>';
}
if ( isset($_GET['editGroup']) && $_GET['editGroup']==='true'){
sqlToQuery("UPDATE sha_scheduler_dev.sha_scheduler_jobs a SET a.group = '".$_GET['groupNum']."' WHERE a.job_ID = ".$ID." ");
echo '<script>window.location.assign("'.$redir.'?id='.$ID.'");</script>';
}
Give you're submit buttons a value and a name catch the different values using $_GET(in your case) or $_POST
Update: (just to give it a try maybe)
First Form
<form id="frm1" action="" method="post">
<center><br>
<input style="width:150px" name="data" type="text" autocomplete="off" placeholder="Add Time (hh:mm:ss)" />
<input name="id" type="hidden" value="<?php echo $ID; ?>" />
<input style="width:50px" type="submit" class='btn btn-success pull-right' name="submit_form1" value="submit" />
<i class="icon-plus"></i>
</button>
</center>
</form>
Second Form
<form id="frm2" name="frm2" action="" method="post">
<center>
Edit Group:
<?php
$styles = sqlToQuery("SELECT groupID, Name from sha_scheduler_dev.sha_scheduler_map_job_group");
echo '<select style="width:100" name="groupNum">';
for ($i = 0; $i < count($styles); $i ++)
{
echo ' <option style="width:100" value="' . $styles[$i][0] . '">' . $styles[$i][1] . '</option>';
}
echo '</select><br>';
?>
<input type="submit" class='btn btn-info pull-right' name="submit_form2" value="Submit Group" />
</form>
PHP
<?php
if (filter_input(INPUT_POST, "submit_form1"))
{
// {Check for empty var on data}
sqlToQuery("replace into sha_scheduler_dev.sha_scheduler_map_jobtimes (job_ID, time_to_run) values (" . $ID . " ,'" . $_GET['data'] . "')");
echo '<script>window.location.assign("' . $redir . '?id=' . $ID . '");</script>';
}
if (filter_input(INPUT_POST, "submit_form2"))
{
sqlToQuery("UPDATE sha_scheduler_dev.sha_scheduler_jobs a SET a.group = '" . $_GET['groupNum'] . "' WHERE a.job_ID = " . $ID . " ");
echo '<script>window.location.assign("' . $redir . '?id=' . $ID . '");</script>';
}
?>
This is pretty tight and keeps your url clean.
I have this code that fetches information in a file and shows the result. That all works as expected. What I'm not able to do is to export the button value in the function when it is pressed so the value of the pressed button will transfer to a variable in my php function. How can I do that?
Here the code:
<h2>Demmarage script torrent</h2>
<form action="search.php" method="POST">
<input type="text" name="input_value">
<input type="submit" name="submit">
<?php
echo "<br>";
if (isset($_POST['submit'])){
$findme = $_POST['input_value'];
$findme1 = str_replace (" ", ".", $findme);
$savedarr = unserialize(file_get_contents('torrent.bin'));
foreach ($savedarr as $val1){
$mystring = $val1['title'];
if((stripos($mystring, $findme) !== false) or (stripos($mystring, $findme1) !== false)) {
echo "Show trouve: ";
echo $mystring;
?>
<button type="submit" value="<?php echo $val1['link']; ?>" name="editId">Telecharger</button>
<?php
echo "<br>";
}
}
}
if (isset($_POST['editId'])){
//Here i want to import the value of the pressed button to do something
echo "download start";
}
?>
you should use a hidden field, like
<input type="hidden" name="link" id="link" value="<?php echo $val1['link'] ?>" />
After the submit you will be able to get the value with
$_POST['link']
Also, this should be in a form,otherwise the submit will post EVERY field ...
like
foreach(...)
{?>
<form action="" method="POST">
<input type="hidden" name="link" id="link" value="<?php echo $val1['link'] ?>" />
<input type="submit" />
</form>
<?php}
The problem is that you are setting the value twice by <button>value1</button> and by <button value="value2">... remember that the value of a submit button is always the text that shows up inside the button.
You should do replace this:
<button type="submit" value="<?php echo $val1['link']; ?>" name="editId">Telecharger</button>
with this:
<form method="POST">
<input type="hidden" value="<?=$val1['link']?>" name="editId" />
<input type="submit" value="Telecharger" />
</form>