included php code is not executing on submit - php

I'm including a a php code with angular ui routing
the problem is if the millitary.php :
`
<div class="jumbotron">
<div class="page-header">
<h1>Millitary Loot Table</h1>
</div>
<?php
$MillitaryLoot = array(
'M4A4' => 47,
'AWP' => 2,
'Karambit' => 1,
'Famas' => 50
);
$newMillitaryLoot = array();
foreach ($MillitaryLoot as $item=>$value)
{
$newMillitaryLoot = array_merge($newMillitaryLoot, array_fill(0, $value, $item));
}
$myLoot = $newMillitaryLoot[array_rand($newMillitaryLoot)];
if (isset($_POST['submit']))
{
echo "\n" . "<h2 style='display:inline;'>Item: </h2>" . '<p class="text-center text-danger">' . $myLoot . '</p>' . "</br>";
}
?>
<form name="Gamble" action="" method="post" target="_self">
<button name="submit" type="submit" class="btn btn-primary">Gamble</button>
</form>
</br>
</div>
`
the include app.js
$stateProvider
.state('millitary', {
url: '/millitary',
templateUrl: 'inc/millitary.php'
})
is included into the index.php the submit button basicly not executes the code above the form
so basically i want the php code to execute in the index.php to keep the ui instead of jumping into the executing php file and losing the index.php linked styles and navigation
edit : if i run the code normaly it works but it doesn't if it gets included
edit2 : everything outside the if statement works basically if i add a echo "test"; it outputs but inside the if it does not so the problem is the form and the connection to the if statement
kind regards, daniel

Change your HTML to the following:
<form id="form" name="Gamble" method="post">
<button id="form" name="submit" type="submit" class="btn btn-primary">Gamble</button>
</form>
You can submit your form with a button with HTML5 but it must share the same ID of the form. Also keep in mind this will only be supported with browsers that support HTML5 so if not they won't be able to submit the form.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button
The other alternative is to use...
<input name="submit" type="submit" class="btn btn-primary" value="Gamble">
as #gbestard suggested.

Change
<button name="submit" type="submit" class="btn btn-primary">Gamble</button>
to
<input name="submit" type="submit" class="btn btn-primary" value="Gamble" />
In order to submit you need a type submit input or use some client side script as javascript

Related

delete button in PHP website behaving like static when clicked

i have a simple delete button in PHP which uses simple SQL query to delete the data from database, I have used the following code:
$myid = $this->uri->segment('3');
if (isset($_POST['submit'])){
$ret = mysqli_query($con,"delete * from smart_category where name='$myid'");
header("Location: https://cloudclassmate.com/jewelry-catalogue/admin/viewcategory");
}
<div style="margin-left:38%" class="clearfix">
<a href="https://cloudclassmate.com/jewelry-catalogue/admin/viewcategory">
<button type="button" class="cancelbtn bemine">Cancel</button></a>
<form action="" method="post" ><button type="submit" name="submit" class="deletebtn bemine">Delete</button></form>
</div>
the problem here is if I click the delete button, its not responding, nothing is happening. the button is like static. can anyone please tell me what could be wrong here
If any Javasscript is not involved, than just put form action.
Also send myid in the request or in URI.
please see below:
<div style="margin-left:38%" class="clearfix">
<button type="button" class="cancelbtn bemine">Cancel</button>
<form action="abc.php" method="post" ><button type="submit" name="submit" class="deletebtn bemine">Delete</button></form>
</div>
You should add an action url/path to where your PHP file is located and add a Method to your form. Also, I noticed you deleting an entry where "name" is equal to $myid please check if that is correct.
<div style="margin-left:38%" class="clearfix">
<button type="button" class="cancelbtn bemine">Cancel</button>
<form action="url-to-server.php" method="post" >
<button type="submit" name="submit" class="deletebtn bemine">Delete</button>
</form>
</div>
$myid = $this->uri->segment('3');
if (isset($_POST['submit'])){
$ret = mysqli_query($con,"delete * from smart_category where name='$myid'");
header("Location: https://cloudclassmate.com/jewelry-catalogue/admin/viewcategory");
}

How to keep values in re-opened form?

I have two PHP files: index.php with a form, and data.php for further data manipulation.
Here is index.php:
<?php
session_start();
require_once("../index.conf");
$language = new Language();
$lang = $language->getLanguage(#$_POST['lang']);
?>
...
<form name="myForm" action="data.php" method="post" onsubmit="return validateForm()">
<input type="text" name="title" placeholder="e.g.: my_title" value="<?php echo isset($_POST['title']) ? $_POST['title'] : '' ?>">
...
<button class="btn_r" name="submit" type="submit">
<?php echo $lang['submit-button']; ?>
</button>
Here is data.php:
// success message
echo sprintf('
<div class="success">Good job! Your file <em>'.$file.'</em> was successfully created with this HTML content:<br>
<form name="goto_preview" method="post">
<input type="hidden" name="img_title" value="'.$title.'">
<button class="btn_l" name="reset" type="submit" name="logout" formaction="preview.php">PREVIEW RESULTS</button>
<button class="btn_r" name="submit" type="submit" name="continue" formaction="index.php">CORRECT DATA</button>
</form>
</div>',$img_name);
I try to return the user to the form, with the original values filled in if correction is needed. But the form always opens empty. What is wrong with my code?
Nothing is wrong with your code. That's just the way php forms work, you're redirecting to a new page therefore the form isn't filled out. To change that you could pass the POST arguments that you receive in the data.php and pass them back to index.php where you set them as default values (if present)

Post 2 different form actions with 2 submit buttons

I have a table where I use an UPDATE button with action form to update the data. But I also need to submit the table using another form action and submit button.
Here's my button:
<button class="btn btn-info" type="submit" name="update" value="update">Update Table</button>
<button class="btn btn-success" type="submit" name="submit_req" value="submit_req">Submit Request</button>
This is currently my form action for the table:
<form method="post" action="">
And this is my PHP if else statement. The first submit_req is supposed to POST to another URL. But I don't know how to do it. I already tried header, but it won't work.
<?php
if(isset($_POST['update'])){
if(!empty($_SESSION['cart'])){
foreach($_POST['quantity'] as $key => $val){
if($val==0){
unset($_SESSION['cart'][$key]);
}else{
$_SESSION['cart'][$key]['quantity']=$val;
}
}
}
}elseif (isset($_POST['submit_req'])) {
//form action: insert_order.php
}
?>
You can use a JS function to redirect to another page for the submit button
Here page.php is where you want to send your form to. formID is the id of the form.
<button class="btn btn-success" type="submit" name="submit_req" value="submit_req" onclick="submitForm('page.php')">Submit Request</button>
<script type="text/javascript">
function submitForm(action)
{
document.getElementById('formID').action = action;
document.getElementById('formID').submit();
}
</script>

How to remove button after submit form using PHP

I need one help.I need to totally remove the submit button after submit the form and when it will be submitted the button will display to user.I am explaining my code below.
<form name="billdata" id="billdata" enctype="multipart/form-data" method="POST" onSubmit="javascript:return checkForm();" action="complain.php">
<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px"> Name :</span>
<input type="text" name="u_name" id="name" class="form-control" placeholder="Add Name" onKeyPress="clearField('name');">
</div>
<input type="submit" class="btn btn-success" name="complainSubmit" id="addProfileData" value="Submit"/>
</form>
complain.php:
require_once("./include/dbconfig.php");
if(isset($_REQUEST['complainSubmit']))
{
// ......data is collecting here....
}
when data are submitted successfully the below part is executing.
<script type="text/javascript">
var phpVar = "<?php echo $_GET['success'];?>";
//console.log('php',phpVar=='');
if(phpVar == 1 && phpVar!=''){
alert('Submitted successfully.');
//var subButton=document.getElementById('addProfileData');
//subButton.disabled=false;
}
else if(phpVar == 0 && phpVar!=''){
alert('Unable to add.\\nTry again.');
}
else{
// nothing
}
</script>
<script>
function checkForm(){
var s=document.billdata;
if(s.u_name.value==''){
alert('Please enter name');
s.u_name.focus();
s.u_name.style.borderColor = "red";
return false;
}
}
</script>
Here i need to button hide when the data is going to submit and it will again display after the submit.Please help me.
A javascript code such as:
document.getElementById("your_div").innerHTML = "";
will erase the content of your div.
So, byt tagging the entire form, then erasing its contents, you can "hide" it.
Why you dont add a hide CSS-Selector or remove it by checking via if-statement?
CSS-Version:
<input type="submit" class="btn btn-success <?php ($isSubmitted ? ' hideme' : '')?>" name="complainSubmit" id="addProfileData" value="Submit">
Except a new CSS-Selector: .hideme { display:none }
Via PHP/Template:
html...
<?php if(!$isSubmitted) : ?>
<input type="submit" class="btn btn-success" name="complainSubmit" id="addProfileData" value="Submit">
<?php endif; ?>
html...
Dont forget: You dont need close input html-tag: <input/> just <input>
And what is wrong to hide the button via JS?
document.getElementById("addProfileData").style.display = "none";
UPDATE:
Prompt hiding after clicking:
<button onclick="javascript:this.style.display='none'">
Submit Button
</button>
in your example:
<input onclick="javascript:this.style.display='none'" type="submit" class="btn btn-success" name="complainSubmit" id="addProfileData" value="Submit"/>

save input data in php to notepad.txt

<form id="form" name="form" method="post" action="">
<div class="jquery-script-clear"></div>
<h1>BARCODE GENERATOR</h1>
<div id="generator"> Please fill in the code :
<input type="text" name="barcodeValue" id="barcodeValue" value="1234"><br> <br>
</div>
<div id="submit">
<input type="button" onclick="generateBarcode();" value=" Generate "> <input type="button" onclick="printDiv('print')" value="Print" />
</div>
</form>
<?php
$barcodeValue = $_POST["barcodeValue"];
$save = file_get_contents("save.txt");
$save = "$barcodeValue" . $save;
file_put_contents("save.txt", $save);
echo $save;
?>
sample picture
How to save the input data in save.txt file. When i clicked generate button the text file not showing in same folder.
The problem with your code is you have no submit button so your form was not actually posting when you pressed the button. if you look at my edits you can see I changed the button from input type="button" to type="submit". That allows for the form to submit back to the same php script.
Your script also was causing errors because you accessed $_POST["barcodeValue"] without checking if it existed. You also have to check if the save.txt exists before reading from it. If analyze my edits you can see how checking if the variables are available will help quite a bit.
<form id="form" name="form" method="post" action="">
<div class="jquery-script-clear"></div>
<h1>BARCODE GENERATOR</h1>
<div id="generator"> Please fill in the code :
<input type="text" name="barcodeValue" id="barcodeValue" value="1234"><br> <br>
</div>
<div id="submit">
<input type="submit" value=" Generate ">
</div>
</form>
<?php
if(isset($_POST["barcodeValue"]))
{
$barcodeValue = $_POST["barcodeValue"];
if(file_exists("save.txt"))
$save = file_get_contents("save.txt");
else
$save = "";
$save = $barcodeValue . $save;
file_put_contents("save.txt", $save);
echo $save;
}
?>
Let me know if you need more help

Categories