how to get a post from a href javascript button - javascript

im using a template to make my project and there is a button which is a (a href ) styled button
I want it use it as post in php to pull data .. but its not working . this is the button
<div class="wrapper">
<span class="link1">
<strong>Search</strong>
</span>
</div>
and i want to use it here ...
<?php
if(isset($_POST['submit'])) {
echo '<script type="text/javascript">alert("This button works")</script>';
}
?>
but it does not seem to work ..plz help

Maybe you were referring to javascript instead of Java.
They are not the same.
But yes you could use javascript to submit the form using that anchor.
But first, you need to make sure that you have a form tag inside that markup.
Then bind an event on that anchor:
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
echo '<script type="text/javascript">alert("This button works")</script>';
}
?>
<div class="wrapper">
<form method="POST" id="your_form_id">
<span class="link1">
<strong>Search</strong>
</span>
</form>
</div>
<script type="text/javascript">
document.getElementById('hide').addEventListener('click', function(e){
e.preventDefault();
document.getElementById('your_form_id').submit();
});
</script>
Simple Demo
FYI: If you want a value to be passed, either utilize hidden input tags. Or put a value attribute and instead use a button.

if(isset($_POST['submit']))
it is false , as you have not submit any form.
Either make it
or add javascript/jquery form submit when clicking on href button.
Hope it helps.
Your May see this

Related

Second button doesn't produce form in same div as first button

I have a php page with two divs. In the first div, users click a button and it brings up a form in the second div. When they submit the form, both divs refresh. All is great. But I want to add a second button to the first div to bring up a different form in the second div. I haven't been able to get this to work, either the first button stops working or the second one doesn't work. I tried making the second button a div and calling it by its ID and that didn't work. I'm an amateur and don't understand jquery very well (I think this is jquery anyway) and this is just for a project for some friends and I, so any help would be appreciated. I may just not be understanding how this works.
<script>
$(document).ready(function () {
$("button").click(function () {
var handle = "<?php echo $_SESSION['name'] ?>";
$("#infoblock").load("createhandle.html");
});
});
</script>
<div id="div1">
<button id="reservename">Reserve A Name</button>
</div>
<div id="div2"></div>
I found a solution here: https://stackoverflow.com/a/20074373/9157720
It ended up being an issue with syntax and using a pound symbol. This is the code that worked in case anyone else has this problem.
<script>
$(document).ready(function(){
var handle = "<?php echo $_SESSION['name'] ?>";
$("#reservename").click(function(){
$("#infoblock").load( "createhandle.html");
});
$("#uploadpic").click(function(){
$("#infoblock").load("uploadpic.html");
});
});
</script>
<button id="reservename">Reserve A Name</button>
<button id="uploadpic">Upload A Picture</button>
Consider the following.
<script>
$(document).ready(function () {
$("button").click(function () {
var handle = $(this).data("ref");
var target = $(this).data("target");
$(target).load("createhandle.html?handle=" + handle);
});
});
</script>
<div id="div1">
<button data-ref="<?php echo $_SESSION['nameA']; ?>" data-target="#infoblock">Reserve A Name</button>
<button data-ref="<?php echo $_SESSION['nameB']; ?>" data-target="#div2">Reserve B Name</button>
</div>
<div id="div2">
</div>
Your example is sort of ambiguous, so I wasn't sure what you were trying to accomplish. It's best to not mix PHP and HTML if you can. You can have a reference point in the HTML so that when you need to load something, in relationship to that button, you can pass that into a stand alone PHP Script and get the HTML you need back with .load().
Remember that PHP is executed before the HTML is presented to the Client. So the Session data must be present in PHP Memory when the page is initially loaded.
Reference:
https://api.jquery.com/data/
https://api.jquery.com/load/
jQuery $(this) keyword

javascript submit() command not sending my post data

From this earlier thread I thought I learned that form data could be sent by POST method using javascript submit() command. But I can't get it to work. This demo doesn't make obvious sense with respect to purpose but bear with me. I just wanted to test this specific command which doesn't seem to work for me. Can you please help me? Clicking the regular submit button sends the post data ok but activating the javascript via the link does not.
<html><body>
<?php
$self = $_SERVER['PHP_SELF'];
$posttext = file_get_contents('php://input');
echo "Received input: ".chr(34).$posttext.chr(34)."<br><br>";
echo "<form id='test' action='{$self}' method='post'>";
?>
Please fill in the fields with any text:<br><br>
foo: <input type="text" name="foofield"><br><br>
bar: <input type="text" name="barfield"><br><br>
<input type="submit" value="Submit button works"><br><br>
Submitting by JS not working – why?
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
jQuery(function($) {
$(document).ready(function(){
$("a#submitlink").click(function(){
alert("Yes, the link has been clicked. It is not that.");
$("form#test").submit();
});
});
});
</script>
</body></html>
You need to: event.preventDefault(); the default behaviour from the <a>. The problem is if you don't do that, the page reloads cause that is what the <a> tag does. So even you submit the data you also refresh the page where the submitted data gets lost.
$("a#submitlink").click(function(event){
event.preventDefault();
$("form#test").submit();
});
The other solution is to add # in the href of the <a> tag like:
Submitting by JS not working – why?
this will also prevent the <a> tag from doing a refresh.

Cannot POST form to another php page

It should be quite simple, yet it does not work for me. I need to get user input to form and print it on another php page using GET or POST (because I think it is the easiest option and I am just building a prototype)
My default.php code:
<html>
<head>
<script src="pytimber.js"></script>
</head>
<body>
<p>Enter the parameter</p>
<form method="post" action="php/plot.php">
<input type="text" name='parameter' value="check"/>
<input type="button" value="Plot" class="homebutton" id="plot"
onclick="plot()"/>
</form>
</body>
</html>
Javascript code:
>
function plot() {
alert('JI')
document.location.href = 'php/plot.php';
}
function goBack() {
document.location.href = '../Default.php';
}
Second php page code:
<html>
<head>
<script src="../pytimber.js"></script>
<?PHP
$username = $_POST["parameter"];
print ($username);
?>
</head>
<html/>
1st : Simple remove all your javascript .
2nd : Change the button type to submit
<input type="submit" value="Plot" name="submit" class="homebutton" id="plot" />
3rd : In second page just echo the post variable with isset() function
if(isset( $_POST["parameter"]))
echo $_POST["parameter"];
Actually you don't need the js code, you just need to put a name on your button and change the type to submit and do this to another page:
<?php if(isset($_POST["buttonname"])){
echo $_POST["parameter"]; } ?>
As stated by different people here, the easiest way is to utilise the awesomeness of the submit button, and let HTML take care of the rest.
But, seeing as you use javascript to 'post' the form, let me explain how that could be done as well.
By adding an id to the form, and using that to submit through js, it will work.
Change the form tag and add an id:
<form method="post" action="php/plot.php" id="js_form">
Change the plot function to this:
function plot() {
alert('JI');
document.forms["js_form"].submit();
}
This way the javascript will act as a submit button and send all the data to plot.php
Either
change button type from 'button' to 'submit'
Or
change in js function 'plot'
function plot(){
document.forms[0].submit();
OR
document.getElementById('formid')'.submit;
}

onChange in form_dropdown codeigniter not work

i try to submit a form without submit button with javascript but not work. My code is:
<?php echo form_dropdown('week_id',$weeklist,'onChange="submitform();"','class ="form-control selectpicker" data-style="btn-danger"'); ?>
and the js is:
<script type="text/javascript">
function submitform() {
document.autosubmit.submit();
}
The name and the id of the form is autosubmit. Also i will try to put the week_id inside the function but not work too. Any suggestions? Thanks in advnace.
If you're going to use an ID:
document.querySelector('#autosubmit').submit();
If you just want to target the form, you need to use the name="" attribute, not the id="" attribute, something like this:
document.forms['autosubmit'].submit();

External POST form through iframe

I'm trying to get a page which includes a POST form to load inside an iframe for users to be able to confirm an action such as deleting a listing. I have successfully got the JS and the form page to load inside the iframe, but when I click an action inside the form, it's not updating the listing. Instead, it just refreshes the page.
The form is working fine because if I try to access the content-url manually in the browser and update the form, it works fine.
How can I get the iframe content to refresh instead of the parent page and load the response inside the iframe itself? What I am doing wrong exactly?
Any help is appreciated!!
Here's my code:
HTML:
<button class="myclass" content-url="<?php bloginfo('siteurl') ?>/?a_action=delete_auction&pid=<?php the_ID(); ?>">Delete Me</button>
<div class="popup"><iframe id="mynewiframeid" name="myframename" src=""></iframe></div>
JS:
$(document).ready(function() {
$(document).click(function(e) {
if ($(".popup").is(":visible")) {
$(".popup").fadeOut("fast")
}
});
$(".myclass").click(function() {
if (!$(".popup").is(":visible")) {
$('#mynewiframeid').attr('src',$(this).attr('content-url'));
$(".popup").fadeIn("slow");
}
return false;
});
$(".popup").click(function(e) {
e.stopPropagation()
})
});
Form:
<div class="popup_content">
<h3> You are about to delete <b><?php echo $title; ?></b>!</h3>
<?php
if(isset($_POST['yes_confirm']))
{
$s = "update ".$wpdb->prefix."posts set post_status='trash' where id='$pid'";
$wpdb->query($s);
echo '<div class="deleted_item_ok">';
printf(__('Your item has been deleted successfully!'));
echo '</div>';
}
else
{
?>
<form method="post">
<div class="are_you_sure_delete">
<?php
_e('Are you sure you want to delete this item?');
?>
</div>
<button class="button-small button-w-green" type="submit" id="submits" name="yes_confirm">Yes, Delete</button>
<button class="button-small button-w-red" type="submit" id="submits" name="no_confirm">No!</button>
</form>
<?php } ?>
</div>
You are trying to replace html in the class "popup" with following js code..
HTML => <div class="popup"><iframe id="iframeid" src=""></iframe></div>
JS => $('.popup').html(response);
This replaces the iframe element in the HTML above with the response..! Therefore after response comes, no iframes inside the page..
If you do want to use an iframe, I don't think you have to deal with AJAX. Instead just change the src of iframe. You may use following js code..
$(document).ready(function() {
$(document).click(function(e) {
if ($(".popup").is(":visible")) {
$(".popup").fadeOut("fast")
}
});
$(".myclass").click(function() {
if (!$(".popup").is(":visible")) {
$('#iframeid').attr('src',$(this).attr('content-url'));
$(".popup").fadeIn("slow");
}
return false;
});
$(".popup").click(function(e) {
e.stopPropagation()
})
});
Fiddle (with fake src urls)
First of all, correct the following errors and then see if it works:
in your you are missing semicolon after the closing parenthesis
you close the php tags and right after you continue with url and then you add another closing php tags
What you are looking for is this inside your content-url:
<?php bloginfo('siteurl') . '/?a_action=delete_auction&pid=' . $the_ID; ?>
What is the_ID()? I dont see that function. And if exists it has to return the value that you will store in variable $the_ID. You also have to call that function before button tag to get that variable.
In your JS you need to wrap everything in $(document).ready(function() {}
It seems you are trying to do GET request and not POST request since you are not sending any data in the body but in the url
Missing semicolon at the end of first .click function
I didn't even look at all other .click functions cause there's some repetitions going on and more weird stuff

Categories