browser is blocking Javascript window.open - javascript

I want to create a button that opens a new tab with the user's name. But in Chrome it always block the popup window. Is there way to do this without enabling popup windows?
<div class="box">
<form action="" method="post">
<input type="text" name="meno" class="form-control" placeholder="Nick">
<input type="submit" name="submit" class="tlacitko" value="Submit">
</form>
<?php
if($_POST){
$nick = $_POST['meno'];
$hlasovat = "<script>window.open('https://czech-craft.eu/vote?id=16942&user=$nick') </script>";
echo $hlasovat;
unset($_POST);
}
?>
</div>

If you want your HTML form to open in a new window/tab when submitted you can use:
<form action="" method="post" target="_blank">
instead of
<form action="" method="post">
For your code to work using this technique you will probably also need to do something like:
$hlasovat = "<script>window.location.href = 'https://czech-craft.eu/vote?id=16942&user=$nick' </script>";
or
$hlasovat = "<script>window.location.assign('https://czech-craft.eu/vote?id=16942&user=$nick') </script>";
or
$hlasovat = "<script>window.location.replace('https://czech-craft.eu/vote?id=16942&user=$nick') </script>";
instead of
$hlasovat = "<script>window.open('https://czech-craft.eu/vote?id=16942&user=$nick') </script>";

Related

How can I submit form and stay on the same page after require?

I have a code which its simplified version could look like this :
file1.php
$array = array();
$array = new randomObject(1);
$array = new randomObject(2);
require('file2.php');
file2.php
<form method="post" action="?">
<?php
foreach ($array as $a) {
<p><?php echo $a->getAValue();
<textarea rows="5" cols="70" name="textbox[]">
</textarea>
</p>
<?php } ?>
<input id="isTrue"> //true or false
<input type="submit" >
</form>
The user is supposed to write answers in the textarea and click on submit then his answers are compared to the randomObject values. Then it shows if it's true or false next to each textarea
You are looking for something that the fron-tend will handle for you and an AJAX call is exactly what you need.
First of all, name your form
<form id="myForm" method="post" action="?">
<?php
foreach ($array as $a) {
<p><?php echo $a->getAValue();
<textarea rows="5" cols="70" name="textbox[]">
</textarea>
</p>
<?php } ?>
<input id="isTrue"> //true or false
<input id="submitButton" type="submit" >
</form>
Now you have proper id's both on the submit button and on the form itself.
<script>
let submitB = document.querySelector("#submitButton");
submit.addEventListener("click", function(e) {
e.preventDefault();
});
</script>
From now on you just have to write a proper ajax call to the url you wanted to access and you will be set to go.
If you need help with that let me know and I will throw something your way.
I am guessing you want to retain the values entered by the user, since they go away if you submit the form. (Page reloads)
This can be done by altering the input fields. If a value was submited pass that value to each corresponding input field.
Something like that:
<textarea rows="5" cols="70" name="textbox[]" <?php if(isset(value[SOMETHING])){?> value="<?php echo value[SOMETHING]; ?>" <?php } ?> >
This is just an example of how it would work. Make sure you adapt it to your code!

Form to redirect to a particular URL

I need a form with one input (eg. "number") that after sumbit redirects the broswer to http://staticurl/[number].html
How can I do?
header('location: http://staticurl/'.$_POST['number'].'html');
Maybe?
Assume a HTML form like
<form action="" method="POST">
<input type="number" name="number">
<input type="submit" name="submit" value="submit">
</form>
Now using php you can redirect
<?php
if(isset($_POST['submit'])){
header("Location: http://staticurl/".$_POST['number'].".html");
}
?>
If header() returns any error then you can do it with javascript inside php like
<?php
if(isset($_POST['submit'])){
echo '
<script>
window.location.href = "http://staticurl/'.$_POST['number'].'.html";
</script>
';
}
?>
Validate the form data before processing it.
Solved with
<script type="text/javascript">
function goTo()
{
var NUM= document.forms[0].NUM.value;
var URL = 'http://staticurl/'
window.location = URL+NUM+'.jpg';
return false;
}
</script>
And
<form action="" method="get" onsubmit="return goTo()">

How to set readonly all input beside input that user try to fill data?

How to set readonly all input except input that user try to fill data ?
When user loads page index.php and tries to fill data into EG: <input id="edValue2" ...>, I want to set readonly all input except <input id="edValue2" ...>
But my code not work, how can i do ?
index.php
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<html>
<body>
<form id="form-id" method="post" action="" ENCTYPE = "multipart/form-data" onsubmit="return checkform(this);">
<?PHP
for ($i=1;$i<=5;$i++)
{
?>
<input id="edValue<?PHP echo $i; ?>" type="text" onKeyUp="send_it_register_to_hidden_input<?PHP echo $i; ?>()" onKeyPress="send_it_register_to_hidden_input<?PHP echo $i; ?>()"><br>
<?PHP
}
?>
<br>
<br>
<br>
<input type="text" id="lblValue" value="">
<input type="text" id="lblValue_number" value="">
</form>
</body>
<?PHP
for ($i=1;$i<=5;$i++)
{
?>
<script>
function send_it_register_to_hidden_input<?PHP echo $i; ?>()
{
lblValue.value = $("#edValue<?PHP echo $i; ?>").val();
lblValue_number.value = <?PHP echo $i; ?>;
Check_register_it();
}
</script>
<?PHP
}
?>
<script>
function Check_register_it()
{
$('#form-id input').attr('disabled','disabled'); //disable all
$(this).removeAttr('disabled'); //enable the one that triggers the event
}
</script>
</html>
I strongly suggest don't create functions like that. It just makes your codes complicated. Use classes in this case so that you don't need to setup functions for each id that you want to manipulate:
<body>
<form id="form-id" method="post" action="" ENCTYPE = "multipart/form-data" onsubmit="return checkform(this);">
<?PHP for ($i=1;$i<=5;$i++) { ?>
<input class="input_boxes" type="text" data-i="<?php echo $i; ?>"><br/>
<?PHP } ?>
<br>
<br>
<br>
<input type="text" id="lblValue" value="">
<input type="text" id="lblValue_number" value="">
</form>
</body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$('.input_boxes').on('keyup', function(e){
var value = $(this).val();
var i_val = $(this).attr('data-i');
$('#lblValue').val(i_val);
$('#lblValue_number').val(value);
$('input').prop('readonly', true);
$(this).prop('readonly', false);
});
</script>
Assuming all inputs are children of the same element (i.e. siblings), you can use jQuery to do this easily:
$('input').on('focus', function(){
$('input').removeAttr('readonly');
$(this).siblings().attr('readonly', 'readonly');
});
JSBin:
http://jsbin.com/qukohosafuje/1/edit
Try this :-
$("input").on('keyup', function () {
$('input').not(this).attr('readonly', 'readonly');
});
DEMO
You have to put all JQuery statement within:
$(function(){
//put your JQuery code here
});
Try and let me know if now works.
You can simply use the html attribute of readonly for the inputs you don't want users to fill.
e.g
<input type="text" id="lblValue" value="" readonly>
if you would required the readonly inputs to be accessible after the necessary input has been filled, you can use jquery to trigger the event.
e.g
$('input').keyUp(function(){
if($('#edValue2').val()!=""){
$('input:not("#edValue2")').removeAttr('readonly');
})

How to show box after submit value in form

So I have pop-up window where user need to enter name, after clicking submit button this window close.
You can test it here: http://eurokos.lt/under20/button.php (Click 21 button, then try to enter any value and see what happens)
For this pop-up box I've used function:
function popUp() {
document.getElementById('light').style.display='block';
document.getElementById('fade').style.display='block';
}
This function is used for X button to close window:
function closeWindow() {
document.getElementById('light').style.display='none';
document.getElementById('fade').style.display='none';
}
My button which open pop-up window looks like:
Echo "<input type='button' onclick='popUp();' value='".$i."' />";
And here is PHP_SELF and form:
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "</br>You have entered: <b> $name </b>";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
`
What I need to do that posted result what entered and window not closed?
Maybe I need to use popUp() function again when submitting? But how to do that correctly? Thank you.
Try this:
html:
<form method="post" action="" onsubmit="return display()">
<div id="answer"></div>
<input type="text" name="name" id="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
javascript:
function display(){
var ans = document.getElementById("name").value
document.getElementById("answer").innerHTML="You have entered:"+ans;
return false;
}
Update:
function popUp(){
document.getElementById("answer").innerHTML=""; //Add these in your popup function
document.getElementById("name").value="";
}
First of all i suggest you to avoid PHP_SELF in the action it can be manipulated by people and it's not good.
at top of page try to do :
<?php
$thispage = $_SERVER['PHP_SELF'];
?>
<html>
.
.
.
<form method="post" action="<?=$thispage?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
</html>
Then, using the action for sending data to PHP, will reload the page so you are probabilly losing all the inline fix you did.
Try to get the input values after the submit, assign them to varibles and do, for example:
xmlhttp.open("POST","<?=$thispage?>",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("name=name");
with an asynchronous call you don't reload the page and you don't lost your changes

Require logging off before log in

I have two buttons, one to log in and another to log out.
Now I want only one button, I want that it only shows the log in button.
And if you are logged in, then the page refresh (already done) but then I want that the log in button is changed into a logoff button. So that you don't log in two or more times a day and you have to log out first.
<form action="begintijd.php" method="POST">
<input name="begintijd" value="aanmelden" type="submit">
</form>
<form action="eindtijd.php" method="POST">
<input name="eindtijd" value="afmelden" type="submit">
</form>
header("Refresh: 2; index.php");
So you can log in and off so many times you want. But I only want that you can log in once and then you first have to log out before you can log in.
(some things are in dutch, you can overwrite it if you want)
Simple.
You are using a cookie to set login data. If cookie is set, then display button 1, else display button 2, like;
<?php
if (isset($_COOKIE["user"])){
echo '<form action="begintijd.php" method="POST">
<input name="begintijd" value="aanmelden" type="submit">
</form>';
}else{
echo '<form action="eindtijd.php" method="POST">
<input name="eindtijd" value="afmelden" type="submit">
</form>';
}
?>
Use sessions: Using sessions & session variables in a PHP Login Script
<?PHP
session_start();
if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
echo '<form action="begintijd.php" method="POST">'.
'<input name="begintijd" value="log in" type="submit">'.
'</form>';
}
else
{
echo '<form action="begintijd.php" method="POST">'.
'<input name="begintijd" value="log out" type="submit">'.
'</form>';
}
?>
You need to read about session variables or cookies.
If your are using sessions.You can do using this.
<?php if($_SESSION['logged_in']==true){?> //if user is logged in show logout button
<form action="begintijd.php" method="POST">
<input name="begintijd" value="aanmelden" type="submit">
</form><?php } else {?>//if user is logged out show login button
<form action="eindtijd.php" method="POST">
<input name="eindtijd" value="afmelden" type="submit">
</form><?php }?>
You can try this.
<form action="<?=($_SESSION['login']!="")?"begintijd.php":"eindtijd.php"?>" method="POST">
<input name="<?=($_SESSION['login']!="")?"begintijd":"eindtijd"?>" value="<?=($_SESSION['login']!="")?"aanmelden":"afmelden"?>" type="submit">
</form>
Send us your begintijd.php and eindtijd.php codes

Categories