This is my js code:
function check() {
var delvar = "<? $_POST["del"]; ?>";
var answer = confirm("Are you sure you want to delete the article?")
if (answer) {
window.location = "adm-site.php?del=" + delvar + "&delete=true";
}
}
Now, my problem is that the delvar does not work. so the url is just ?del=&delete=true , instead of e.g. ?del=testarticle&delete=true
EDIT: My $_POST["del"] is a select tag with all the articles in it, so you will choose which to delete
The HTML code:
<select name="del">
<option value="none" SELECTED></option>
all articles echo'ed here by php
</select>
I believe you want this scenario:
User gets a page with a form containing the select tag having all the articles as values/options.
User selects an article, which he/she wants to delete.
User clicks a button.
Then you want to redirect user to URL adm-site.php?del=02&delete=true.
So, in this case, only JavaScript is getting the value of selected article value and embedding it in the redirect URL. So, PHP can do nothing here. All the action is happening client-side.
Try this HTML code:
<html>
<head>
<script language="JavaScript">
function check(){
var delvar=document.form1.del.options[document.form1.del.selectedIndex].value;
//alert(delvar); //debug
var answer = confirm("Are you sure you want to delete the article?")
if (answer) {
//alert("adm-site.php?del=" + delvar + "&delete=true");
window.location = "adm-site.php?del=" + delvar + "&delete=true";
}
}
</script>
</head>
<body>
<form name="form1">
<select name="del">
<option value="none" SELECTED>None</option>
<option value="01">Article 01</option>
<option value="02">Article 02</option>
<option value="03">Article 03</option>
</select>
<input type="button" value="Delete This Article" onclick="check();">
</form>
</body>
</html>
LIVE Demo at JSFiddle.net (Alerting the Redirecting URL)
Things I have edited/changed:
Added a name to form tag.
Changed the way var delvar is assigned the value of currently selected article.
Else all is same code as yours.
As mentioned by Pheonix, A bit secure code, doing the same thing as the above one, but by using $_POST. You need to use $_POST array instead of $_GET in your adm-site.php.
<html>
<head>
<script language="JavaScript">
function check(){
var delvar = document.form1.del.options[document.form1.del.selectedIndex].value;
var agree = confirm("Are you sure you want to delete Article " + delvar + " ?");
if (agree)
return true ;
else
return false ;
} //function check() ENDS
</script>
</head>
<body>
<form name="form1" action="adm-site.php" method="POST">
<select name="del">
<option value="none" SELECTED>None</option>
<option value="01">Article 01</option>
<option value="02">Article 02</option>
<option value="03">Article 03</option>
</select>
<input type="hidden" name="delete" value="true" />
<input type="submit" name="submit" value="Delete this Article" onClick="return check();" />
</form>
</body>
</html>
Demo at JSFiddle.net
var delvar = '<?php echo $_POST["del"]; ?>';
note the <?php instead of the discouraged shortcode too.
Your HTML contains " so you need to surround the PHP with single apostrophes. And if you're using the short tag, use it right. So in the end, it should look like this:
var delvar = '<?= $_POST["del"]; ?>';
Here's another way to think about it: make a link to the page you want to be sent to, and then give it an onclick like this:
<a href="/adm-site.php?del=<?php echo $_POST['del']; ?>&delete=true" onclick="return confirm('Confirm Deletion?')" >Delete</a>
This works, but it might not fit into your environment, depending on what triggers check().
var delvar = "<? $_POST["del"]; ?>";
You are trying to combine javascript and php but we don't have that type of possibility because javascript is client side scripting language, PHP is server side scripting language.
There is another way to combine javascript & php. Please refer this link.
Related
How it's possible to get jquery variable value to PHP variable
My Jquery code
jQuery(document).ready(function(){
jQuery("#mec_make").change(function(){
jQuery("#frame").html('');
makeval = jQuery(this).val();
jQuery("#sec").val(makeval);
});
});
</scipt>
HTML
<input type="hidden" name="te" value="" id="sec">
I'll try to get jquery value in my php code but i cant get it.
PHP
echo $year = '<script>jquery("#sec").val()</script>';
echo $year = '<script>document.write(document.getElementById("sec").value)</script>';
i don't need ajax i want it without ajax , i need it when change event fire value will store to php variable.
Full Code
<div class="mak" id="mmm"><input type="hidden" name="te" value="" id="sec"></div>
<?php
echo "buddy".$year = '<script>jQuery("#sec").val();</script>';
$get_recent = $wpdb->get_results("SELECT * FROM wp_make_details
WHERE post_id =".$post_id."
AND year =$year
AND make_name ='Yamaha'
");
foreach ($get_recent as $key => $rec_value) {
$rec_value_year_data[] = $rec_value->year;
$model_name['model_name'] = $rec_value->model_name;
$datarrr[] =$model_name;
}
?>
<select name="mec_year" id="car_year_select">
<?php
$post_id = get_the_ID();
$inserted_year = get_post_meta($post_id,'year',true);
echo "<option value=".$inserted_year.">".$inserted_year."</option>"; ?>
<script>
jQuery(document).ready(function(){
jQuery("#mec_make").change(function(){
jQuery("#frame").html('');
makeval = jQuery(this).val();
jQuery("#sec").val(makeval);
});
});
</script>
</select>
If you just want to put mec_make value into hidden field id="sec" than why are you using PHP here, you can get the desired output as:
jQuery(document).ready(function(){
jQuery("#mec_make").change(function(){
jQuery("#frame").html('');
makeval = jQuery(this).val();
jQuery("#sec").val(makeval);
console.log(jQuery("#sec").val()) ;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<input type="hidden" name="te" value="" id="sec">
<select name="mec_make" id="mec_make">
<option>Select</option>
<option value="1">My Value</option>
</select>
Also note that, this </scipt> should be </script>, fix the typo.
Now, how can you get the values in php, simple answer use <form> and submit button, you will get the values in php.
hy, i have a problem with a form. i know the question is simple but i can not have a solution. Well, this is my form:
<form id="search" method="post" action="cerca_redirect2.php" >
<select id="tipo" name="tipo"class="chzn-select" style="width:165px;" tabindex="1" >
<option value="http://case.vortigo.it/vendita-immobili/index.php"> Vendita</option>
<option value="http://case.vortigo.it/affitto-immobili/index.php">Affitto</option>
</select>
<input id="field" name="field" type="text" value=""/>
<input id="submit" type="submit" value="" />
</form>
my goal is when i select "Vendita" and i submit the form i have to go to the url in the select "Vendita", for each select. someone can help me? thanks
In the server side php code, do something like this
if (isset($_POST['tipo']) && !empty($_POST['tipo']))
{
header('Location: ' . $_POST['tipo']);
}
Note: This is a very basic version, you will want to ensure the url is valid by either maintaining a list of urls on the server, or something similar.
There are many different ways, but you can for example use following:
See the onsubmit part in the form definition
<form id="search" method="post" action="cerca_redirect2.php" onsubmit="this.action=document.getElementById('tipo')[document.getElementById('tipo').selectedIndex].value" >
If I understan your question correctly, you need a way to change the action value to the selected option's value
this is how to do that
$(document).ready(function(){
$("#tipo").on("change", function(){
$("#search").attr("action", $(this).val());
});
});
DEMO: http://jsfiddle.net/6ybMP/
You want to change the action of the form based on the select? The following should be along the lines of what you want.
$('#tipo').on('change', function() {
var newAction = this.val();
$('#search').prop('action', newAction);
}
UPDATE
You will want to wrap this code in $(document).ready() so that the event will be registered after the DOM has been loaded.
Like so:
$(document).ready(function() {
$('#tipo').on('change', function() {
var newAction = this.val();
$('#search').prop('action', newAction);
}
});
I'm trying to change a series of links by inserting values from a dropdown box. I found this straight forward to do with php (code below) but I'd like to be able to change the links without 1) having a page refresh (these links are near the bottom of the homepage) and 2) without a submit button if possible.
Here is the php code:
<form name="test" action="<?php echo
htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<select name="county" id="select13">
<option value="All">All</option>
<option value="County1">County1</option>
<option value="County_2">County 2</option>
...
</select>
<input type="submit" name="submit" value="Set County">
</form>
<?php
if(isset($_POST['submit']))
{
$county = $_POST['county'];
$www = "http://www.website.com/";
$food = "/topics/Food/programs.aspx";
$meals = "/topics/Meals/programs.aspx";
....
echo "<a href=$www/$county/$food>Food</a><br>";
echo "<a href=$www/$county/$meals>Meals</a><br>";
The above code works fine, but requires a page refresh. I was attempting to use jquery to just take a list of links, such as:
Food
And just replace "county" with the value from a dropdown box.
function changelinks(){
var countyvals = $("#county").val();
$("a[href^='http://www.webpage.com/topics/']")
.each(function(){
this.href = this.href.replace(/^http:\/\/www\.webpage\.com\/topics\//,"http://www.webpage.com/topics/"+countyvals);
});}
The above code works fine changing the link once, but after the link is changed I can't target the links anymore. I just started teaching myself jquery today, so I've no idea where to look for a better solution.
What might be the best way of changing these links?
Should I try some ajax on the php code, or is there a better/simpler solution through jquery?
THANKS!
I would use HTML 5 data attributes. Have a data-relativeurl attribute on each anchor of interest, then you can use
function changelinks(){
var countyvals = $("#selector13").val();
$("a[href^='http://www.webpage.com/topics/']")
.each(function(){
this.href = "http://www.webpage.com/" + countyvals + $(this).attr('data-relativeurl');
});}
I have got a form (php in html or the other way around). Once user selects an option in a drop down list, it would get the input value and create a few text boxes. It seems like I have to use onchange(). How do I read the input and perform logics within the script inself? Instead of opening another .php script?
Currently this is what I have.
<?php
$tables = $_POST["tables"];
?>
<html>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
Table Name: <div id="tables">
<select name="tables">
<option value="Applications">Application</option>
<option value="Device">Device</option>
</select>
</div>
</form>
<?
echo "".$tables."";
?>
You can't interact with PHP once the HTML is sent to the browser without either
Refreshing the page, or
Using AJAX (JavaScript).
If you know the options in the <select> beforehand (which it seems like you do), you should write some JavaScript to accomplish what you need. Here is a simple example using jQuery.
$('#tables_select').change(
function( eventObject ){
alert('You chose ' + $(this).val());
switch( $( this ).val())
{
case 'Applications':
$('#tables').append('<input type="text" name="application_name" value="Enter an application name" />"');
break;
case 'Device':
$('#tables').append('<input type="text" name="device_name" value="Enter a device name" />"');
break;
}
}
);
You will need to add additional logic to remove the inserted elements if the user changes their choice, and to insert the correct <input> elements when the page first loads, but it is a good starting point.
if you want to add any input type ... here is the demo demo with code
you use following method.
<form method="post" action="<?php echo $PHP_SELF;?>" onChange="return createTxtbox()">
Table Name: <div id="tables">
<select name="tables">
<option value="Applications">Application</option>
<option value="Device">Device</option>
</select>
</div>
<span id="fooBar"> </span>
</form>
then write javascript,
<SCRIPT language="javascript">
function createTxtbox() {
var type="text";
//Create an input type dynamically.
var element = document.createElement("input");
//Assign different attributes to the element.
element.setAttribute("type", type);
element.setAttribute("value", type);
element.setAttribute("name", type);
var foo = document.getElementById("fooBar");
//Append the element in page (in span).
foo.appendChild(element);
}
</SCRIPT>
I have a PHP script designed to allow users to decide which language a page is displayed in. The information is stored in a cookie and then read when needed to display the correct content.
Currently, I use an HTML dropdown box to allow the user to select the language and then they must press the form submit button to set the cookie. How can I make it so when they select the language in the dropdown menu it automatically selects that and submits the form? I hope you can understand my question.
My current PHP code is:
<?php
$user_lang = null;
if (isset($_POST["setc"])) {
$expire = time() + 60 * 60 * 24 * 30;
setcookie("mycookie", $_POST["sel"], $expire);
header("location: " . $_SERVER["PHP_SELF"]);
} else if (isset($_COOKIE["mycookie"])) {
$user_lang = $_COOKIE["mycookie"];
}
?>
<meta charset='utf-8'>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<select name="sel" size="1">
<option value="en"<?php echo (!is_null($user_lang) && $user_lang === "en" ? " selected" : ""); ?>>English</option>
<option value="es"<?php echo (!is_null($user_lang) && $user_lang === "es" ? " selected" : ""); ?>>Español</option>
<option value="fr"<?php echo (!is_null($user_lang) && $user_lang === "fr" ? " selected" : ""); ?>>Français</option>
<option value="de"<?php echo (!is_null($user_lang) && $user_lang === "de" ? " selected" : ""); ?>>Deutsch</option>
</select>
<input name="setc" value="save setting" type="submit">
</form>
Change your select tag to:
<select name="sel" size="1" onchange="this.form.submit();">
to make the form submit when users selects a language.
If you wish to do that without JavaScript you can use multiple submit buttons method instead:
<input name="set_language[en]" value="English" type="submit">
<input name="set_language[es]" value="Español" type="submit">
<input name="set_language[fr]" value="Français" type="submit">
<input name="set_language[de]" value="Deutsch" type="submit">
Processing this form is simple as it is:
if (isset($_POST["set_language"])) {
$language = key($_POST["set_language"]);
// $language contains user-selected language code now
}
This can't be done with a select field but without JavaScript or any other client-side scripting language.
You need to do it using JavaScript:
<select name="sel" size="1" onchange="this.form.submit();">
Or if you use jQuery:
$("select[name='sel']").change(function() {
$(this).parent().submit();
});
This will submit the form whenever a user changes value in the dropdown list.
You can add an onchange event to the select:
<select onChange="document.forms[0].submit();">
Remember that it's a good thing to leave the submit button for those users who don't have javascript enabled. You can hide the button using javascript, so it won't clutter the window of those that doe have a regular browser.
I think you have to use javascript like the others said, but you can also use the submit button as a backup if the user doesn't have javascript activated:
<noscript><input value="save setting" type="submit"></noscript>
And then, since the submit button will only be shown when the user has javascript disabled, you will need to add a hidden input to check if the form has been submitted on the server side
<input type="hidden" name="setc" value="true" />
The answer is in JavaScript not PHP. You could do it unobtrusively with JavaScript like this:
<script>
window.onload = function() {
document.getElementById("idOfDropDown").addEventListener("change", function() {
document.forms["formName"].submit();
});
}
</script>