if(isset($_POST['Submit'])){} Not working - php

I am trying to figure out the problem in this code but no luck. When I click on the form submission it doesn't generate any alert even its not echo anything. Please have a look on the code and let me know if there is anything I am missing`
<form action="" method="post">
<textarea rows="6" cols="70" name="notes" placeholder="Enter Notes"></textarea><br>
<input type="submit" name="submit1" id="submit1" class="upload-button" style="margin-left:40%" value="Post" />
</form>
<?php
if(isset($_POST['submit1'])) {
echo '<script type="text/javascript">';
echo 'alert("This is alert");';
echo '</script>';
echo 'ewuifbhfiueruigriug';
}`
?>

You have a backtick at the end of your php. Just remove it. The code works as expected once the backtick is removed.
<?php
if(isset($_POST['submit1'])) {
echo '<script type="text/javascript">';
echo 'alert("This is alert");';
echo '</script>';
echo 'ewuifbhfiueruigriug';
}` <=== right HERE
?>

<?php
if(isset($_POST['submit1'])) {
echo '<script type="text/javascript">';
echo 'alert("This is alert");';
echo '</script>';
echo 'ewuifbhfiueruigriug';
}
echo '<form action="" method="post">';
echo '<textarea rows="6" cols="70" name="notes" placeholder="Enter Notes"></textarea><br>';
echo '<input type="submit" name="submit1" id="submit1" class="upload-button" style="margin-left:40%" value="Post" />';
echo '</form>';
?>
If you look at the end of the code for the php submit, there's a ' that shouldn't be there in the OP.

Something like this should very well work (which is not so far from what you did).
<?php
if(isset($_POST['submit1'])) {
echo '<script type="text/javascript">';
echo 'alert("This is alert");';
echo '</script>';
echo 'ewuifbhfiueruigriug';
}
?>
<section>
<form action="" method="post">
<textarea rows="6" cols="70" name="notes"
placeholder="Enter Notes"></textarea><br>
<input type="submit" name="submit1"
id="submit1" class="upload-button"
style="margin-left:40%" value="Post" />
</form>
</section>
Side-Note: If your intent is not to submit the Form but to see an Alert Message once the button is clicked, you may do something like this:
<section>
<form action="" method="post">
<textarea rows="6" cols="70" name="notes"
placeholder="Enter Notes"></textarea><br>
<input type="submit" name="submit1"
id="submit1" class="upload-button"
style="margin-left:40%" value="Post" />
</form>
</section>
<?php
// ECHO OUT RAW JAVASCRIPT
// TO PREVENT DEFAULT SUBMIT-BUTTON BEHAVIOUR WHEN CLICKED.
echo '<script type="text/javascript">';
echo "function stopBehaviour(evt) {";
echo "evt.preventDefault();";
echo 'alert("This is alert");';
echo "}";
echo "document.getElementById('submit1').addEventListener( 'click', stopBehaviour, false );";
echo '</script>';
?>
Notice the Placement of the PHP Code in both instances... especially the latter... However, be clear that your Form will never get submitted as you are preventing the Button's Default behaviour... and there would also be no need to check if the form was submitted or not (using PHP)... The earlier Approach above would submit the Form but once the Form gets Submitted, PHP automatically adds a Javascript code which will stall the entire Form-Submission Trip till the Alert message gets run and your ewuifbhfiueruigriug gets written to the Stream...

Related

interface php and html, in foreach loop

I have a PHP page that shows me data from the database. I'm inserting a button that, if pressed, deletes the record. The problem is that if I push the button the action is done on all the records, because I can't uniquely identify the click on the button
some code
foreach ( $associati as $associato ) {
echo "<hr />";
echo "Nome associato : ".$associato->Nome."</br>";
echo "Codice Fiscale : ".$associato->CF."</br>";
echo "<hr />";
if(isset($_POST["numerazione"])){
echo "Hello world"; //Query for delete
}
?>
<form method="POST" action="">
<input type="submit"
name="numerazione"
value="Elimina utente"
onclick="return confirm('Are you sure?')"
/>
</form>
<?php
}
How can I do to uniquely identify the button?
You can add a hidden field to each form that contains the unique identifier of the data, that means when you click the button, it will create a POST request, and in that POST request you can get the ID of the clicked record by doing $_POST['unique-id'], also make sure to populate the value of that hidden field using PHP
<?php
foreach ( $associati as $associato ) {
echo "<hr />";
echo "Nome associato : ".$associato->Nome."</br>";
echo "Codice Fiscale : ".$associato->CF."</br>";
echo "<hr />";
if(isset($_POST["numerazione"])){
$numerazione = $_POST["unique-id"];
echo "Unique record is : ".$numerazione."</br>";
}
?>
<form method="POST" action="">
<input type="hidden" name="unique-id" value="<?php echo $associato->CF; ?>" />
<input type="submit"
name="numerazione"
value="Elimina utente"
onclick="return confirm('Are you sure?')"
/>
</form>
<?php
}
?>
Pass the unique information (e.g. $id or $associato->id or whatever the variable which can identify the record) when the form is submitted
<form method="POST" action="">
<input type=hidden name=id value=<?php echo $id; ?>>
<input type="submit"
name="numerazione"
value="Elimina utente"
onclick="return confirm('Are you sure?')"
/>
</form>
Solution for this problem:
if(isset($_POST["mod"]) and $_POST["id"]== $associato->ID ){
echo "Hello world";
}
?>
<form method="POST" action="">
<input type="hidden" name="id" value=<?php echo $associato->ID; ?>>
<input type="submit"
name="mod"
value="valore"
onclick="return confirm('Are you sure?')"
/>
</form>

How start a JQuery inside PHP

I want to start a jquery (freeow plugin) when I got an error in e-mail input field form.
I'm checking some fields on the form and each error that I got I want to open an alert message.
I can do this using a button, like this...
<div id="freeow-tr" class="freeow freeow-top-right"></div>
<div id="freeow-br" class="freeow freeow-bottom-right"></div>
<div class="form-line-check">
<input id="freeow-title" value="Freeow!" type="hidden" class="text" />
<input id="freeow-error" value="0" type="hidden" />
<input id="freeow-position" value="#freeow-tr" type="hidden" />
<input id="freeow-style" value="smokey" type="hidden" />
<input id="freeow-message" value="Error message..." type="hidden" />
</div>
<div class="form-line">
<input id="freeow-show" type="button" value="Click to Freeow!" />
</div>
How do this without the button? Inside the PHP code.
if ($erro == "0") {
if(filter_var($email01, FILTER_VALIDATE_EMAIL)){
$erro = "0";
} else {
echo "<div id=\"freeow-tr\" class=\"freeow freeow-top-right\"></div> ";
echo "<div id=\"freeow-br\" class=\"freeow freeow-bottom-right\"></div> ";
echo "<div class=\"form-line-check\"> ";
echo "<input id=\"freeow-title\" value=\"Freeow!\" type=\"hidden\" class=\"text\" /> ";
echo "<input id=\"freeow-error\" value=\"0\" type=\"hidden\" /> ";
echo "<input id=\"freeow-position\" value=\"#freeow-tr\" type=\"hidden\" /> ";
echo "<input id=\"freeow-style\" value=\"smokey\" type=\"hidden\" /> ";
echo "<input id=\"freeow-message\" value=\"Error message...\" type=\"hidden\" /> ";
echo "</div> ";
$erro = "1";
}
}
You have to create a javascript function
function mensagem01() {
var title, message, opts, container;
title = $("#freeow-title").val();
message = $("#freeow-message").val();
message = 'E-mail not in database...';
opts = {};
opts.classes = [$("#freeow-style").val()];
opts.prepend = false;
opts.classes.push("error");
opts.autoHide = false;
opts.classes.push("slide");
opts.hideStyle = { opacity: 0, left: "400px" };
opts.showStyle = { opacity: 1, left: 0 };
container = $("#freeow-position").val();
$(container).freeow(title, message, opts);
}
HTML code need to have the freeow definitions
<div id="freeow-tr" class="freeow freeow-top-right"></div>
<div id="freeow-br" class="freeow freeow-bottom-right"></div>
<div class="form-line-check">
<input id="freeow-title" value="Lembrou?" type="hidden" class="text" />
<input id="freeow-error" value="0" type="hidden" />
<input id="freeow-position" value="#freeow-tr" type="hidden" />
<input id="freeow-style" value="smokey" type="hidden" />
<input id="freeow-message" value="message field must be empty" type="hidden" />
</div>
Inside PHP code
You need to include the javascript code to call the javascript function message
echo "<script type=\"text/javascript\"> $(document).write(mensagem01()); </script> \n";
When the php is executed the javascript write the document, which contains the call to the freeow message.
What you are trying to achieve will is best done with AJAX and some more logic than you have in your code.
You want to catch the form submit with jQuery. Using event.prefentDefault() you can stop the form from submitting. Then, you want to AJAX your FormData to the server.
In your PHP script, you do your validation. If it succeeds, return something (for example, a JSON object) that tells your front end that the validation was a success. If not, you want an object that describes what exactly is failing.
From front end, you check to see if the validation was a success (do whatever), or not (show freeow)

HTML Forms in PHP

i am writing html form code with in php script
here it is
<?php
$problem_code="STR";
echo '<form name="submit-button" action="/codejudge/submit.php?id='.$problem_code.'">';
echo '<button type="submit" >Submit</button>';
echo "</form>";
?>
But after submitting url look like this localhost/codejudge/submit.php ?
but it should be like this localhost/codejudge/submit.php?id=STR
If a form is method="GET" (which is the default), as this one is, then submitting it will erase the existing query string in the action.
Store the data in a hidden input instead.
<?php
$problem_code="STR";
?>
<form name="submit-button" action="/codejudge/submit.php">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($problem_code); ?>">
<button type="submit">Submit</button>
</form>
You should specify a method of form submit.
$problem_code="STR";
echo '<form method=post name="submit-button" action="/codejudge/submit.php?id='.$problem_code.'">';
echo '<button type="submit" >Submit</button>';
echo "</form>";

how to send javascript variable as a php parameter

hi i have a javascript function which opens a pop-up window and opens a php file into it but i even want to send a parameter with it but it doesnt work the parameter is sent as a variable name instead of its value
here is my script
<?php
if($addflag == 0){
echo "
<script type=\"text/javascript\">
function mopen(){
var mobj=document.getElementById('dtype').value; //// this variable is shown as a name instead of its value
window.open('quotprin.php?vouchno=' . urlencode($getvouch) . '&dtype=mobj','popUpWindow','height=800,width=950,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');
}
</script>
";
echo "<td>";
echo '<font color="red">Print On Letter Head</font>
<input type="checkbox" id="dtype" name="dtype" value="1" checked="checked" />';
echo '<input class="cmdprint" type="button" id="submit" name="print" value="Print" onclick="mopen();"></td>';
echo "<td>";
}
?>
This is not exactly your original code, but it works. You have too many escape slashes and single or double quotes. It can be too confusing to figure out errors like this.
<?php
$getvouch = isset($_GET['getvouch'])? $_GET['getvouch'] : null;
?>
<script type="text/javascript">
function mopen(){
var mobj=document.getElementById('dtype').value;
var url="quotprin.php?vouchno=<?php echo $getvouch; ?>&dtype="+ mobj;
window.open(url,'popUpWindow','height=800,width=950,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');
}
</script>
<table><tr><td>
<font color="red">Print On Letter Head</font>
<input type="checkbox" id="dtype" name="dtype" value="1" checked="checked" />
<input class="cmdprint" type="button" id="submit" name="print" value="Print" onclick="mopen();"></td>
</tr></table>
Most important here is to see how the "getvouch" variable is added to the getmobi url.
If you use this url: add-a-javascript-var-and-send-it-with-post.php?getvouch=6
get mobi links to : quotprin.php?vouchno=6&dtype=mobj
If this is not the issue, please explain a little better.
UPDATE.
Now I see the error is you did not escape the mobj variable in the url. try the changes.
try this:
<?php
if($addflag == 0){
$getvouch = urlencode($getvouch);
echo "
<script type=\"text/javascript\">
function mopen(){
var mobj=document.getElementById('dtype').value; //// this variable is shown as a name instead of its value
window.open('quotprin.php?vouchno= {$getvouch}&dtype=mobj','popUpWindow','height=800,width=950,left=100,top=100,resizable=yes, scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');
}
</script>
";
echo "<td>";
echo '<font color="red">Print On Letter Head</font>
<input type="checkbox" id="dtype" name="dtype" value="1" checked="checked" />';
echo '<input class="cmdprint" type="button" id="submit" name="print" value="Print" onclick="mopen();"></td>';
echo "<td>";
}
?>

Passing PHP form variables from one page to other

my php configuration on server are showing i can post variables to maximum size upto 8MB , thats enough .. but how to check for number of variables , sever is running ubuntu 4.4 , php .
i have a page which takes students marks and send them to a action page , but on action page doing echo for the post variables nothing is being displayed , where are doing an echo "hello"; this shows ...
this is the page which sends the variables
<form name="frm" action="marklistI.php" method="POST" class="" >
<?php $tb->displayTable() ?>
<div class="mainframe">
<input type="hidden" name="batch" value="<?php print $_GET['batch']; ?>"/>
<input type="hidden" name="sem" value="<?php print $_GET['sem']; ?>" />
<input type="hidden" name="chance" value="<?php print $_GET['chance']; ?>"/>
<input name="submit" type="submit" class="hide" value="Save"/>
<input type="hidden" name="url" value="<?php print $_SERVER['REQUEST_URI']; ?>"/>
</div>
</form>
and this are the variables are coming to action page .. but on echo they are not showing any value .
$dept =$_COOKIE['dept'];
$join=$_POST['batch'];
$type='e';
$sem=$_POST['sem'];
$chance=$_POST['chance'];
try placing this code on your action page:
if (isset($_GET)) {
echo "<h3>GET METHOD</h3>";
var_dump($_GET);
}
if (isset($_POST)) {
echo "<h3>POST METHOD</h3>";
var_dump($_POST);
}
if (isset($_COOKIE)) {
echo "<h3>COOKIE METHOD</h3>";
var_dump($_COOKIE);
}
See which method returns your variables and use it, otherwise, you are not filling any values on the form.
this is your code:
<form name="frm" action="marklistI.php" method="POST" class="" >
<?php $tb->displayTable(); ?>
<div class="mainframe"> <input type="hidden" name="batch" value="<?php print $_GET['batch']; ?>"/>
<input type="hidden" name="sem" value="<?php print $_GET['sem']; ?>" />
<input type="hidden" name="chance" value="<?php print $_GET['chance']; ?>"/>
<input name="submit" type="submit" class="hide" value="Save"/>
<input type="hidden" name="url" value="<?php print $_SERVER['REQUEST_URI']; ?>"/>
</div>
</form>
One possible reason for your issue:
You use "_GET[]" variables here but the form is POST.
GET and POST are two different methods to send data, GET is in the URL path (a=&b=&c=) while POST is hidden in the HTML headers.
So make sure you read those results as "$_POST['name']" and not GET.
I suggest this in the "receiving script" for debugging:
var_dump($_GET);
var_dump($_POST);
And in your browser use Chrome or Firefox + Firebug and Press "f12".
In that debugger you can catch the POST when you click the button and you can look which variables were sent.
That should help you debug your issue fast.
One other suggestion, I personally would write the code less "mixed".
It makes it hard to read and hard to modify.
Why not like this:
<?php
echo "
<form name='frm' action='marklistI.php' method='POST' class='' >".
$tb->displayTable().
"<div class='mainframe'>
<input type='hidden' name='batch' value='$_GET[batch]'/>
<input type='hidden' name='sem' value='$_GET[sem]' />
<input type='hidden' name='chance' value='$_GET[chance]'/>
<input name='submit' type='submit' class='hide' value='Save'/>
<input type='hidden' name='url' value='$_SERVER[REQUEST_URI]'/>
</div>
</form> ";
?>
My guess for your problem is that those values in the formular are actually empty, that's why you don't receive anything.

Categories