how to send javascript variable as a php parameter - php

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>";
}
?>

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>

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

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...

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)

how to send a check box value as a php parameter on a button click

hi i have a check box and a button which opens a popup window on a button click i am sending a parameter which works fine now i have added a check box and i want to send its value as a parameter too and i am stuck here n have no idea of what has to be done
here is my script
<?php
if($addflag == 0){
echo "<td>";
echo '<font color="red"><strong>Print On Letter Head</strong></font><input type="checkbox" id="dtype" name="dtype" value="1" checked></input>';
echo '<input class="cmdprint" type="button" id="submit" name="print" value="Print" onclick="window.open(\'quotprin.php?vouchno='.$getvouch.'&dtype=\'document.getElementById(\'status1\').value;\'\',\'popUpWindow\',\'height=800,width=950,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes\');"></td>';
echo "<td>";
}
?>
somthing like this should work.
on suggestion try to avoid inline js and use functions instead.
other suggestion try to use checked="checked" instead of checked.
add this to javascript
<?php
if($addflag == 0){
echo '
<script type="text/javascript">
function mopen(){
var mobj=document.getElementById(\'dtype\');
var mval=mobj.value;
window.open(\'quotprin.php?vouchno='.$getvouch.'&dtype=mval\',\'popUpWindow\',\'height=800,width=950,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes\');
</script>
';
}
and the PHP
<?php
if($addflag == 0){
echo "<td>";
echo '<font color="red"><strong>Print On Letter Head</strong></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>";
}
?>

How to get selected radio type input and use it as a variable?

How can i get the selected radio input type value in a php script and direct it to a different directory to retrieve the files selected. I tried writing the code, but couldn't write it properly since i m a newbie in the world of php.
The code i have written for displaying radio button is
<input type="radio" name="radio<?php echo $orderby1[0]; ?>"
id="<?php echo $orderby1[0]; ?>" value="<?php echo $orderby1[0]; ?>"
/><?php echo $orderby1[0]; ?>
Now how to get the selected values from it and direct it to the directory contains the file?
The code i have written is , which doesn't seems to work
<script type="text/javascript">
var element = document.getElementById("<?php echo $orderby1[0]; ?>");
jmolApplet(400, "load /jmol/'element.value'.txt");
</script>
I don't want to include any for loops... that makes my script work differently.
Please help!
You would use a $_REQUEST method within a form, something like:
<?php
if ($_REQUEST['radiotest']) {
echo "You chose ".$_REQUEST['radiotest'];
}
?>
<form>
<div><input type='radio' name='radiotest' value='1' /> Option 1</div>
<div><input type='radio' name='radiotest' value='2' /> Option 2</div>
<div><input type='radio' name='radiotest' value='3' /> Option 3</div>
<div><input type='submit' value='Submit' /></div>
</form>
You need first to execute when the radio has rendered and then not put the value inside the quotes
function someEvent() {
var element = document.getElementById("<?php echo $orderby1[0]; ?>");
jmolApplet(400, "load /jmol/"+element.value+".txt");
}

Categories