简体   繁体   English

防止页面刷新在PHP表单提交?

[英]prevent page refresh on php form submit?

I have a page (http://qccr.dyndns.org:82/on-air-schedule) where a visitor can click a day of the week and have our radio station's schedule for that day displayed. 我有一个页面(http://qccr.dyndns.org:82/on-air-schedule),访客可以在其中单击一周中的某一天,并显示我们电台当天的时间表。 The only issue is that the page refreshes completely when that happens; 唯一的问题是发生这种情况时页面会完全刷新; it would be better if just the schedule in the centre of the page changed, not the whole page. 如果只更改页面中心的日程表,而不更改整个页面,那会更好。 I don't really know much about javascript...is there a simple way to do that? 我对javascript不太了解...有没有简单的方法可以做到这一点?

The relevant piece of code is here (I think): 相关的代码在这里(我认为):

    <?php



    <div class="date-input-form">
        <form action="" method="post" id="ddateform" >
            <input type="hidden" name="ddate" id="ddate" size="11" maxlength="10" value="" />
            <?php echo JHTML::_( 'form.token' ); ?>
        </form>
    </div>


    <div id="navcontainer">

    <ul id="navlist">
    <?php if($currentday=="Monday") : ; ?>
    <li class="currentday"><?php echo $currentday; ?></li>
    <?php else : ?>
    <li><a href="#" onclick="document.getElementById('ddate').value = '<?php echo date('Y-m-d', strtotime('monday')); ?>'; document.getElementById('ddateform').submit();">Monday</a></li>
    <?php endif; ?>

    <?php if($currentday=="Tuesday") : ; ?>
    <li class="currentday"><?php echo $currentday; ?></li>
    <?php else : ?>
    <li><a href="#" onclick="document.getElementById('ddate').value = '<?php echo date('Y-m-d', strtotime('tuesday')); ?>'; document.getElementById('ddateform').submit();">Tuesday</a></li>
    <?php endif; ?>

    <?php if($currentday=="Wednesday") : ; ?>
    <li class="currentday"><?php echo $currentday; ?></li>
    <?php else : ?>
    <li><a href="#" onclick="document.getElementById('ddate').value = '<?php echo date('Y-m-d', strtotime('wednesday')); ?>'; document.getElementById('ddateform').submit();">Wednesday</a></li>
    <?php endif; ?>

    <?php if($currentday=="Thursday") : ; ?>
    <li class="currentday"><?php echo $currentday; ?></li>
    <?php else : ?>
    <li><a href="#" onclick="document.getElementById('ddate').value = '<?php echo date('Y-m-d', strtotime('thursday')); ?>'; document.getElementById('ddateform').submit();">Thursday</a></li>
    <?php endif; ?>

    <?php if($currentday=="Friday") : ; ?>
    <li class="currentday"><?php echo $currentday; ?></li>
    <?php else : ?>
    <li><a href="#" onclick="document.getElementById('ddate').value = '<?php echo date('Y-m-d', strtotime('friday')); ?>'; document.getElementById('ddateform').submit();">Friday</a></li>
    <?php endif; ?>

    <?php if($currentday=="Saturday") : ; ?>
    <li class="currentday"><?php echo $currentday; ?></li>
    <?php else : ?>
    <li><a href="#" onclick="document.getElementById('ddate').value = '<?php echo date('Y-m-d', strtotime('saturday')); ?>'; document.getElementById('ddateform').submit();">Saturday</a></li>
    <?php endif; ?>

    <?php if($currentday=="Sunday") : ; ?>
    <li class="currentday"><?php echo $currentday; ?></li>
    <?php else : ?>
    <li><a href="#" onclick="document.getElementById('ddate').value = '<?php echo date('Y-m-d', strtotime('sunday')); ?>'; document.getElementById('ddateform').submit();">Sunday</a></li>
    <?php endif; ?>

    </ul>
</div>

I don't know much about PHP so take this with a grain of salt. 我对PHP不太了解,因此请多加考虑。

HTML forms do not refresh the page on submit by default. HTML表单默认不会在提交时刷新页面。 Something (I'm guessing the PHP) is causing an HTML form to be generated which causes a page refresh on form submit. 某种原因(我猜是PHP)导致生成HTML表单,从而导致表单提交时刷新页面。

I'm not sure if Barry's answer will work, but if you want to give it a try, replace the code for the Monday li with this: 我不确定Barry的答案是否会起作用,但是如果您想尝试一下,请使用以下代码替换Monday li的代码:

With your formatting 用你的格式

<?php if($currentday=="Monday") : ; ?>
    <li class="currentday"><?php echo $currentday; ?></li>
    <?php else : ?>
    <li><a href="#" onclick="document.getElementById('ddate').value = '<?php echo date('Y-m-d', strtotime('monday')); ?>'; document.getElementById('ddateform').submit(); return false;">Monday</a></li>
<?php endif; ?>

With sane formatting 具有理智的格式化

<script type="text/javascript">
    function onMondayClick(event) {
        document.getElementById('ddate').value = '<?php echo date('Y-m-d', strtotime('monday')); ?>';
        document.getElementById('ddateform').submit();
        return false;
    }
</script>

<?php if($currentday=="Monday") : ; ?>
    <li class="currentday"><?php echo $currentday; ?></li>
    <?php else : ?>
    <li><a href="#" onclick=onMondayClick>Monday</a></li>
<?php endif; ?>

In whatever javascript function you call, do a return false; 在您调用的任何javascript函数中,都return false; to prevent the default behavior. 以防止默认行为。

<a onclick="dothis(); return false;" href="#">Click Me</a>

-or- -要么-

<a onclick="dothis();" href="#">Click Me</a>
<script type="text/javascript">
  function dothis() { /* stuff here */ return false; }
</script>

Learn some javascript basics, you will be well served :) 了解一些javascript基础知识,将为您服务:)

您可以使用jquery post函数和/或通过ajax调用来发布formdata来执行此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM